码迷,mamicode.com
首页 > 其他好文 > 详细

使用pyltp提取文本中的地址

时间:2018-06-13 23:30:53      阅读:611      评论:0      收藏:0      [点我收藏+]

标签:segment   doc   技术分享   get   lease   下载   os.path   +=   let   

class Singleton(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, ‘_the_instance‘):
            cls._the_instance = object.__new__(cls, *args, **kwargs)
        return cls._the_instance
  • 使用pyltp提取地址
import os
from pyltp import Segmentor, Postagger, NamedEntityRecognizer
from main.models.Singleton import Singleton

class address_extract_model(Singleton):
    print(‘load ltp model start...‘)

    pwd = os.getcwd()
    project_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")

    LTP_DATA_DIR = project_path + ‘\AlarmClassification\main\ltp\model‘  # ltp模型目录的路径
    cws_model_path = os.path.join(LTP_DATA_DIR, ‘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`

    print(‘path‘ + cws_model_path)

    segmentor = Segmentor()  # 初始化实例
    segmentor.load(cws_model_path)  # 加载模型

    postagger = Postagger() # 初始化实例
    postagger.load(pos_model_path)  # 加载模型

    recognizer = NamedEntityRecognizer() # 初始化实例
    recognizer.load(ner_model_path)  # 加载模型


    def get_model(self):
        return self.segmentor, self.postagger, self.recognizer


def get_address_prediction(alarm_content):
    model = address_extract_model()
    segmentor, postagger, recognizer = model.get_model()

    words = segmentor.segment(alarm_content)  # 分词
    postags = postagger.postag(words)  # 词性标注
    netags = recognizer.recognize(words, postags)  # 命名实体识别

    result = ‘‘
    for i in range(0, len(netags)):
        print(words[i] + ‘: ‘ + netags[i])
        # 地名标签为 ns
        if ‘s‘ in netags[i]:
            result += words[i] + ‘,‘
    if len(result) < 1:
        result = ‘No address!‘
    print(result)
    return result


def get_address(alarm_content):
    print("start get_address...")
    result = "Exception"
    try:
        result = get_address_prediction(alarm_content)
    except Exception as ex:
        print(ex)

    print("Output is " + result)
    return result

# segmentor.release()  # 释放模型
# postagger.release()
# recognizer.release()
  • 运行效果

技术分享图片

使用pyltp提取文本中的地址

标签:segment   doc   技术分享   get   lease   下载   os.path   +=   let   

原文地址:https://www.cnblogs.com/bincoding/p/9180553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!