自然语言处理python进阶(一)

from nltk.corpus import reuters #加载路透语料库
files = reuters.fileids()
print(len(files)) # 10788
words16097 = reuters.words(['test/16097'])
print(words16097)

什么是语料

就是一个个txt,一个txt就是一个列表

categories种类

reutersGenres = reuters.categories()
print(reutersGenres)  # 90

加载语料特定的种类的句子

# 加载语料特定的种类
for w in reuters.words(categories=['bop','cocoa']):print(w +' ',end='')if(w is '.'): # 一个句子就换行print()

实际生活中要自己下载语料来搞

读取下载的语料

from nltk.corpus import CategorizedPlaintextCorpusReader# 语料将分成不同的种类的txt,所以采用正则的方法来读取txt
reader = CategorizedPlaintextCorpusReader(r'/Volumes/Data/NLP-CookBook/Reviews/txt_sentoken', r'.*\.txt', cat_pattern=r'(\w+)/*')
print(reader.categories())  


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部