python – NLTK的XMLCorpusReader可用于多文件语料库吗?
发布时间:2020-12-20 12:18:36 所属栏目:Python 来源:网络整理
导读:我正在尝试使用NLTK在 New York Times Annotated Corpus上做一些工作,其中包含每篇文章的XML文件(采用新闻行业文本格式NITF). 我可以解析单个文档,没有问题,如下: from nltk.corpus.reader import XMLCorpusReaderreader = XMLCorpusReader('nltk_data/corp
我正在尝试使用NLTK在
New York Times Annotated Corpus上做一些工作,其中包含每篇文章的XML文件(采用新闻行业文本格式NITF).
我可以解析单个文档,没有问题,如下: from nltk.corpus.reader import XMLCorpusReader reader = XMLCorpusReader('nltk_data/corpora/nytimes/1987/01/01',r'0000000.xml') 我需要在整个语料库上工作. reader = XMLCorpusReader('corpora/nytimes',r'.*') 但这不会创建一个可用的读者对象.例如 len(reader.words()) 回报 raise TypeError('Expected a single file identifier string') TypeError: Expected a single file identifier string 如何将此语料库读入NLTK? 我是NLTK的新手,所以非常感谢任何帮助. 解决方法
我不是NLTK专家,所以可能有一种更简单的方法来做到这一点,但我天真地建议你使用
Python’s
glob module.它支持Unix-stle路径名模式扩展.
from glob import glob texts = glob('nltk_data/corpora/nytimes/*') 这样就会以列表形式给出与指定表达式匹配的文件的名称. from nltk.corpus.reader import XMLCorpusReader for item_path in texts: reader = XMLCorpusReader('nltk_data/corpora/nytimes/',item_path) 正如@waffle paradox所建议的那样:你也可以缩小这个文本列表以满足你的特定需求. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |