作业任务:

使用98年人民日报语料库进行中文分词训练及测试。

作业输入:

98年人民日报语料库(1998-01-105-带音.txt),用80%的数据作为训练集,20%的数据作为验证集。

运行环境:

Jupyter Notebook, Python3

作业方法:

实现了前向匹配算法的分词功能。

源码地址:

https://github.com/YanqiangWang/NLP-Summer-Course

作业步骤:

1.处理语料库: 删除段前标号,以及词性标注。

# 读取原始语料文件
in_path = '1998-01-105-带音.txt'
file = open(in_path, encoding='gbk')
in_data = file.readlines()
# 预处理后的语料库
curpus_path = 'curpus.txt'
curpusfile = open(curpus_path, 'w', encoding='utf-8')
#删除段前标号,[],{},词性标注(最短匹配)
for sentence in in_data:
words = sentence.strip().split(' ')
words.pop(0) for word in words:
if word.strip() != '':
if word.startswith('['):
word = word[1:]
elif ']' in word:
word = word[0:word.index(']')] if '{' in word:
word = word[0:word.index('{')] w_c = word.split('/')
# 生成语料库
curpusfile.write(w_c[0] + ' ') curpusfile.write('\n')

2.随机划分训练集80%和验证集20%。

from sklearn.model_selection import train_test_split

# 随机划分
curpus = open(curpus_path, encoding='utf-8').readlines()
train_data, test_data = train_test_split(
curpus, test_size=0.2, random_state=10)
# 查看划分后的数据大小
print(len(curpus))
print(len(train_data) / len(curpus))
print(len(test_data) / len(curpus))
22787
0.7999736691973494
0.20002633080265064

3.前向匹配算法FMM的实现。

# 生成词典
from tqdm import tqdm_notebook dic = [] for sentence in tqdm_notebook(train_data):
words = sentence.strip().split(' ')
for word in words:
if word.strip() != '':
if word not in dic:
dic.append(word)
# 设置单词最大长度
max_dic_len = 5
# 生成分词测试文本
test_text = []
for sentence in test_data:
words = sentence.strip().split(' ')
test_text.append(''.join(words))
# 保存验证集
test_path = 'test.txt'
testfile = open(test_path, 'w', encoding='utf-8')
for sentence in test_data:
testfile.write(sentence)
# 保存分词结果
result_path = 'result.txt'
resultfile = open(result_path, 'w', encoding='utf-8')
# 前向匹配
for sentence in tqdm_notebook(test_text):
sent = sentence
words = []
max_len = max_dic_len
while(len(sent) > 0):
word_len = max_len
for i in range(0, max_len):
word = sent[0:word_len]
if word_len == 1 or word in dic:
sent = sent[word_len:]
words.append(word)
word = []
break
else:
word_len -= 1
word = []
resultfile.write(' '.join(words) + '\n')

性能评价

查准率,查全率,F度量

Precision = (Number of words correctly segmented) / (Number of words segmented) * 100%

Recall = (Number of words correctly segmented) / (Number of words in the reference) * 100%

F measure = 2 * P * R / (P + R)

def get_word(path):
f = open(path, 'r', encoding='utf-8')
lines = f.readlines()
return lines result_lines = get_word(result_path)
test_lines = get_word(test_path) list_num = len(test_lines) if len(test_lines) < len(result_lines) else len(result_lines)
right_num = 0
result_cnt = 0
test_cnt = 0 for i in tqdm_notebook(range(list_num)):
result_sent = list(result_lines[i].split())
test_sent = list(test_lines[i].split()) result_cnt += len(result_sent)
test_cnt += len(test_sent) str_result = ''
str_test = '' i_result = 0
i_test = 0 while i_result < len(result_sent) and i_test < len(test_sent):
word_result = result_sent[i_result]
word_test = test_sent[i_test] str_result += word_result
str_test += word_test if word_result == word_test:
right_num += 1
i_result += 1
i_test += 1 else:
while len(str_result) > len(str_test):
i_test += 1
if i_test >= len(test_sent):
break
str_test += test_sent[i_test] while len(str_result) < len(str_test):
i_result += 1
if i_result >= len(result_sent):
break
str_result += result_sent[i_result] i_test += 1
i_result += 1
print("生成结果词的个数:", result_cnt)
print("验证集结果词个数:", test_cnt) p = right_num / result_cnt
r = right_num / test_cnt
f = 2 * p * r / (p + r) print("查准率:", p)
print("查全率:", r)
print("F度量:", f)
生成结果词的个数: 227640
验证集结果词个数: 219680
查准率: 0.8301748374626603
查全率: 0.8602558266569555
F度量: 0.8449476884556917

【NLP】暑假课作业1 - 中文分词(前向匹配算法实现)的更多相关文章

  1. 【NLP】暑假课作业3 - 词性标注(简单词频概率统计)

    作业任务: 使用98年人民日报语料库进行词性标注训练及测试. 作业输入: 98年人民日报语料库(1998-01-105-带音.txt),用80%的数据作为训练集,20%的数据作为验证集. 运行环境: ...

  2. 开源中文分词工具探析(五):Stanford CoreNLP

    CoreNLP是由斯坦福大学开源的一套Java NLP工具,提供诸如:词性标注(part-of-speech (POS) tagger).命名实体识别(named entity recognizer ...

  3. 开源中文分词工具探析(六):Stanford CoreNLP

    CoreNLP是由斯坦福大学开源的一套Java NLP工具,提供诸如:词性标注(part-of-speech (POS) tagger).命名实体识别(named entity recognizer ...

  4. NLP+词法系列(二)︱中文分词技术简述、深度学习分词实践(CIPS2016、超多案例)

    摘录自:CIPS2016 中文信息处理报告<第一章 词法和句法分析研究进展.现状及趋势>P4 CIPS2016 中文信息处理报告下载链接:http://cips-upload.bj.bce ...

  5. NLP+词法系列(一)︱中文分词技术小结、几大分词引擎的介绍与比较

    笔者想说:觉得英文与中文分词有很大的区别,毕竟中文的表达方式跟英语有很大区别,而且语言组合形式丰富,如果把国外的内容强行搬过来用,不一样是最好的.所以这边看到有几家大牛都在中文分词以及NLP上越走越远 ...

  6. NLP自然语言处理 jieba中文分词,关键词提取,词性标注,并行分词,起止位置,文本挖掘,NLP WordEmbedding的概念和实现

    1. NLP 走近自然语言处理 概念 Natural Language Processing/Understanding,自然语言处理/理解 日常对话.办公写作.上网浏览 希望机器能像人一样去理解,以 ...

  7. NLP & 中文分词

    NLP & 中文分词 中文分词 (Word Segmentation, WS) 指的是将汉字序列切分成词序列. 中文自然语言处理系统 https://www.ltp-cloud.com/int ...

  8. NLP系列-中文分词(基于统计)

    上文已经介绍了基于词典的中文分词,现在让我们来看一下基于统计的中文分词. 统计分词: 统计分词的主要思想是把每个词看做是由字组成的,如果相连的字在不同文本中出现的次数越多,就证明这段相连的字很有可能就 ...

  9. 【NLP】中文分词:原理及分词算法

    一.中文分词 词是最小的能够独立活动的有意义的语言成分,英文单词之间是以空格作为自然分界符的,而汉语是以字为基本的书写单位,词语之间没有明显的区分标记,因此,中文词语分析是中文信息处理的基础与关键. ...

随机推荐

  1. list练习题—输入工人信息

    1) 创建一个List,在List 中增加三个工人,基本信息如下: 姓名 年龄 工资 zhang3 18 3000 li4 25 3500 wang5 22 3200 2) 在li4 之前插入一个工人 ...

  2. 【Qt学习笔记】Qt+VS2010的配置

    http://blog.csdn.net/jocyln9026/article/details/8575218 关于Qt Qt是1991年由Trolltech公司开发的一个跨平台的C++图形用户界面应 ...

  3. css 纯css轮播图 示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. 性能测试监控平台Grafana的使用

    Grafana的监控是基于数据库的,通过插件获取到服务器性能并存储到数据库中,然后使用Grafana连接数据库形成可视化的图表.本篇给大家介绍对服务器的性能的监控,下一篇会介绍对于mysql数据库的监 ...

  5. pytorch-- Attention Mechanism

    1. paper: Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translat ...

  6. Nginx安装(yum源)

    CentOS7 $ vi /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/c ...

  7. 解决shiro自定义filter后,ajax登录无法登录,并且无法显示静态资源的问题

    这个问题困扰了我一天,看了下面两个文章,豁然开朗: https://www.cnblogs.com/gj1990/p/8057348.html https://412887952-qq-com.ite ...

  8. webpack性能优化

    Webpack优化打包速度以及性能优化 1.跟上技术的迭代(Node.Npm.Yarn) 2.在尽可能少的模块上应用loader 3.Plugin尽可能精简并确保可靠 4.resolve参数合理配置 ...

  9. Jenkins 插件使用国内镜像源-解决插件下载慢的问题

    问题 我们在Jenkins里面经常会遇到安装插件很慢,这是由于我们使用的是更新中心镜像默认为国外的源.现在我们可以进行设置为国内镜像源,来解决安装插件慢的问题. 解决办法 安装插件localizati ...

  10. ABP框架迁移到Mysql

    ABP框架 .NetCore3.x版本 1.首先找到xxx.Core 项目,添加引用Microsoft.EntityFrameworkCore.Tools 2.找到xxx.EntityFramewor ...