jeudi 15 décembre 2016

Python how to overcome TypeError: 'bool' object is not iterable

My problem is that i need to have two conditions inside of an ifany() statement, the two conditions are checking if a word from a list appears in a line AND if another word from secondary list also appears in the same line. If it does then print it. I have used this if in a previous function just checking one with so i didn't need to use And operator, I've tried reading up for alternatives but other operator's wouldn't allow me to achieve my condition.

the lword in cleanLex (cleanLex is a list created and populated inside of readfiles.py)

Here is the partial code showing function

Main.py:

from collections import Counter
from Categories.GainingAccess import GA
from Readfiles import *

# Speech Act Categories
CategoryGA = GA
# ----------------------

# Word hit counters
CategoryHits = []
LexiconHits = []
# ----------------------

# unsure if used at this point
cleansedLex = []
# ----------------------

# Lists to hold lines where words have been matched
matchedCatlines = []
matchedLexlines = []
TestLine = []


    def languagemodel():


    for line in cleanChat.values():
        for lword in [cleanLex]:
            for cword in [CategoryGA]:
                for section in line:
                                    if any(lword and cword in section for lword in
                   cleanLex and cword in CategoryGA):     # searches each section to find words matching words stored in cleanLex
                        pattern = r"\b" + re.escape(lword) + r"\b"  # patern to match containing Lword
                        if re.search(pattern, section, re.IGNORECASE):  # Running pattern
                            result = re.search(pattern, section,
                                           re.IGNORECASE)  # if match it displays match word with full line
                            if section.find(lword) != -1:
                                Word_Hit = True
                                LexiconHits.append(lword)
                                if section not in matchedLexlines:
                                    matchedLexlines.append(section)

languagemodel()

Traceback Error:

Traceback (most recent call last):
  File "C:/Users/Lewis Collins/Downloads/test/main.py", line 100, in <module>
    languagemodel()
  File "C:/Users/Lewis Collins/Downloads/test/main.py", line 89, in languagemodel
    cleanLex and cword in CategoryGA):  # searches each section to find words matching words stored in cleanLex
TypeError: 'bool' object is not iterable

Any suggestions as to how to get around or fix this issue.

Aucun commentaire:

Enregistrer un commentaire