【发布时间】:2012-03-19 21:01:07
【问题描述】:
我刚刚开始使用 nltk,但无法让 concordance 模块使用条件变量。我想为拉丁文本中的任何给定单词返回一个索引,但由于语言是屈折的,我希望能够指定词干,识别语料库中包含词干的任何单词,并返回一个索引.我使用的代码是:
book1 = open('Book1.txt', 'rU').read()
token1 = nltk.word_tokenize(book1)
text1 = nltk.Text(token1)
word = raw_input("What stem do you want to search?\n > ")
text1.concordance([w for w in text1 if w.startswith(word)])
返回错误:
Traceback (most recent call last):
File "C:\Users\admin\Desktop\start_nltk_horace.py", line 68, in <module>
concordance()
File "C:\Users\admin\Desktop\start_nltk_horace.py", line 49, in concordance
text1.concordance([w for w in text1 if w.startswith(word)])
File "C:\Python27\lib\site-packages\nltk\text.py", line 314, in concordance
self._concordance_index.print_concordance(word, width, lines)
File "C:\Python27\lib\site-packages\nltk\text.py", line 177, in print_concordance
offsets = self.offsets(word)
File "C:\Python27\lib\site-packages\nltk\text.py", line 156, in offsets
word = self._key(word)
File "C:\Python27\lib\site-packages\nltk\text.py", line 312, in <lambda>
key=lambda s:s.lower())
AttributeError: 'list' object has no attribute 'lower'
只需指定 text1.concordance(word) 即可返回我正在寻找的内容,没有任何问题(假设我输入了完全拒绝的词),但我必须重复该函数六次才能获得所有不同的一致性一个词的变格。
【问题讨论】:
标签: python conditional nltk