IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 逆向最大匹配(自然语言处理) -> 正文阅读

[人工智能]逆向最大匹配(自然语言处理)

作者:>

今天学习了一种自然语言匹配中的中文分词方法,逆向最大匹配。

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

Description:
    逆向最大匹配: Reverse Maximum Match Method(RMM)
"""


class RMM:
    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 = []
        index = len(text)
        # if text is not bland, start matching process
        while index > 0:
            word = None
            # start from the index of last word(self.maximum) and end at the index of first word(0)
            # use the maximum matching phrase to match
            for size in range(self.maximum, 0, -1):
                if index - size < 0:
                    continue
                # inverse the oder of text
                # piece = text[(index - size):index]
                print(piece)
                if piece in self.dictionary:
                    word = piece
                    result.append(word)
                    index -= size
                    break
            # if no matching is find, just decrease the index value by 1
            if word is None:
                index -= 1
		
		# due to inverse matching, we need to inverse back here
        return result[::-1]


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

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

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

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-08-05 17:21:26  更:2021-08-05 17:24:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/7 23:01:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码