短文本相似度匹配

短文本相似度匹配

服务器环境:

Centos 7.x

python环境:

3.6.X

问题描述:

1.项目中遇到这样一个问题:公司的正式名称和工作人员手动录入的公司名称匹配问题。

例如:

杭州艾索电子科技有限公司杭州艾索电子公司

豪世华邦(和平店)豪世华邦

浙江维尔科技股份有限公司浙江维尔科技有限公司

工作人员手动录入时会把某些公司名称缩写,但是项目中需要的是公司全称。因此需要来匹配相似度,如果满足条件的会返回Match,不满足条件的返回Mismatch

#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 2019-04-15 20:07
# @Author : Duke
# @Email: duke_coding@163.com
# @File : similarity.py
import math
import pkuseg
seg = pkuseg.pkuseg()           # 以默认配置加载模型

def compute_cosine(sentence_a, sentence_b):
    # 找单词及词频
    words_a = seg.cut(sentence_a)  # 进行分词
    words_b = seg.cut(sentence_b)  # 进行分词


    words_a_dict = {}
    words_b_dict = {}
    for word in words_a:
        if word != '' and word in words_a_dict:
            num = words_a_dict[word]
            words_a_dict[word] = num + 1
        elif word != '':
            words_a_dict[word] = 1
        else:
            continue
    for word in words_b:
        if word != '' and word in words_b_dict:
            num = words_b_dict[word]
            words_b_dict[word] = num + 1
        elif word != '':
            words_b_dict[word] = 1
        else:
            continue
    print(words_a_dict)
    print(words_b_dict)

    # 排序
    dic1 = sorted(words_a_dict.items(), key=lambda asd: asd[1], reverse=True)
    dic2 = sorted(words_b_dict.items(), key=lambda asd: asd[1], reverse=True)
    print(dic1)
    print(dic2)

    # 得到词向量
    words_key = []
    for i in range(len(dic1)):
        words_key.append(dic1[i][0])  # 向数组中添加元素
    for i in range(len(dic2)):
        if dic2[i][0] in words_key:
            pass
        else:  # 合并
            words_key.append(dic2[i][0])

    vector_a = []
    vector_b = []
    for word in words_key:
        if word in words_a_dict:
            vector_a.append(words_a_dict[word])
        else:
            vector_a.append(0)
        if word in words_b_dict:
            vector_b.append(words_b_dict[word])
        else:
            vector_b.append(0)
    print(vector_a)
    print(vector_b)

    # 计算余弦相似度
    sum = 0
    sq1 = 0
    sq2 = 0
    for i in range(len(vector_a)):
        sum += vector_a[i] * vector_b[i]
        sq1 += pow(vector_a[i], 2)
        sq2 += pow(vector_b[i], 2)
    try:
        result = round(float(sum) / (math.sqrt(sq1) * math.sqrt(sq2)), 2)
    except ZeroDivisionError:
        result = 0.0
    return result

def return_result(text1, text2):
    if compute_cosine(text1,text2) > 0.5:
        return 'Match'
    else:
        return 'Mismatch'

if __name__ == '__main__':
    text1 = '杭州艾索电子科技有限公司'
    text2 = '杭州艾索电子公司'
    print(return_result(text1,text2))

当然,这只是一个简单的demo。因为员工可能会录入错字,比如说豪写成毫或者其他笔误;或者录入时短据顺序错乱,这都是有可能的。要解决这些问题,需要新训练一个模型,来解决这些问题。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值