正向最大匹配(自然语言处理)(机器学习)

代码如下:

# -*- coding:utf-8 -*-
"""
author: 15025
time: 2021/8/4 9:05
software: PyCharm

Description:
    正向最大匹配:Maximum Match Method(MM)
"""


class MM:
    def __init__(self, dict_path):
        # define a dictionary set
        self.dictionary = set()
        # define a variable
        self.maximum = 0
        # read dictionary
        with open(dict_path, 'r', encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                # jump the blank row in IMM_Dict file
                if not line:
                    continue
                # add reading element in our dictionary
                self.dictionary.add(line)
                # get the maximum length of phrase in our dictionary
                if len(line) > self.maximum:
                    self.maximum = len(line)
        # print the element in dictionary
        # print(self.dictionary)

    def cut(self, text):
        # create a list to save the final result
        result = []
        # get first index of string
        index = 0
        # if text is not bland, start matching process
        while index < len(text):
            word = None
            # start from the index of first word and end at the index of first word(len(text))
            # use the maximum matching phrase to match
            for size in range(self.maximum, 0, -1):
                # if the final index exceed the len(text), keep doing loop
                if index + size > len(text):
                    continue
                # get text
                piece = text[index:(index+size)]
                if piece in self.dictionary:
                    word = piece
                    result.append(word)
                    index += size
                    break
            # if no matching is find, just increase the index value by 1
            if word is None:
                index += 1

        return result


if __name__ == '__main__':
    text_ = "西安市大雁塔"
    file_path = r"C:/Users/15025/Desktop/NLP/IMM_Dict.txt"
    NLP = MM(file_path)
    print(NLP.cut(text_))
"""
['西安市', '大雁塔']
"""

其中对应的IMM_Dict字典文件内容如下图所示。
在这里插入图片描述
代码注释已经十分清晰了,这里不做过多的解释了,如果在阅读时遇到问题,可以评论区留言给我。

码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤奋的大熊猫

你的鼓励将是我写作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值