zoukankan      html  css  js  c++  java
  • 使用哈工大LTP进行文本命名实体识别并保存到txt

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
    本文链接:https://blog.csdn.net/broccoli2/article/details/84025285
    需求说明:
    (1)将计算机本地文档集中的文本进行分词、词性标注,最后进行命名实体识别。
    (2)将(1)中处理结果保存到本地txt文件中。

    技术选择:
    本需求的实现使用了哈工大的pyltp,如果你对ltp还不太了解,请点击这里或者去哈工大语言云官网了解相关内容。

    完整代码展示:

    # -*- coding: utf-8 -*-
    import os
    import jieba

    LTP_DATA_DIR = 'D:pyprojectsLTPltp_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(' '.join(sents))


    # 分词
    def segmentor(sentence):
    segmentor = Segmentor() # 初始化实例
    segmentor.load(cws_model_path) # 加载模型
    #segmentor.load_with_lexicon('cws_model_path', 'D:pyprojectsLTPltp_datadict.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 + ' ')


    for tag in dict:
    num = tags.count(tag)
    print(tag + ":"+str(num))
    f1.write(tag + ":"+str(num) + ' ')
    f1.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    效果展示:


    参考:
    https://blog.csdn.net/informationscience/article/details/76850652
    ————————————————
    版权声明:本文为CSDN博主「broccoli2」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/broccoli2/article/details/84025285

  • 相关阅读:
    Apache2的安装
    JVM(9) 程序编译及代码优化
    Java基础(43)Queue队列
    Java基础(42)AbstractSet类
    OptimalSolution(10)--日常
    OptimalSolution(9)--其他问题(1)
    OptimalSolution(9)--其他问题(2)
    OptimalSolution(8)--位运算
    OptimalSolution(7)--大数据和空间限制
    golang教程汇总
  • 原文地址:https://www.cnblogs.com/jfdwd/p/11468776.html
Copyright © 2011-2022 走看看