[英]How to build POS-tagged corpus with NLTK?
我嘗試從外部.txt
文件構建一個POS標記的語料庫,用於分塊和實體及關系提取。 到目前為止,我發現了一個繁瑣的多步解決方案:
使用純文本語料庫讀取文件:
from nltk.corpus.reader import PlaintextCorpusReader my_corp = PlaintextCorpusReader(".", r".*\\.txt")
標簽語料庫,內置Penn POS-tagger:
my_tagged_corp= nltk.batch_pos_tag(my_corp.sents())
(順便說一句,在這篇文章中Python引發了一個錯誤: NameError: name 'batch' is not defined
)
將標記的句子寫入文件:
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 ()
最后,再次將此輸出作為標記語料庫讀取:
from nltk.corpus.reader import TaggedCorpusReader my_corpus2 = TaggedCorpusReader(".",r"output.txt")
這對於一個非常常見的任務來說非常不方便(分塊總是請求標記的語料庫)。 我的問題是:是否有更緊湊和優雅的方式來實現這一點? 例如,同時獲取原始輸入文件和標記器的語料庫閱讀器?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.