作业任务:

使用98年人民日报语料库进行词性标注训练及测试。

作业输入:

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

运行环境:

Jupyter Notebook, Python3

作业方法:

使用简单的统计词频的方法,对于单词的词性做出预测。暂未使用N-gram语言规则。

作业步骤:

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(']')] w_c = word.split('/')
# 生成语料库
if(len(w_c) > 1):
curpusfile.write(w_c[0] + ' ' + w_c[1] + '\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))
1114419
0.7999998205342874
0.20000017946571264

3.统计训练集的词频。

# 生成词频记录文件
from tqdm import tqdm_notebook doc = [] for sentence in tqdm_notebook(train_data):
words = sentence.strip().split(' ')
if len(words) > 1:
temp = []
temp.append(words[0])
temp.append(words[1])
flag = False
for line in doc:
if line[0] == temp[0] and line[1] == temp[1]:
line[2] += 1
flag = True
break
if not flag:
temp.append(1)
doc.append(temp)

4.选择概率最大的词性。

# 保存验证集
test_path = 'test.txt'
testfile = open(test_path, 'w', encoding='utf-8')
for sentence in test_data:
words = sentence.strip().split(' ')
if len(words) > 1:
testfile.write(sentence)
# 保存标注结果
result_path = 'result.txt'
resultfile = open(result_path, 'w', encoding='utf-8')
# 选择概率最大的词性进行标注
for sentence in tqdm_notebook(test_data):
words = sentence.strip().split(' ')
if len(words) > 1:
words[1] = 'n'
max = 0
for line in doc:
if line[0] == words[0] and line[2] > max:
max = line[2]
words[1] = line[1]
resultfile.write(words[0] + ' ' + word[1] + '\n')

性能评价:准确率

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)
right_num = 0 for i in range(0, list_num):
if result_lines[i][1] == test_lines[i][1]:
right_num += 1 print("准确率为:", right_num / list_num)
准确率为: 0.23189316857201872

【NLP】暑假课作业3 - 词性标注(简单词频概率统计)的更多相关文章

  1. 【NLP】暑假课作业1 - 中文分词(前向匹配算法实现)

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

  2. C语言博课作业11

    一.本周作业头 这个作业属与那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/10130 ...

  3. ROS第一次课作业分享

    ROS第一次课作业分享 2021年夏季学期学院开设了ROS的相关课程,最近在复习相关知识,正好做一下整理.下面是第一次作业的要求: 编写一个ROS节点,具备以下功能: 读取小海龟仿真器的/turtle ...

  4. 2016福州大学软件工程第二次团队作业——预则立&&他山之石成绩统计

    第二次团队作业--预则立&&他山之石成绩统计结果如下: T:团队成绩 P:个人贡献比 T+P:折算个人成绩,计算公式为T+T/15*团队人数*P 学号 组别 Team P T+P 03 ...

  5. Struts2实现简单的在线人数统计

    用Strust2框架的知识简单实现一个统计在线人数的问题. 1 搭建开发环境:(配置文件,jar包等问题) 2 index.jsp <%@ page language="java&qu ...

  6. 超简单的qps统计方法(推荐)【转】

    统计最近N秒内的QPS值(包括每秒select,insert等值) mysql> select variable_name,sum(per_sec) as qps from (select st ...

  7. 用python实现简单EXCEL数据统计的实例

    用python实现简单EXCEL数据统计的实例 下面小编就为大家带来一篇用python实现简单EXCEL数据统计的实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 任 ...

  8. 作业3-个人项目<词频统计>

    上了一天的课,现在终于可以静下来更新我的博客了.       越来越发现,写博客是一种享受.来看看这次小林老师的“作战任务”.                词频统计 单词: 包含有4个或4个以上的字 ...

  9. 作业4-两人编程<词频统计>

     协作:苗中峰,刘鑫成       我主要攻克排序,成哥写了文件流的使用.整合工作由我完成,成哥帮我查阅资料,避免和解决语法错误.              这次任务较作业三的变化是:       * ...

随机推荐

  1. web语义化这个坑

    什么是wen语义化:https://www.zhihu.com/question/20455165 标签大全:http://www.w3school.com.cn/tags/tag_html.asp ...

  2. SpingBoot错误信息处理及原理

    SpringBoot错误信息处理机制 在一个web项目中,总需要对一些错误进行界面或者json数据返回,已实现更好的用户体验,SpringBoot中提供了对于错误处理的自动配置 ErrorMvcAut ...

  3. Unity酱~ 卡通渲染技术分析(二)

    前面的话 上一篇Unity酱~ 卡通渲染技术分析(一) 写了CharaMain.cginc,服装的渲染是怎么实现的.这篇来分析一下头发跟皮肤的实现 头发 本来以为unitychan的头发会有各向异性的 ...

  4. Ubuntu18 永久设置分辨率1920x1080

    起因 虚拟机(virtualBox)中设置 1920*1080 的分辨率后, 每次重启后都会回到默认,永久设置的方式如下 步骤: 添加系统设置 sudo xrandr --newmode " ...

  5. [Effective Java 读书笔记] 第三章类和接口 第十六条

    第十六条 复合优先于继承 如果不确定B和A的关系是,is-a的关系,B确实也是A,那么久不应该使用B继承A,否则会暴露实现细节, 你的实现都会限制在原始的实现上. 书中举的第一个例子,实现了一个类ex ...

  6. C#开源组件DocX版本区别点滴

    在C#中,需要处理Office Word文档时,由于MsOffice Com的版本局限性,所以选择不与本机MsOffice安装与否或安装版本相关的软件,以便软件或使用时的通用性与版权限制,特别是对于国 ...

  7. LwIP与IPv6

    2.0.0中才开始支持IPv6,在此版本中改写了SNMP,但还没有IPv6的统计量.目前最新版本是2.0.2,其中SNMP也没有IPv6统计量(哪些?与IP的统计量有何区别?) 1.4.1中虽然有ip ...

  8. Prometheus监控k8s集合

    Prometheus监控k8s Prometheus监控k8s(1)-Prometheus简介 Prometheus监控k8s(2)-手动部署Prometheus Prometheus监控k8s(3) ...

  9. python-nmap 使用

    安装 [root@localhost ~]# yum -y install nmap [root@localhost ~]# pip install python-nmap 使用 import nma ...

  10. Load_file 常用路径

    load_file 常用路径 WINDOWS下: c:/boot.ini //查看系统版本 c:/windows/php.ini //php配置信息 c:/windows/my.ini //MYSQL ...