数据挖掘NPL——词义消歧复现

本文记录了一次Word Disambiguation的项目复现过程,使用了nltk和pywsd库。通过简单Lesk算法理解词在特定语境下的含义,探讨了词典和同义词集在解决词汇歧义中的应用。
摘要由CSDN通过智能技术生成

数据挖掘学习记录@Ur Guxxi

记一次简单的Word Disambiguation复现

从现在开始记录两门课的机器学习历程。
这次是一次简单的小项目复现,主要是跟着README做一遍,然后研究一下代码分别都在做什么,顺便扫一下知识盲点。
目标是把https://zhuanlan.zhihu.com/p/51279338中的小项目都能复现一遍并且弄清楚,希望做完以后NPL能算入个门吧。

项目名称: 词义消歧 Word Sense Disambiguation
源码地址: https://github.com/alvations/pywsd

相关包的安装

  • pip install -U nltk
  • python -m nltk.downloader ‘popular’
  • pip install -U pywsd
说明
  1. nltk包即Natural Language Toolkit,自然语言处理工具包,在NLP领域中,最常使用的一个Python库。其中官网文档。CSDN上随便找了个详细使用说明可以看看。
    这次我们使用到了nltk的corpus(获取和处理语料库的模块)
  2. pywsd包即Python Implementations of Word Sense Disambiguation (WSD) technologies,就是本项目,在Pypi可以找到所以可用pip直接下载安装
  3. python的-m参数作用是run library module as a script (terminates option list),即将模块当作脚本运行。
    python xxx.py
    /python -m xxx.py
    这是两种加载py文件的方式:
    前者叫做直接运行
    后者相当于import,叫做当做模块来启动。不同的加载py文件的方式,主要是影响sys.path这个属性:
    直接启动是把run.py文件,所在的目录放到了sys.path属性中。
    模块启动是把你输入命令的目录(也就是当前路径),放到了sys.path属性中
    参考:https://www.cnblogs.com/xueweihan/p/5118222.html
  4. pip安装时的-U参数就是 --upgrade,意思是如果已安装就升级到最新版

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G

模型的使用

一下是模型的使用方法,跟着README敲就可以了,我是直接在terminal进行操作的。
⚠️特别注意:GitHub中的样例使用的是python2语法,需要自己修改print函数。

@xxx python
Python 3.7.1 (default, Dec 14 2018, 13:28:58)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pywsd.lesk import simple_lesk
Warming up PyWSD (takes ~10 secs)... took 8.250467777252197 secs.
>>> sent = 'I went to the bank to deposit my money'
>>> ambiguous = 'bank'
>>> answer = simple_lesk(sent,ambiguous,pos='n')
>>> print answer
  File "<stdin>", line 1
    print answer
               ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(answer)?
>>> print(answer)
Synset('depository_financial_institution.n.01')
>>> print(answer.definition())
a financial institution that accepts deposits and channels the money into lending activities
>>> from pywsd import disambiguous
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'disambiguous' from 'pywsd' (/Users/magni/anaconda3/lib/python3.7/site-packages/pywsd/__init__.py)
>>> from pywsd import disambiguate
>>> from pywsd.similarity import max_similarity as maxsim
>>> disambiguate('I went to the bank to deposit my money.')
[('I', None), (
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值