当我们使用其他分词工具进行分词之后,我们可以使用pyltp对已经分词后的语句进行词性标注。
from pyltp import Postagger
def posttagger(words):postagger = Postagger()postagger.load("/home/liusenubuntu/program/ltp_data/pos.model")posttags = postagger.postag(words) postags = list(posttags)postagger.release() return postags
file=open("/home/liusenubuntu/file/sentence/宫腔粘连MMseg分词.txt",'r',encoding='utf8')
outfile=open("/home/liusenubuntu/file/sentence/宫腔粘连MMseg分词词性标注.txt",'w',encoding='utf8')
for words in file:words=words.strip().split(' ')postags = posttagger(words)for postag, word in zip(postags, words):outfile.write(word + "|" + postag+" ")print(word + " | " + postag)outfile.write("\n")
outfile.close()