2024-09-30 02:36:14 发布
网友
我能从自然语言中找到很多东西,但我不能用自然的方式去阅读。我试过这样的事情:
from nltk.book import * text6 #Brings the title of the text open(text6).read() #or nltk.book.text6.read()
但它似乎不起作用,因为它没有fileid。以前好像没人问过这个问题,所以我想答案应该很简单。你知道怎样阅读这些文本或者如何把它们转换成字符串吗? 提前谢谢
看来他们已经把它分成了代币。在
from nltk.book import text6 text6.tokens
让我们深入研究代码=)
首先,nltk.book代码驻留在https://github.com/nltk/nltk/blob/develop/nltk/book.py上
nltk.book
如果我们仔细看一下,文本是作为nltk.Text对象加载的,例如text6来自https://github.com/nltk/nltk/blob/develop/nltk/book.py#L36:
nltk.Text
text6
text6 = Text(webtext.words('grail.txt'), name="Monty Python and the Holy Grail")
Text对象来自https://github.com/nltk/nltk/blob/develop/nltk/text.py#L286,您可以从http://www.nltk.org/book/ch02.html了解如何使用它
Text
webtext是来自nltk.corpus的语料库,因此要获得nltk.book.text6的原始文本,可以直接加载webtext,例如
webtext
nltk.corpus
nltk.book.text6
只有当您加载一个PlaintextCorpusReader对象时,fileids才会出现,而不是从Text对象(已处理对象)加载:
PlaintextCorpusReader
fileids
>>> type(webtext) <class 'nltk.corpus.reader.plaintext.PlaintextCorpusReader'> >>> for filename in webtext.fileids(): ... print(filename) ... firefox.txt grail.txt overheard.txt pirates.txt singles.txt wine.txt
看来他们已经把它分成了代币。在
让我们深入研究代码=)
首先,
nltk.book
代码驻留在https://github.com/nltk/nltk/blob/develop/nltk/book.py上如果我们仔细看一下,文本是作为
nltk.Text
对象加载的,例如text6
来自https://github.com/nltk/nltk/blob/develop/nltk/book.py#L36:Text
对象来自https://github.com/nltk/nltk/blob/develop/nltk/text.py#L286,您可以从http://www.nltk.org/book/ch02.html了解如何使用它
^{pr2}$webtext
是来自nltk.corpus
的语料库,因此要获得nltk.book.text6
的原始文本,可以直接加载webtext,例如只有当您加载一个
PlaintextCorpusReader
对象时,fileids
才会出现,而不是从Text
对象(已处理对象)加载:相关问题 更多 >
编程相关推荐