我有很多纯文本文件(在.txt中)。我需要使用带标记的语料库阅读器并为我的项目分类:
首先,我需要使用每个单词的POS标记这些文件。
是否有任何图书馆或方法可以在没有代码的情况下自行迭代每个单词并找到POS并使用' /'旁边的这个词。因为如果我手动不同的段落都会出现一个段落
我编写的代码用于生成文本文件,其中的每个单词都包含它的POS(例如,public将成为public / JJ)
from nltk.corpus import PlaintextCorpusReader from nltk.tokenize import word_tokenize
import nltk
class CorpusInitialize:
def loading_textfile(self):
corpus_root = 'C:/Users/nkumarn/PycharmProjects/Corpus1/'
wordlists = PlaintextCorpusReader(corpus_root,'.*')
files=wordlists.fileids()
for eachfile in files:
textfile = wordlists.paras(fileids=eachfile)
text=self.set_paragraphs(textfile)
self.write_to_textfile(text,eachfile)
def set_paragraphs(self, textfile):
new_text = ""
flag = 0
for all_paras in textfile:
for every_para in all_paras:
if every_para != " ":
for every_word in every_para:
if new_text == "":
new_text = every_word
elif every_word == '.' or every_word == '?' or every_word == ',' or every_word == '!':
new_text = new_text + every_word
elif every_word == '@':
flag = 1
new_text = new_text + " " + every_word
else:
if flag == 1:
new_text = new_text + every_word
else:
new_text = new_text + " " + every_word
if new_text != "":
new_text = new_text + " " + '~'
text= self.create_corpos(new_text)
return text
def create_corpos(self,new_text):
words = word_tokenize(new_text)
all_pos_words = nltk.pos_tag(words)
text=""
for every_pos_words in all_pos_words:
if every_pos_words[0] == '~':
text = text + '\n'
continue
if text == "":
text = every_pos_words[0] + '/' + every_pos_words[1] + ' '
else:
text = text + every_pos_words[0] + '/' + every_pos_words[1] + ' '
return text
def write_to_textfile(self,textfile,fileid):
file = open("C:/Users/nkumarn/PycharmProjects/taggedcorpus/%s"%(fileid,), "w")
file.write(textfile)
file.close()
此文件的输入是纯文本文件: 例如:
"""无监督学习是推断a的机器学习任务 用于描述未标记数据的隐藏结构。自从 给学习者的例子没有标签,没有错误或 奖励信号以评估潜在的解决方案。无监督学习 与密度估计问题密切相关 统计信息。[1]然而,无监督学习也包含许多 寻求总结和解释关键特征的其他技术 数据。"""
并且此文件的输出将如下所示。
无监督/ VBN学习/ NN是/ VBZ / DT机器/ NN学习/ VBG 任务/ NN / IN推断/ VBG a / DT功能/ NN到/ TO描述/ VB 隐藏/ JJ结构/ NN来自/ IN未标记/ JJ数据/ NNS ./。既然/ IN / DT示例/ NNS给定/ VBN到/ TO / DT学习器/ NN是/ VBP unlabeled / VBN,/,/ EX是/ VBZ否/ DT错误/ NN或/ CC奖励/ JJ 信号/ NN到/ TO评估/ VB a / DT电位/ JJ解/ NN ./。 无监督/ VBN学习/ NN是/ VBZ紧密/ RB相关/ VBN到/ TO / DT问题/ NN / IN密度/ NN估计/ NN in / IN statistics / NNS ./。 [/ $ 1 / CD] / NNP然而/ RB,/,无监督/ JJ学习/ NN也/ RB 包含/ VBZ很多/ JJ其他/ JJ技术/ NNS / WDT寻求/ VBP to / TO总结/ VB和/ CC解释/ VB键/ JJ功能/ /在/ DT中的NNS data / NN ./.
所以回过头来问一下,是否有任何库或更简单但有效的方法来提出这个输出。我正在尝试获得类似于棕色语料库的输出结果,因此我可以使用所有taggedcorperreader函数。
我还使用过纯文本语料库,但是现在我的项目不需要。 请帮助我找到解决方案...我希望我必须成为一个错过的方式