问题描述
我有一个关于 NLTK 中 Python concordance 命令的问题.首先,我通过一个简单的例子:
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'))
concordance(word, width=79,lines=25)
使用指定的上下文窗口打印单词的索引.单词匹配不区分大小写.
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 一致性命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!