问题描述
我对NLTK中的Python一致性命令有疑问.首先,我举了一个简单的例子:
I have a question regarding Python concordance command in NLTK. First, I came through an easy example:
from nltk.book import *
text1.concordance("monstrous")
效果很好.现在,我有自己的.txt文件,我想执行相同的命令.我有一个名为"textList"的列表,想找到单词"CNA",所以我输入了命令
which worked just fine. Now, I have my own .txt file and I would like to perform the same command. I have a list called "textList" and want to find the word "CNA" so I put command
textList.concordance('CNA')
但是,我得到了错误
AttributeError: 'list' object has no attribute 'concordance'.
在示例中,text1不是列表吗?我不知道这是怎么回事.
In the example, is the text1 NOT a list? I wonder what is going on here.
推荐答案
.concordance()
是特殊的nltk函数.因此,您不能只在任何python对象(例如您的列表)上调用它.
.concordance()
is a special nltk function. So you can't just call it on any python object (like your list).
更具体地说:.concordance()
是 Text
中的方法nltk的类别
More specifically: .concordance()
is a method in the Text
class of nltk
基本上,如果要使用.concordance()
,则必须首先实例化Text对象,然后在该对象上对其进行调用.
Basically, if you want to use the .concordance()
, you have to instantiate a Text object first, and then call it on that object.
import nltk.corpus
from nltk.text import Text
moby = Text(nltk.corpus.gutenberg.words('melville-moby_dick.txt'))
在指定的上下文窗口中打印单词的一致性.单词匹配不区分大小写.
Print a concordance for word with the specified context window. Word matching is not case-sensitive.
所以我想像这样的事情会起作用(未经测试)
So I imagine something like this would work (not tested)
import nltk.corpus
from nltk.text import Text
textList = Text(nltk.corpus.gutenberg.words('YOUR FILE NAME HERE.txt'))
textList.concordance('CNA')
这篇关于NLTK中的Python一致性命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!