好的,我搜索了更多,我发现如何获得这些标签。 首先必须做一些预处理,以确保该文件将得到标记(在我的情况下,它是关于从pdf转换为txt后删除了一些遗留的东西)。
然后这些文件必须被标记为句子,然后将每个句子转换为单词数组,然后可以通过nltk tagger进行标记。通过这种词法化可以完成,然后在其上添加词干。
from nltk.tokenize import sent_tokenize, word_tokenize
# use sent_tokenize to split text into sentences, and word_tokenize to
# to split sentences into words
from nltk.tag import pos_tag
# use this to generate array of tuples (word, tag)
# it can be then translated into wordnet tag as in
# [this response][1].
from nltk.stem.wordnet import WordNetLemmatizer
from stemming.porter2 import stem
# code from response mentioned above
def get_wordnet_pos(treebank_tag):
if treebank_tag.startswith('J'):
return wordnet.ADJ
elif treebank_tag.startswith('V'):
return wordnet.VERB
elif treebank_tag.startswith('N'):
return wordnet.NOUN
elif treebank_tag.startswith('R'):
return wordnet.ADV
else:
return ''
with open(myInput, 'r') as f:
data = f.read()
sentences = sent_tokenize(data)
ignoreTypes = ['TO', 'CD', '.', 'LS', ''] # my choice
lmtzr = WordNetLemmatizer()
for sent in sentences:
words = word_tokenize(sentence)
tags = pos_tag(words)
for (word, type) in tags:
if type in ignoreTypes:
continue
tag = get_wordnet_pos(type)
if tag == '':
continue
lema = lmtzr.lemmatize(word, tag)
stemW = stem(lema)
而在这一点上,我得到朵朵字stemW
,我可以再写入文件,并使用这些计算每个文档TFIDF向量。