我尝试从外部.txt
文件中构建一个POS标记的语料库,用于分块以及实体和关系提取。到目前为止,我已经找到了一种繁琐的多步解决方案:
Read files with into a plain text corpus:
from nltk.corpus.reader import PlaintextCorpusReader my_corp = PlaintextCorpusReader(".", r".*\.txt")
Tag corpus with built-in Penn POS-tagger:
my_tagged_corp= nltk.batch_pos_tag(my_corp.sents())
NameError: name 'batch' is not defined
)
Write out tagged sentences into file:
taggedfile = open("output.txt" , "w") for sent in dd_tagged: line = " ".join( w+"/"+t for (w, t) in sent ) taggedfile.write(line + "\n") taggedfile.close ()
And finally, read this output again as tagged corpus:
from nltk.corpus.reader import TaggedCorpusReader my_corpus2 = TaggedCorpusReader(".",r"output.txt")
对于一个非常普遍的任务(分块总是需要标记过的语料库),这一切都非常不方便。我的问题是:是否有更紧凑、更优雅的实现方式?例如,同时获取原始输入文件和标记器的语料库阅读器?
nltk_data/corpora
文件夹导入我的语料库。您能否提供详细的创建步骤? - Aditi