typecho前端建站工具,百度官网首页登陆,虚拟云电脑免费,东莞企网站建设1、完全切分法、正向最大匹配算法、逆向最大匹配算法和双向最大匹配算法
一、实验内容
一个好的NLP系统一定要有完备的词典#xff0c;用于判断算法分出的词是否是具有实际意义的词。自定义一个词典#xff0c;比如dic [项目, 研究, 目的用于判断算法分出的词是否是具有实际意义的词。自定义一个词典比如dic [项目, 研究, 目的, 商品, 服务, 和服, 和尚, 尚未, 生命, 起源, 当下, 雨天, 地面, 积水, 下雨天, 欢迎, 老师, 生前, 就餐, 迎新, 师生, 前来]。实现相关的分词方法完全切分、正向最长匹配、逆向最长匹配、双向最长匹配算法并输入一些句子验证分词结果的正确性
二、实现步奏
一新建Python工程NLPExp01
1.打开编辑器新建NLPExp01工程 2.打开对应工程在对应目录下新建python文件exp01.py
二定义字典内容实现不同的分词模式
1.字典内容可自定义如 [项目, 研究, 目的, 商品, 服务, 和服, 和尚, 尚未, 生命, 起源, 当下, 雨天, 地面, 积水, 下雨天, 欢迎, 老师, 生前, 就餐, 迎新, 师生, 前来]
2.实现对应的分词匹配算法完全切分法、正向最大匹配算法、逆向最大匹配算法和双向最大匹配算法
3.输入验证字符检查分词结果至少输入“和尚尚未结婚”、“中外科学研究”、“商品和服务”“研究生命起源”“当下雨天地面积水”“结婚的和尚未结婚的”“欢迎新老师生前来就餐”,检查对应的分词结果
三、实现代码
#my_dic为自定义字典内容可更改
my_dic [项目, 研究, 目的, 商品, 服务, 和服, 和尚, 尚未, 生命, 起源, 当下, 雨天, 地面, 积水, 下雨天, 欢迎, 老师, 生前, 就餐, 迎新, 师生, 前来];#在双向匹配中调用用于比较列表元素位置
def select_word(text):result 0for i in text:if (len(i) 1):result 1return result#在main函数中调用将输入数据初始化为列表数据
def fully_segment(text, dic):word_list []for i in range(len(text)):for j in range(i 1, len(text) 1):word text[i:j]if word in dic:word_list.append(word)return word_list#正向最大匹配
def positive_max_match(text, dict):word_list []i 0while(ilen(text)):longest_word text[i]for j in range(i1, len(text) 1):#从字典里开始找词如果找到先记录如果有更长的保存直到遍历结束word text[i:j]if word in dict:if len(word) len(longest_word):longest_word wordword_list.append(longest_word)i len(longest_word)return word_list#逆向最大匹配
def backward_segment(text, dict):word_list []i len(text) - 1while (i 0):longest_word text[i]for j in range(0, i):word text[j:i 1]if word in dict:if len(word) len(longest_word):longest_word wordword_list.insert(0, longest_word)i - len(longest_word)return word_list#双向最大匹配
def all_segment(text, dic):list_forward positive_max_match(text, dic)list_backward backward_segment(text, dic)list_final []if (len(list_forward) len(list_backward)):list_final list_backward[:]elif (len(list_forward) len(list_backward)):list_final list_forward[:]else:if (select_word(list_forward) select_word(list_backward)):list_final list_backward[:]elif (select_word(list_forward) select_word(list_backward)):list_final list_forward[:]else:list_final list_backward[:]return list_finalif __name__ __main__:#使用自定义词库进行分词练习while (1):a input(请输入你要分词的句子输入0结束输入)if (a 0):print(输入结束)breakb fully_segment(a, my_dic)print(分词的结果, b)list_forward positive_max_match(a, my_dic)list_backward backward_segment(a, my_dic)list_all all_segment(a, my_dic)print(正向最长匹配, list_forward)print(逆向最长匹配, list_backward)print(双向最长匹配, list_all) 2、使用jieba库进行中文分词、词组特性标注、关键词提取的相关方法
一、实验内容
安装jieba分词库并调用其中的分词、词组特性标注、关键词提取的相关方法查看对应的功能。
二、实现步奏
1.安装jieba工具包pip install jieba
2.调用jieba分词功能 seq_list jieba.cut(“中外科学研究”,cut_allTrue) print(“全模式”“/”.join(seq_list)) seq_list jieba.cut(“中外科学研究”,cut_allFalse) print(“精确模式”“/”.join(seq_list)) seq_list jieba.cut_for_search(“中外科学研究”) print(“全模式”“/”.join(seq_list)) 三、实现代码
if __name__ __main__:# 使用jieba词库进行不同模式的分词词性标注方法和关键字的提取seq_list jieba.cut(中外科学研究, cut_allTrue)print(全模式: /.join(seq_list))seq_list jieba.cut(中外科学研究, cut_allFalse)print(精确模式: /.join(seq_list))seq_list jieba.cut_for_search(中外科学研究)print(搜索模式 /.join(seq_list))
二、实现步奏
1.调用词性标注功能 import jieba.posseg as psg text”去北京大学学习” seg psg.cut(text) for ele in seg: print(ele) 三、代码实现
import jieba.posseg as psgif __name__ __main__:# 使用jieba词库进行不同模式的分词词性标注方法和关键字的提取text 去北京大学学习seg psg.cut(text)for ele in seg:print(ele) 文章转载自: http://www.morning.jqzns.cn.gov.cn.jqzns.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.mkrqh.cn.gov.cn.mkrqh.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.pqkgb.cn.gov.cn.pqkgb.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.znqxt.cn.gov.cn.znqxt.cn http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.yrkdq.cn.gov.cn.yrkdq.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.ljcjc.cn.gov.cn.ljcjc.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.rkrl.cn.gov.cn.rkrl.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.rfrx.cn.gov.cn.rfrx.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.rwjfs.cn.gov.cn.rwjfs.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.lhwlp.cn.gov.cn.lhwlp.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.trsmb.cn.gov.cn.trsmb.cn http://www.morning.jjzxn.cn.gov.cn.jjzxn.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn