def clean_text (text):
'''Text Preprocessing '''
# Convert words to lower case
text = text.lower()
#Expand contractions
if True:
text = text.split()
new_text= []
for word in text:
if word in contractions:
new_text.append(contractions [word])
else:
new_text.append(word)
text = "".join(new_text)
# Format words and remove unwanted characters
text = re.sub(r'https?:\/\/[\r\n],"[\r\n]"', '', text, flags=re.MULTILINE)
text = re.sub(r'\<a href', ' ', text)
text = re.sub(r'&', '', text)
text- re.sub(r'[_"\-;%()|+&=*%.,!?:#$@\[\]/]',' ', text)
text = re.sub(r'<br />', ' ', text)
text = re.sub(r'\'', ' ', text)
#remove stopwords
if remove_stopwords:
text = text.split()
stops = set(stopwords.words ("english"))
text = [w for w in text if not w in stops]
text = "" .join(text)
# Tokenize each word
text = nltk.WordPunctTokenizer().tokenize(text)
text = nltk.TreebankWordTokenizer().tokenize(text)
text = nltk.WordPunctTokenizer().tokenize(text)
#Lemmatize each token
lemm = nltk.stem.WordNetLemmatizer()
text = list(map(lambda word:list(map(lemm.lemmatize, word)), text))
return text
当我运行上面的代码时,它运行时没有问题。
但是当我使用上面的def运行下面的代码时,它显示"argument of type 'module' is not iterable'
sentences_train = list(map(clean_text, sentences_train))
我尝试了不同的方法来解决这个问题,但这会使错误变得更严重。如果有人能帮我,告诉我为什么会发生这种情况,那就太好了。非常感谢。 我们会考虑任何建议
该错误似乎是由
contractions
引起的。我不知道您是如何创建contractions
的,但是请记住,只有当contractions
是一个类似于contractions = ["abc", "xyz", "123"]
您导入的
contradictions
与您实际想要迭代的内容似乎不匹配,以检查word
是否在某个内容中(没有很难说的完整示例)。有关相同的错误,请参见下面的示例。解决办法是使用contradictions.[something iterable from this module]
相关问题 更多 >
编程相关推荐