【发布时间】:2018-05-30 18:17:18
【问题描述】:
我正在尝试使用多线程来加快进程。我正在使用 wordnetlemmatizer 对单词进行词形还原,这些单词可以被 sentiwordnet 进一步用于计算文本的情绪。我使用 WordNetLemmatizer 的情绪分析功能如下:
import nltk
from nltk.corpus import sentiwordnet as swn
def SentimentA(doc, file_path):
sentences = nltk.sent_tokenize(doc)
# print(sentences)
stokens = [nltk.word_tokenize(sent) for sent in sentences]
taggedlist = []
for stoken in stokens:
taggedlist.append(nltk.pos_tag(stoken))
wnl = nltk.WordNetLemmatizer()
score_list = []
for idx, taggedsent in enumerate(taggedlist):
score_list.append([])
for idx2, t in enumerate(taggedsent):
newtag = ''
lemmatized = wnl.lemmatize(t[0])
if t[1].startswith('NN'):
newtag = 'n'
elif t[1].startswith('JJ'):
newtag = 'a'
elif t[1].startswith('V'):
newtag = 'v'
elif t[1].startswith('R'):
newtag = 'r'
else:
newtag = ''
if (newtag != ''):
synsets = list(swn.senti_synsets(lemmatized, newtag))
score = 0
if (len(synsets) > 0):
for syn in synsets:
score += syn.pos_score() - syn.neg_score()
score_list[idx].append(score / len(synsets))
return SentiCal(score_list)
运行 4 个线程后,前 3 个线程出现以下错误,最后一个线程运行良好。
AttributeError: 'WordNetCorpusReader' object has no attribute '_LazyCorpusLoader__args'
【问题讨论】:
-
LazyCorpusLoader应该在 Pool 之前评估 =) 如果没有人回答,我会在今天〜10 小时后回答。
标签: python multithreading python-3.x nltk wordnet