Python - 语法无效

时间:2018-05-10 10:30:57

标签: python spyder

我有这样的错误:我的代码中的语法无效:

   with gzip.open('EnCorp2Million.txt.gz', 'rb') as f:
try:
    anatxt = f.read().decode('utf-8') 
except ValueError as e:
   print("Error:",e)

def strip_html(text):
soup = BeautifulSoup(text, "html.parser")
return soup.get_text()
anatxt = strip_html(anatxt)

def remove_between_square_brackets(text):
return re.sub('\[[^]]*\]', '', text)
anatxt = remove_between_square_brackets(anatxt)

def denoise_text(text):
text = strip_html(text)
text = remove_between_square_brackets(text)
return text
anatxt = denoise_text(anatxt)

def replace_contractions(text):
   return contractions.fix(text)
anatxt = replace_contractions(anatxt)

words = nltk.word_tokenize(anatxt)

#1gram
onegram = ngrams(words, 1)
fdist_onegram = nltk.FreqDist(onegram)
for c,v in fdist_onegram.items(30):
    print (c,v)

#As c,v mais frequentes
print(fdist_onegram.most_common(30))  
print(fdist_onegram.plot(30)

#2gram
bigram = ngrams(words, 2)
fdist_bigram = nltk.FreqDist(bigram)

#As c,v mais frequentes
print(fdist_bigram.most_common(30))  
print(fdist_bigram.plot(30)

执行时,我收到错误:

bigram = ngrams(words, 2)
     ^
SyntaxError: invalid syntax

如果我删除了二元组计算,则错误消失,但我需要对7克进行计算

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

如果您看到一行看起来很好的语法错误,请查看上一行。在这种情况下,您忘记了右括号。

print(fdist_onegram.plot(30)