我試圖從標記文件中提取專有名詞。但問題是,我是用有時試圖代碼給出了一個錯誤,即:python錯誤 - 太多值
Traceback (most recent call last):
File "E:\pt\paragraph", line 35, in <module>
sen1= noun(mylist[s])
File "E:\pt\paragraph", line 5, in noun
word, tag = word.split('/')
ValueError: too many values to unpack
的代碼工作正常,一些文本但對於一些它給人的錯誤。
代碼:
def noun(words):
nouns = []
for word in words.split():
word, tag = word.split('/')
if (tag.lower() == 'np'):
nouns.append(word);
return nouns
def splitParagraph(paragraph):
import re
paragraphs = paragraph.split('\n\n')
return paragraphs
if __name__ == '__main__':
import nltk
rp = open("t3.txt", 'r')
text = rp.read()
mylist = []
para = splitParagraph(text)
for s in para:
mylist.append(s)
for s in range(len(mylist)-1):
sen1= noun(mylist[s])
sen2= noun(mylist[s+1])
當前我與作品想如果我刪除的第一段其他明智它給人的錯誤。
樣的文字:
A/at good/jj man/nn-hl departs/vbz-hl ./. Goodbye/uh-hl ,/,-hl Mr./np-hl Sam/np-hl./. Sam/np Rayburn/np was/bedz a/at good/jj man/nn ,/, a/at good/jj American/np ,/, and/cc ,/, third/od ,/, a/at good/jj Democrat/np ./. He/pps was/bedz all/abn of/in these/dts rolled/vbn into/in one/cd sturdy/jj figure/nn ;/. ;/. Mr./np Speaker/nn-tl ,/, Mr./np Sam/np ,/, and/cc Mr./np Democrat/np ,/, at/in one/cd and/cc the/at same/ap time/nn ./.
The/at House/nn-tl was/bedz his/pp$ habitat/nn and/cc there/rb he/pps flourished/vbd ,/, first/rb as/cs a/at young/jj representative/nn ,/, then/rb as/cs a/at forceful/jj committee/nn chairman/nn ,/, and/cc finally/rb in/in the/at post/nn for/in which/wdt he/pps seemed/vbd intended/vbn from/in birth/nn ,/, Speaker/nn-tl of/in-tl the/at-tl House/nn-tl ,/, and/cc second/od most/ql powerful/jj man/nn in/in Washington/np ./.
如果我刪除了第1段(A /在好/ JJ人/ NN-HL離開......)代碼工作。如何解決這個問題呢。
在此先感謝。