2
我有以下文本文件(您可以从here下载它)。Python - 用NLTK搜索文本
我试图在文件中搜索词language
。对于这一点,我有以下Python脚本:
import nltk
file = open('NLTK.txt', 'r')
read_file = file.read()
text = nltk.Text(read_file)
match = text.concordance('language')
print(match)
然而,当我运行程序时,我得到下面的输出,虽然该文件包含单词language
:
No matches
None
为什么不能节目找到这个词language
如果它存在于文件中?
编辑1
我注意到,声明text = nltk.Text(read_file)
回报:
<Text: T h i s i s ...>
感谢。
接受的答案是关于如何解决这个问题是正确的,但这里的另一个忠告:不要打扰学习与'Text'类工作;它仅用于交互式探索和演示。直接进入'PlaintextCorpusReader'(和其对应的注释格式)。 – alexis