TRIE树在输入法分词的应用

2024-06-09 09:58
文章标签 应用 分词 输入法 trie

本文主要是介绍TRIE树在输入法分词的应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

TRIE树,即字典树,可以用于排序、保存大量字符串,在搜索引擎和防火墙中都有着重要的作用。本文使用字典树读取汉语拼音并进行匹配,成功实现了汉语拼音的划分。

先来看看TRIE树的结构:


树从root根节点出发,每个节点都有26个子节点(对应各个字母)。不难发现所有n长度的单词组合都在高度为n的TRIE树中。我们把从root节点出发,到某叶子(或节点)的字母组合称为一个单词。

1.定义以下结构体构造TRIE树:

typedef struct TRIE_NODE_ {struct TRIE_NODE_ *children[26];bool is_word;
} TRIE_NODE;

和我们料想的一样,每个节点都有26个子节点,还有一个标记用于表示root节点到该处是不是一个单词。

2.初始化TRIE树:

static TRIE_NODE* TrieAllocateNode() {TRIE_NODE *ret = (TRIE_NODE*) malloc(sizeof(TRIE_NODE));if (!ret) exit(1);ret->is_word = false;memset(ret->children, 0, 26 * sizeof(TRIE_NODE*));return ret;
}
通用的树的新节点的建立方法,注意将节点属性初始化为false,并把子节点清空。

3.添加新的单词到树中

void TrieAdd(TRIE_NODE *root, char *text) {for (; *text != '\0'; ++text) {                                                   //C中遍历字符串的通用做法if (root->children[(*text) - 'a'] == NULL) {root->children[(*text) - 'a'] = TrieAllocateNode();                     //把字母直接减去A的ASCLL码值,可以将字母与26个子节点一一对应。}root = root->children[(*text) - 'a'];                                       //转向下一层树}root->is_word = true;                                                            //将该单词标为true
}

4.利用深度优先搜索(dfs)分词

void PinyinSolve(TRIE_NODE *root, char *pinyin, char *sp[], int len) {           //字典树root,待处理字符串pinyin,分割点标记数组sp,分割点位置lenchar *p = pinyin;                                                            TRIE_NODE *proc = root;                                                      //获取分割指针p和字典中指针procwhile(true) {if (*p == '\0') break;                                                    //字符串处理结束后返回if (proc->is_word) {                                                      //是单词时继续递归找出合法子结构sp[len] = p;                                                          //每次分割成功都要标记PinyinSolve(root, p, sp, len + 1);                                    //递归,检查每种分割}if (proc->children[(*p) - 'a'] != NULL) {                                 //没到树底就推进指针proc = proc->children[(*p) - 'a'];p++;} else {break;}}if (*p == '\0' && proc->is_word == true) {                                     //到结尾且分割方案可行就打印sp[len] = p;int i;for (i = 0; i <= len; ++i) {char *mb;for (mb = sp[i - 1]; mb < sp[i]; ++mb) {                               //打出分割点间的字符串printf("%c", *mb);}printf("\n");}printf("----\n");                                                           //给下种分割方案留空}
}
典型的DFS的思想,或者DP的思想,递归部分理解有些困难,要注意p的位置和值有效范围,显然展开和回溯过程中p的位置是一样的。

完整代码如下:


//  gcc 下编译通过
//  Copyright (c) 2015年 XiaoJSoft. All rights reserved.
//  字典树词库来自网络
//  In ChestnutHeng's Blog ,If you have any questions ,please contact with <u></u>ChestnutHeng@Gmail.com
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>char *pinyin[] = {"ai","an","ang","ei","ou","ao","ba","bo","bai","bei","bao","ban","ben","bang","beng","bi","bie","biao","bian","bin","bing","pa","po","pai","pao","pou","pan","pen","pang","peng","pi","pie","piao","pian","pin","ping","ma","mo","me","mai","mao","mou","man","men","mang","meng","mi","mie","miao","miu","mian","min","ming","fa","fo","fei","fou","fan","fen","fang","feng","da","tu","de","dai","dei","dao","dou","dan","dang","deng","di","die","diao","diu","dian","ding","ta","te","tai","tao","tou","tan","tang","teng","ti","tie","tiao","tian","ting","na","nai","nei","nao","no","nen","nan","nang","neng","ni","nie","niao","niu","nian","nin","niang","ning","la","le","lai","lei","lao","lou","lan","lang","leng","li","lia","lie","liao","liu","lian","lin","liang","ling","ga","ge","gai","gei","gao","gou","gan","gen","gang","geng","ka","ke","kai","kou","kan","ken","kang","keng","ha","he","hai","hei","hao","hou","hen","hang","heng","ji","jia","jie","jiao","jiu","jian","jin","jiang","jing","qi","qia","qie","qiao","qiu","qian","qin","qiang","qing","xi","xia","xie","xiao","xiu","xian","xin","xiang","xing","zha","zhe","zhi","zhai","zhao","zhou","zhan","zhen","zhang","zheng","cha","che","chi","chai","chou","chan","chen","chang","duo","cheng","sha","she","shi","shai","shao","shou","shan","shen","shang","sheng","re","ri","rao","rou","ran","ren","rang","reng","za","ze","zi","zai","zao","zou","zang","zeng","ca","ce","ci","cai","cao","cou","can","cen","cang","ceng","sa","se","si","sai","sao","sou","san","sen","sang","seng","ya","yao","you","yan","yang","yu","ye","yue","yuan","yi","yin","yun","ying","wa","wo","wai","wei","wan","wen","wang","weng","wu",NULL
};                  //这个字典不全, = =坑爹的,不是我写的啊。typedef struct TRIE_NODE_ {struct TRIE_NODE_ *children[26];bool is_word;
} TRIE_NODE;#define MAX_LEN 128char g_Input[MAX_LEN + 1];static TRIE_NODE* TrieAllocateNode() {TRIE_NODE *ret = (TRIE_NODE*) malloc(sizeof(TRIE_NODE));if (!ret) exit(1);ret->is_word = false;memset(ret->children, 0, 26 * sizeof(TRIE_NODE*));return ret;
}void TrieAdd(TRIE_NODE *root, char *text) {for (; *text != '\0'; ++text) {if (root->children[(*text) - 'a'] == NULL) {root->children[(*text) - 'a'] = TrieAllocateNode();}root = root->children[(*text) - 'a'];}root->is_word = true;
}void PinyinSolve(TRIE_NODE *root, char *pinyin, char *sp[], int len) {char *p = pinyin;TRIE_NODE *proc = root;while(true) {if (*p == '\0') break;if (proc->is_word) {sp[len] = p;PinyinSolve(root, p, sp, len + 1);}if (proc->children[(*p) - 'a'] != NULL) {proc = proc->children[(*p) - 'a'];p++;} else {break;}}if (*p == '\0' && proc->is_word == true) {sp[len] = p;int i;for (i = 0; i <= len; ++i) {char *mb;for (mb = sp[i - 1]; mb < sp[i]; ++mb) {printf("%c", *mb);}printf("\n");}printf("----\n");}
}int main(int argc, const char * argv[]) {TRIE_NODE *root = TrieAllocateNode();char **ptr;for (ptr = pinyin; *ptr != NULL; ++ptr) {TrieAdd(root, *ptr);}scanf("%s", g_Input);char *buffer[999];buffer[0] = g_Input;PinyinSolve(root, g_Input, buffer + 1, 0);printf("OK\n");return 0;
}



这篇关于TRIE树在输入法分词的应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1044858

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

zoj3820(树的直径的应用)

题意:在一颗树上找两个点,使得所有点到选择与其更近的一个点的距离的最大值最小。 思路:如果是选择一个点的话,那么点就是直径的中点。现在考虑两个点的情况,先求树的直径,再把直径最中间的边去掉,再求剩下的两个子树中直径的中点。 代码如下: #include <stdio.h>#include <string.h>#include <algorithm>#include <map>#

【区块链 + 人才服务】可信教育区块链治理系统 | FISCO BCOS应用案例

伴随着区块链技术的不断完善,其在教育信息化中的应用也在持续发展。利用区块链数据共识、不可篡改的特性, 将与教育相关的数据要素在区块链上进行存证确权,在确保数据可信的前提下,促进教育的公平、透明、开放,为教育教学质量提升赋能,实现教育数据的安全共享、高等教育体系的智慧治理。 可信教育区块链治理系统的顶层治理架构由教育部、高校、企业、学生等多方角色共同参与建设、维护,支撑教育资源共享、教学质量评估、

AI行业应用(不定期更新)

ChatPDF 可以让你上传一个 PDF 文件,然后针对这个 PDF 进行小结和提问。你可以把各种各样你要研究的分析报告交给它,快速获取到想要知道的信息。https://www.chatpdf.com/

【区块链 + 人才服务】区块链集成开发平台 | FISCO BCOS应用案例

随着区块链技术的快速发展,越来越多的企业开始将其应用于实际业务中。然而,区块链技术的专业性使得其集成开发成为一项挑战。针对此,广东中创智慧科技有限公司基于国产开源联盟链 FISCO BCOS 推出了区块链集成开发平台。该平台基于区块链技术,提供一套全面的区块链开发工具和开发环境,支持开发者快速开发和部署区块链应用。此外,该平台还可以提供一套全面的区块链开发教程和文档,帮助开发者快速上手区块链开发。

【C++高阶】C++类型转换全攻略:深入理解并高效应用

📝个人主页🌹:Eternity._ ⏩收录专栏⏪:C++ “ 登神长阶 ” 🤡往期回顾🤡:C++ 智能指针 🌹🌹期待您的关注 🌹🌹 ❀C++的类型转换 📒1. C语言中的类型转换📚2. C++强制类型转换⛰️static_cast🌞reinterpret_cast⭐const_cast🍁dynamic_cast 📜3. C++强制类型转换的原因📝

韦季李输入法_输入法和鼠标的深度融合

在数字化输入的新纪元,传统键盘输入方式正悄然进化。以往,面对实体键盘,我们常需目光游离于屏幕与键盘之间,以确认指尖下的精准位置。而屏幕键盘虽直观可见,却常因占据屏幕空间,迫使我们在操作与视野间做出妥协,频繁调整布局以兼顾输入与界面浏览。 幸而,韦季李输入法的横空出世,彻底颠覆了这一现状。它不仅对输入界面进行了革命性的重构,更巧妙地将鼠标这一传统外设融入其中,开创了一种前所未有的交互体验。 想象