实验目的:

利用给定的中英文文本序列(见 Chinese.txt 和 English.txt),分别利用以下给定的中

英文分词工具进行分词并对不同分词工具产生的结果进行简要对比分析。

实验工具:

中文 Jieba(重点),尝试三种分词模式与自定义词典功能、SnowNLP、THULAC、NLPIR、StanfordCoreNLP、

英文 NLTK、SpaCy、StanfordCoreNLP

实验环境:

语言:Python 3.7.0

IDE: Pycharm

需要使用 pip 安装很多包,这里请大家去搜索相关教程安装

实验步骤:

首先进行中文分词:

一、jieba

import jieba
import re
Chinese=‘央视315晚会曝光湖北省知名的神丹牌、莲田牌“土鸡蛋”实为普通鸡蛋冒充,同时在商标上玩猫腻,分别注册“鲜土”、注册“好土”商标,让消费者误以为是“土鸡蛋”。3月15日晚间,新京报记者就此事致电湖北神丹健康食品有限公司方面,其工作人员表示不知情,需要了解清楚情况,截至发稿暂未取得最新回应。新京报记者还查询发现,湖北神丹健康食品有限公司为农业产业化国家重点龙头企业、高新技术企业,此前曾因涉嫌虚假宣传“中国最大的蛋品企业”而被罚6万元。’str=re.sub('[^\w]','',chinese)   #使用正则去符号,之后都是用这个str字符串seg_list=jieba.cut(s_list, cut_all=False) #精确模式
print('/'.join(seg_list))

分词的结果:

央视/315/晚会/曝光/湖北省/知名/的/神丹/牌莲田牌/土/鸡蛋/实为/普通/鸡蛋/冒充/同时/在/商标/上/玩/猫腻/分别/注册/鲜土/注册/好土/商标/让/消费者/误以为/是/土/鸡蛋/3/月/15/日/晚间/新/京报/记者/就/此事/致电/湖北/神丹/健康/食品/有限公司/方面/其/工作人员/表示/不知情/需要/了解/清楚/情况/截至/发稿/暂未/取得/最新/回应/新/京报/记者/还/查询/发现/湖北/神丹/健康/食品/有限公司/为/农业/产业化/国家/重点/龙头企业/高新技术/企业/此前/曾/因涉嫌/虚假/宣传/中国/最大/的/蛋品/企业/而/被/罚/6/万元

载入自创建的词典

使用 jieba.load_userdict(file)

file=open(dict,'r')   # 载入一个词典,这个词典的内容为:神丹牌、莲花牌、土鸡蛋、新京报
jieba.load_userdict(file)
file.close()
seg_list=jieba.cut(str, cut_all=False) #精确模式  str 为之前的字符串
print('/'.join(seg_list))

结果:

央视/315/晚会/曝光/湖北省/知名/的/神丹牌/莲田牌/土鸡蛋/实为/普通/鸡蛋/冒充/同时/在/商标/上/玩/猫腻/分别/注册/鲜土/注册/好土/商标/让/消费者/误以为/是/土鸡蛋/3/月/15/日/晚间/新京报/记者/就/此事/致电/湖北/神丹/健康/食品/有限公司/方面/其/工作人员/表示/不知情/需要/了解/清楚/情况/截至/发稿/暂未/取得/最新/回应/新京报/记者/还/查询/发现/湖北/神丹/健康/食品/有限公司/为/农业/产业化/国家/重点/龙头企业/高新技术/企业/此前/曾/因涉嫌/虚假/宣传/中国/最大/的/蛋品/企业/而/被/罚/6/万元

显然载入词典之后,神丹牌、莲花牌、土鸡蛋、新京报合在了一起

二、SnowNlp

from snownlp import SnowNLP
s=SnowNLP(str)   #str为之前去掉符号的中文字符串
print(s.words)   #进行分词
print(s.pinyin)  #得到拼音
print(s.summary(3)) #进行总结 summary
print(s.keywords(3)) # 得到关键词
print(s.han)  #把繁体字变成简体字

分词的结果:是一个列表

['央视', '315', '晚会', '曝光', '湖北省', '知名', '的', '神丹', '牌', '莲', '田', '牌', '土', '鸡蛋', '实', '为', '普通', '鸡蛋', '冒充', '同时', '在', '商标', '上', '玩猫', '腻', '分别', '注册', '鲜', '土', '注册', '好', '土', '商标', '让', '消费者', '误', '以为', '是', '土', '鸡蛋', '3', '月', '15', '日', '晚间', '新京', '报', '记者', '就', '此事', '致电', '湖北', '神', '丹', '健康', '食品', '有限公司', '方面', '其', '工作', '人员', '表示', '不', '知情', '需要', '了解', '清楚', '情况', '截至', '发稿', '暂', '未', '取得', '最新', '回应', '新京', '报', '记者', '还', '查询', '发现', '湖北', '神', '丹', '健康', '食品', '有限公司', '为', '农业', '产业化', '国家', '重点', '龙头', '企业', '高新技术', '企业', '此前', '曾', '因', '涉嫌', '虚假', '宣传', '中国', '最', '大', '的', '蛋品', '企业', '而', '被', '罚', '6', '万', '元']

三、Thulac

t=thulac.thulac()  #进行分词和标注词性
text=t.cut(str,text=False) #进行分词和标注词性,若text=True 则为 str,否则为默认模式返回值为list
print(text)

[['央视', 'v'], ['315', 'm'], ['晚会', 'n'], ['曝光', 'v'], ['湖北省', 'ns'], ['知名', 'a'], ['的', 'u'], ['神丹牌', 'nz'], ['莲田牌', 'nz'], ['土鸡蛋', 'n'], ['实', 'a'], ['为', 'v'], ['普通', 'a'], ['鸡蛋', 'n'], ['冒充', 'v'], ['同时', 'd'], ['在', 'p'], ['商标', 'n'], ['上', 'f'], ['玩', 'v'], ['猫腻', 'n'], ['分别', 'd'], ['注册', 'v'], ['鲜土', 'n'], ['注册', 'v'], ['好', 'a'], ['土', 'n'], ['商标', 'n'], ['让', 'v'], ['消费者', 'n'], ['误', 'd'], ['以为', 'v'], ['是', 'v'], ['土鸡蛋', 'n'], ['3月', 't'], ['15日', 't'], ['晚间', 't'], ['新', 'a'], ['京报', 'n'], ['记者', 'n'], ['就', 'p'], ['此事', 'r'], ['致电', 'v'], ['湖北', 'ns'], ['神丹', 'nz'], ['健康', 'a'], ['食品', 'n'], ['有限公司', 'n'], ['方面', 'n'], ['其', 'r'], ['工作', 'v'], ['人员', 'n'], ['表示', 'v'], ['不', 'd'], ['知情', 'v'], ['需要', 'v'], ['了', 'u'], ['解', 'v'], ['清楚', 'a'], ['情况', 'n'], ['截至', 'v'], ['发稿', 'v'], ['暂', 'd'], ['未', 'd'], ['取得', 'v'], ['最新', 'a'], ['回应', 'v'], ['新', 'a'], ['京报', 'n'], ['记者', 'n'], ['还', 'd'], ['查询', 'v'], ['发现', 'v'], ['湖北', 'ns'], ['神丹', 'nz'], ['健康', 'a'], ['食品', 'n'], ['有限公司', 'n'], ['为', 'p'], ['农业', 'n'], ['产业化', 'v'], ['国', 'm'], ['家', 'q'], ['重点', 'n'], ['龙头', 'n'], ['企业', 'n'], ['高新技术', 'n'], ['企业', 'n'], ['此前', 't'], ['曾', 'd'], ['因', 'p'], ['涉嫌', 'v'], ['虚假', 'a'], ['宣传', 'v'], ['中国', 'ns'], ['最', 'd'], ['大', 'a'], ['的', 'u'], ['蛋品', 'n'], ['企业', 'n'], ['而', 'c'], ['被', 'p'], ['罚', 'v'], ['6万', 'm'], ['元', 'q']]

t2=thulac.thulac(seg_only=True)  #只进行分词 segment

则只进行分词,不标注词性

四、Pynlpir

pynlpir.open()
print(pynlpir.segment(str)) #分词

[('央', 'verb'), ('视', 'verb'), ('315', 'numeral'), ('晚会', 'noun'), ('曝光', 'verb'), ('湖北省', 'noun'), ('知名', 'adjective'), ('的', 'particle'), ('神', 'noun'), ('丹', 'distinguishing word'), ('牌', 'noun'), ('、', 'punctuation mark'), ('莲', 'noun'), ('田', 'noun'), ('牌', 'noun'), ('“', 'punctuation mark'), ('土', 'noun'), ('鸡蛋', 'noun'), ('”', 'punctuation mark'), ('实', 'adjective'), ('为', 'verb'), ('普通', 'adjective'), ('鸡蛋', 'noun'), ('冒充', 'verb'), (',', 'punctuation mark'), ('同时', 'conjunction'), ('在', 'preposition'), ('商标', 'noun'), ('上', 'noun of locality'), ('玩', 'verb'), ('猫腻', 'noun'), (',', 'punctuation mark'), ('分别', 'adverb'), ('注册', 'verb'), ('“', 'punctuation mark'), ('鲜', 'adjective'), ('土', 'noun'), ('”', 'punctuation mark'), ('、', 'punctuation mark'), ('注册', 'verb'), ('“', 'punctuation mark'), ('好', 'adjective'), ('土', 'noun'), ('”', 'punctuation mark'), ('商标', 'noun'), (',', 'punctuation mark'), ('让', 'verb'), ('消费者', 'noun'), ('误', 'adverb'), ('以为', 'verb'), ('是', 'verb'), ('“', 'punctuation mark'), ('土', 'noun'), ('鸡蛋', 'noun'), ('”', 'punctuation mark'), ('。', 'punctuation mark'), ('3月', 'time word'), ('15日', 'time word'), ('晚间', 'time word'), (',', 'punctuation mark'), ('新京报', None), ('记者', 'noun'), ('就', 'adverb'), ('此事', 'pronoun'), ('致电', 'verb'), ('湖北', 'noun'), ('神', 'noun'), ('丹', 'distinguishing word'), ('健康', 'adjective'), ('食品', 'noun'), ('有限公司', 'noun'), ('方面', 'noun'), (',', 'punctuation mark'), ('其', 'pronoun'), ('工作', 'verb'), ('人员', 'noun'), ('表示', 'verb'), ('不', 'adverb'), ('知', 'verb'), ('情', 'noun'), (',', 'punctuation mark'), ('需要', 'verb'), ('了解', 'verb'), ('清楚', 'adjective'), ('情况', 'noun'), (',', 'punctuation mark'), ('截至', 'verb'), ('发稿', 'verb'), ('暂', 'adverb'), ('未', 'adverb'), ('取得', 'verb'), ('最新', 'adjective'), ('回应', 'verb'), ('。', 'punctuation mark'), ('新京报', None), ('记者', 'noun'), ('还', 'adverb'), ('查询', 'verb'), ('发现', 'verb'), (',', 'punctuation mark'), ('湖北', 'noun'), ('神', 'noun'), ('丹', 'distinguishing word'), ('健康', 'adjective'), ('食品', 'noun'), ('有限公司', 'noun'), ('为', 'preposition'), ('农业', 'noun'), ('产业化', 'verb'), ('国家', 'noun'), ('重点', 'noun'), ('龙头', 'noun'), ('企业', 'noun'), ('、', 'punctuation mark'), ('高新技术', 'noun'), ('企业', 'noun'), (',', 'punctuation mark'), ('此前', 'time word'), ('曾', 'adverb'), ('因', 'preposition'), ('涉嫌', 'verb'), ('虚假', 'adjective'), ('宣传', 'verb'), ('“', 'punctuation mark'), ('中国', 'noun'), ('最', 'adverb'), ('大', 'adjective'), ('的', 'particle'), ('蛋品', 'noun'), ('企业', 'noun'), ('”', 'punctuation mark'), ('而', 'conjunction'), ('被', 'preposition'), ('罚', 'verb'), ('6万', 'numeral'), ('元', 'classifier'), ('。', 'punctuation mark')]

五、StanfordCoreNLP:

nlp=StanfordCoreNLP(r'G:\\stanford-corenlp-full-2018-10-05\\stanford-corenlp-full-2018-10-05',lang='zh')
print(nlp.word_tokenize(s_list)) #返回一个列表
# print(nlp.pos_tag(str))  #词性标注
# print(nlp.parse(str))  #解析

结果:

['央视', '315', '晚会', '曝光', '湖北省', '知名', '的', '神丹', '牌', '莲', '田', '牌', '土', '鸡蛋', '实为', '普通', '鸡蛋', '冒充', '同时', '在', '商标', '上', '玩', '猫腻', '分别', '注册', '鲜土', '注册', '好', '土', '商标', '让', '消费者', '误以为', '是', '土', '鸡蛋', '3月', '15日', '晚间', '新京报', '记者', '就此事', '致电', '湖北', '神丹', '健康', '食品', '有限', '公司', '方面', '其', '工作', '人员', '表示', '不知情', '需要', '了解', '清楚', '情况', '截至', '发稿', '暂', '未', '取得', '最新', '回应', '新京报', '记者', '还', '查询', '发现', '湖北', '神丹', '健康', '食品', '有限', '公司', '为', '农业', '产业化', '国家', '重点', '龙头', '企业', '高', '新', '技术', '企业', '此前', '曾', '因', '涉嫌', '虚假', '宣传', '中国', '最', '大', '的', '蛋品', '企业', '而', '被', '罚', '6万', '元']

进行英文分词:

Englisth=‘Trump was born and raised in the New York City borough of Queens and received an economics degree from the Wharton School. He was appointed president of his family's real estate business in 1971, renamed it The Trump Organization, and expanded it from Queens and Brooklyn into Manhattan. The company built or renovated skyscrapers, hotels, casinos, and golf courses. Trump later started various side ventures, including licensing his name for real estate and consumer products. He managed the company until his 2017 inauguration. He co-authored several books, including The Art of the Deal. He owned the Miss Universe and Miss USA beauty pageants from 1996 to 2015, and he produced and hosted The Apprentice, a reality television show, from 2003 to 2015. Forbes estimates his net worth to be $3.1 billion.’

六、nltk:

import nltk
import re
english='H:\\自然语言处理\\Experiment2\\English.txt'
with open(english,'r',encoding='utf-8') as file:u=file.read()
str=re.sub('[^\w ]','',u)
print(nltk.word_tokenize(str))
print(nltk.pos_tag(nltk.word_tokenize(str))) #对分完词的结果进行词性标注

结果:

['Trump', 'was', 'born', 'and', 'raised', 'in', 'the', 'New', 'York', 'City', 'borough', 'of', 'Queens', 'and', 'received', 'an', 'economics', 'degree', 'from', 'the', 'Wharton', 'School', 'He', 'was', 'appointed', 'president', 'of', 'his', 'familys', 'real', 'estate', 'business', 'in', '1971', 'renamed', 'it', 'The', 'Trump', 'Organization', 'and', 'expanded', 'it', 'from', 'Queens', 'and', 'Brooklyn', 'into', 'Manhattan', 'The', 'company', 'built', 'or', 'renovated', 'skyscrapers', 'hotels', 'casinos', 'and', 'golf', 'courses', 'Trump', 'later', 'started', 'various', 'side', 'ventures', 'including', 'licensing', 'his', 'name', 'for', 'real', 'estate', 'and', 'consumer', 'products', 'He', 'managed', 'the', 'company', 'until', 'his', '2017', 'inauguration', 'He', 'coauthored', 'several', 'books', 'including', 'The', 'Art', 'of', 'the', 'Deal', 'He', 'owned', 'the', 'Miss', 'Universe', 'and', 'Miss', 'USA', 'beauty', 'pageants', 'from', '1996', 'to', '2015', 'and', 'he', 'produced', 'and', 'hosted', 'The', 'Apprentice', 'a', 'reality', 'television', 'show', 'from', '2003', 'to', '2015', 'Forbes', 'estimates', 'his', 'net', 'worth', 'to', 'be', '31', 'billion']

七、spacy:

import spacy
nlp=spacy.load('en_core_web_sm')
document=nlp(str)
print(document.text.split())

结果:

['Trump', 'was', 'born', 'and', 'raised', 'in', 'the', 'New', 'York', 'City', 'borough', 'of', 'Queens', 'and', 'received', 'an', 'economics', 'degree', 'from', 'the', 'Wharton', 'School', 'He', 'was', 'appointed', 'president', 'of', 'his', 'familys', 'real', 'estate', 'business', 'in', '1971', 'renamed', 'it', 'The', 'Trump', 'Organization', 'and', 'expanded', 'it', 'from', 'Queens', 'and', 'Brooklyn', 'into', 'Manhattan', 'The', 'company', 'built', 'or', 'renovated', 'skyscrapers', 'hotels', 'casinos', 'and', 'golf', 'courses', 'Trump', 'later', 'started', 'various', 'side', 'ventures', 'including', 'licensing', 'his', 'name', 'for', 'real', 'estate', 'and', 'consumer', 'products', 'He', 'managed', 'the', 'company', 'until', 'his', '2017', 'inauguration', 'He', 'coauthored', 'several', 'books', 'including', 'The', 'Art', 'of', 'the', 'Deal', 'He', 'owned', 'the', 'Miss', 'Universe', 'and', 'Miss', 'USA', 'beauty', 'pageants', 'from', '1996', 'to', '2015', 'and', 'he', 'produced', 'and', 'hosted', 'The', 'Apprentice', 'a', 'reality', 'television', 'show', 'from', '2003', 'to', '2015', 'Forbes', 'estimates', 'his', 'net', 'worth', 'to', 'be', '31', 'billion']

八、StanfordcoreNLP:

nlp=StanfordCoreNLP(r'G:\\stanford-corenlp-full-2018-10-05\\stanford-corenlp-full-2018-10-05',lang='en')
print(nlp.word_tokenize(str))

结果;

['Trump', 'was', 'born', 'and', 'raised', 'in', 'the', 'New', 'York', 'City', 'borough', 'of', 'Queens', 'and', 'received', 'an', 'economics', 'degree', 'from', 'the', 'Wharton', 'School', 'He', 'was', 'appointed', 'president', 'of', 'his', 'familys', 'real', 'estate', 'business', 'in', '1971', 'renamed', 'it', 'The', 'Trump', 'Organization', 'and', 'expanded', 'it', 'from', 'Queens', 'and', 'Brooklyn', 'into', 'Manhattan', 'The', 'company', 'built', 'or', 'renovated', 'skyscrapers', 'hotels', 'casinos', 'and', 'golf', 'courses', 'Trump', 'later', 'started', 'various', 'side', 'ventures', 'including', 'licensing', 'his', 'name', 'for', 'real', 'estate', 'and', 'consumer', 'products', 'He', 'managed', 'the', 'company', 'until', 'his', '2017', 'inauguration', 'He', 'coauthored', 'several', 'books', 'including', 'The', 'Art', 'of', 'the', 'Deal', 'He', 'owned', 'the', 'Miss', 'Universe', 'and', 'Miss', 'USA', 'beauty', 'pageants', 'from', '1996', 'to', '2015', 'and', 'he', 'produced', 'and', 'hosted', 'The', 'Apprentice', 'a', 'reality', 'television', 'show', 'from', '2003', 'to', '2015', 'Forbes', 'estimates', 'his', 'net', 'worth', 'to', 'be', '31', 'billion']

以上就是八种分词工具的分词过程,我建议:中文分词使用 jieba进行分词,英文使用 NLTK进行分词。

Jieba、NLTK等中英文分词工具进行分词相关推荐

  1. jieba nltk 进行中英文分词

    Jieba.NLTK等中英文分词工具进行分词 建议:中文分词使用 jieba(SnowNlp.THULAC.NLPIR.StanfordCoreNLP)进行分词,英文使用 NLTK进行分词:还有git ...

  2. 自然语言处理学习笔记2:分词工具及分词原理

    中文分词(Chinese Word Segmentation) 指的是将一个汉字序列切分成一个一个单独的词.分词就是将连续的字序列按照一定的规范重新组合成词序列的过程.我们知道,在英文的行文中,单词之 ...

  3. 分词工具 java_IK分词工具的使用(java)

    说明 到目前为止,就中文分词工具和中文分词方法而言,分词模型大概有10几个种类,效果较好的并不多,HMM,MMSEG之类的,当然还有深度学习的模型,paddle的模型比较好.按工具名说的话有中科院的n ...

  4. jieba textrank关键词提取 python_五款中文分词工具在线PK: Jieba, SnowNLP, PkuSeg,THULAC, HanLP...

    最近玩公众号会话停不下来:玩转腾讯词向量:Game of Words(词语的加减游戏),准备把NLP相关的模块搬到线上,准确的说,搬到AINLP公众号后台对话,所以,趁着劳动节假期,给AINLP公众号 ...

  5. python 分词工具对比_五款中文分词工具在线PK: Jieba, SnowNLP, PkuSeg, THULAC, HanLP

    最近玩公众号会话停不下来: 玩转腾讯词向量:Game of Words(词语的加减游戏) ,准备把NLP相关的模块搬到线上,准确的说,搬到AINLP公众号后台对话,所以,趁着劳动节假期,给AINLP公 ...

  6. 自然语言处理系列十七》中文分词》分词工具实战》Python的Jieba分词

    注:此文章内容均节选自充电了么创始人,CEO兼CTO陈敬雷老师的新书<分布式机器学习实战>(人工智能科学与技术丛书)[陈敬雷编著][清华大学出版社] 文章目录 自然语言处理系列十七 分词工 ...

  7. 数据处理之文本分词、MMSEG分词工具、斯坦福NLTK分词工具

    一. 文本分词概述 二. 中文分词算法分类 三. MMSEG分词工具 四. 斯坦福NLTK分词工具 1 CRF方法实例 一. 文本分词概述 文本分词是将字符串文本划分为有意义的单位的过程,如词语.句子 ...

  8. 详细介绍NLP中文分词原理及分词工具

    基于词表的分词方法 正向最大匹配算法FMM 从左到右扫描文本,得到词的最大匹配. 案例分析: 用正向最大匹配法对"秦皇岛今天晴空万里"进行中文分词,见下表. 词典 :"秦 ...

  9. 各种中文分词工具的使用方法

    诸神缄默不语-个人CSDN博文目录 本文将介绍jieba.HanLP.LAC.THULAC.NLPIR.spacy.stanfordcorenlp.pkuseg等多种中文分词工具的简单使用方法. 对于 ...

最新文章

  1. 1行代码搞定Latex公式编写,这个4.6M的Python小插件,堪称论文必备神器
  2. P4887 【模板】莫队二次离线(第十四分块(前体))
  3. JQuery图片切换 Win8 Metro风格Banner
  4. 如何配置php的ip地址吗,linux如何配置IP
  5. SRAM与SDRAM的区别
  6. matlab 轴承 压力分布 绘图,数值计算绘制动压轴承压力场分布图
  7. 回归标准差和残差平方和的关系_ISLR实验:简单线性回归
  8. lucene bug的报告经历
  9. Bug管理 Mantis(配置详细)
  10. JavaEE学习14(应用)--用户自动登陆
  11. StarUML接口视图修改为类的形式
  12. 跟我学制作javaEE网上书店销售管理系统(沙箱支付)springboot+vue
  13. 基于北京二手房价数据的探索性数据分析和房价评估——房价评估模型构建
  14. RFID定位技术下的资产管理与应急仓储物流--RFID资产管理--新导智能
  15. Android通过第三方软件打开Word、Excel、PPT、PDF等文档
  16. 如何将linux编译过程中的警告及错误信息输出到文件中
  17. 一文看完计算机基础知识总结
  18. win10系统计算机搜索文件,win10搜索文件内容的方法是什么_win10精确搜索文件内容的方法...
  19. 三大web服务器对比(lighttpd,apache,nginx)
  20. 怎么用手机拍摄制作视频

热门文章

  1. SQL Server代理(已禁用代理XP) 出现的原因以及解决方法【通俗易懂,简洁明了】
  2. 冯 诺依曼体系结构是现代计算机的基础,作为现代计算机基本结构的冯·诺依曼体系包括______。...
  3. error C1083:无法打开文件stddef.h或crtd bg.h
  4. 将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005
  5. 微信企业号用户验证php,身份验证
  6. Android适配全面屏
  7. 让微信二维码扫描你的APK
  8. 华硕B460m plus+Intel 10400+AMD 5500xt黑苹果EFI引导文件
  9. mysql连接池耗尽_定时任务和jdbc连接池的耗尽问题
  10. 「Adobe国际认证」关于Adobe Photoshop,创建和修改画笔教程?