使用哈工大LTP进行文本命名实体识别并保存到txt
需求说明:
(1)将计算机本地文档集中的文本进行分词、词性标注,最后进行命名实体识别。
(2)将(1)中处理结果保存到本地txt文件中。
技术选择:
本需求的实现使用了哈工大的pyltp,如果你对ltp还不太了解,请点击这里或者去哈工大语言云官网了解相关内容。
完整代码展示:
# -*- coding: utf-8 -*-
import os
import jieba
LTP_DATA_DIR = 'D:\pyprojects\LTP\ltp_data' # ltp模型目录的路径
cws_model_path = os.path.join(LTP_DATA_DIR, 'cws.model') # 分词模型路径,模型名称为`cws.model`
pos_model_path = os.path.join(LTP_DATA_DIR, 'pos.model') # 词性标注模型路径,模型名称为`pos.model`
ner_model_path = os.path.join(LTP_DATA_DIR, 'ner.model') # 命名实体识别模型路径,模型名称为`ner.model`
par_model_path = os.path.join(LTP_DATA_DIR, 'parser.model') # 依存句法分析模型路径,模型名称为`parser.model`
srl_model_path = os.path.join(LTP_DATA_DIR, 'srl') # 语义角色标注模型目录路径,模型目录为`srl`。注意该模型路径是一个目录,而不是一个文件。
from pyltp import SentenceSplitter
from pyltp import Segmentor
from pyltp import Postagger
from pyltp import NamedEntityRecognizer
from pyltp import Parser
from pyltp import SementicRoleLabeller
#创建停用词表
def stopwordslist(filepath):
stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
return stopwords
# 分句,也就是将一片文本分割为独立的句子
def sentence_splitter(sentence):
sents = SentenceSplitter.split(sentence) # 分句
print('\n'.join(sents))
# 分词
def segmentor(sentence):
segmentor = Segmentor() # 初始化实例
segmentor.load(cws_model_path) # 加载模型
#segmentor.load_with_lexicon('cws_model_path', 'D:\pyprojects\LTP\ltp_data\dict.txt') #加载模型 使用用户自定义字典的高级分词
words = segmentor.segment(sentence) # 分词
# 默认可以这样输出
# print('/'.join(words))
# 可以转换成List 输出
words_list = list(words)
segmentor.release() # 释放模型
return words_list
# 词性标注
def posttagger(words):
postagger = Postagger() # 初始化实例
postagger.load(pos_model_path) # 加载模型
postags = postagger.postag(words) # 词性标注
#for word, tag in zip(words, postags):
# print(word + '/' + tag)
postagger.release() # 释放模型
return postags
# 命名实体识别
def ner(words, postags):
recognizer = NamedEntityRecognizer() # 初始化实例
recognizer.load(ner_model_path) # 加载模型
netags = recognizer.recognize(words, postags) # 命名实体识别
#for word, ntag in zip(words, netags):
# print(word + '/' + ntag)
recognizer.release() # 释放模型
return netags
stopwords = stopwordslist('D:/2181729/stop_words.txt')
final = ''
f1=open('D:/2181729/nerfcdata/30.txt','w', encoding='UTF-8')
with open('D:/2181729/data/30.txt', 'r', encoding='UTF-8') as f:
for line in f:
segs = jieba.cut(line, cut_all=False)
for seg in segs:
if seg not in stopwords:
final += seg
words = segmentor(final)
postags = posttagger(words)
netags = ner(words,postags)
tags = []
dict = []
for word, ntag in zip(words, netags):
if(ntag != 'O'):#过滤非命名实体
tags.append(ntag)
if (ntag not in dict):
dict.append(ntag)
# print(word + '/' + ntag)
f1.write(word + ':' + ntag + '\r\n')
for tag in dict:
num = tags.count(tag)
print(tag + ":"+str(num))
f1.write(tag + ":"+str(num) + '\r\n')
f1.close()
效果展示:
参考:
https://blog.csdn.net/informationscience/article/details/76850652