windows7 32位环境下pynlpir的安装与使用

6 篇文章 0 订阅

windows7 32位环境下pynlpir的安装与使用

http://blog.sina.com.cn/s/blog_664f17ce0102w4h8.html

pynlpir是一个针对中科院分词器NLPIR/ICTCLAS而开发的一个python包,只能在python2.7或3上运行。仅能在Windows or GNU/Linux环境中运行。

一、pynlpir的安装

1、​安装环境:windows7 32位;python3.4;pycharm5.0.3

2、安装步骤:

(1)​下载最新版pynlpir,地址:https://pypi.python.org/pypi/PyNLPIR#downloads

下载的文件是:PyNLPIR-0.4.2-py3-none-any​.whl

(2)将文件后缀名改为.zip,然后将解压后的文件放到site-packages文件夹下,即可调用pynlpir。

示例:

import pynlpir

pynlpir.open()s = '欢迎科研人员、技术工程师、企事业单位与个人参与NLPIR平台的建设工作。'

​print(pynlpir.segment(s))​

结果:

[('欢迎', 'verb'), ('科研', 'noun'), ('人员', 'noun'), ('、', 'punctuation mark'), ('技术', 'noun'), ('工程师', 'noun'), ('、', 'punctuation mark'), ('企事业', 'noun'), ('单位', 'noun'), ('与', 'conjunction'), ('个人', 'noun'), ('参与', 'verb'), ('NLPIR', 'noun'), ('平台', 'noun'), ('的', 'particle'), ('建设', 'verb'), ('工作', 'verb'), ('。', 'punctuation mark')]​​

 二、pynlpir的使用

下面稍微讲一些PyNLPIR的用法

参考网址:http://pynlpir.readthedocs.org/en/latest/tutorial.html

有两种方式来使用PyNLPIR,一种是使用PyNLPIR提供的ctypes接口,另一种是使用PyNLPIR的帮助函数。ctypes接口包含的更广泛,但也更严密,枯燥。而帮助函数非常容易使用,但并不提供访问每一个NLPIR函数的方法。你可以混合使用这两种方法。首先,先介绍帮助函数​

1、PyNLPIR的帮助函数

帮助函数在PyNLPIR的__init__.py文件载入,因此可以直接使用pynlpir导入。

初始化NLPIR:

导入PyNLPIR可自动载入NLPIR API库:

import pynlpir

一旦被导入,调用open()来告诉NLPIR打开数据文件,初始化API。

pynlpir.open()

默认的输入编码方式是unicode或者是UTF-8。如果使用其他的编码(比如GBK或BIG5),调用open()函数的时候使用encoding关键字:

pynlpir.open(encoding='big5')

注意:不管使用什么编码方式,都可以在pynlpir的函数中通过。

PyNLPIR的帮助函数经常返回Unicode字符串。

文本分词:

首先,举个分句子的例子:

s="NLPIRf分词系统前身为2000年发布的ICTCLAS词法分词系统,从2009年开始,为了和……"

pynlpir.segment(s)

#sample output: [('NLPIR', 'noun'), ('分词', 'verb'),…… ]

如果不想使用词性标注,则在调用segment()函数时设置pos_tagging为False:

pynlpir.segment(s, pos_tagging=False)

#sample output: ['NLPIR', '分词', '系统', ……]

也可以定制怎么显示词性标记。默认的是使用最通用大的词性标记名称。比如说parent(例如,使用'noun'而不是'transcribed toponym'),如果你想使用最特殊的词性标记名称,比如说child,则设置pos_names为'child':

pynlpir.segment(s, pos_names='child')

如果想得到更多关于词性标记的信息,可以设置pos_names为'all',然后会返回一个词性标记的层次结构。例如:'noun: toponym: transcribed toponym'

pynlpir.segment(s, pos_names='all')

默认的词性标记名称为英文,如果想使用中文,则设置pos_english为False

pynlpir.segment(s, pos_english=False)

得到关键词:

另一个有用的函数是get_key_words():

pynlpir.get_key_words(s, weighted=True)

[('NLPIR', 2.08), ('系统', 1.74)]

get_key_words()分析了给出的中文字符串并返回了NLPIR认为的关键词。如果weighted设置为True,那么返回浮点型的词的权重。

关闭API:

当不再使用PyNLPIR时,你可以通过调用close()函数来释放内存空间:

pynlpir.close()

2、ctypes NLPIR 接口

 

参考:http://pynlpir.readthedocs.org/en/latest/api.html

pynlpir.nlpir通过ctypes提供了访问NLPIR函数的方法。你不用在使用上面提及的帮助函数就可以直接调用它们。这些函数与C实现的方法工作一样的效果。

pynlpir.nlpir包含NLPIR导出的需要调用许多函数的模块层次常量。获取更多信息,参考 pynlpir.nlpir pynlpir.nlpir的API页面。

(1)初始化和退出API

在调用任何NLPIR函数前,都需要初始化NLPIR API,通过调用Init()方法实现。需要指明NLPIR数据路径。PyNLPIR有一个副本,在顶层的文件路径中可以找到。所以调用Init()时使用模块层次的常量PACKAGE_DIR:

from pynlpir import nlpir

nlpir.Init(nlpir.PACKAGE_DIR)

NLPIR默认使用GBK编码方式。如果你不想使用GBK编码的字符串,则在调用Init()时改变编码:

nlpir.Init(nlpir.PACKAGE_DIR, nlpir.UTF8_CODE)

初始化NLPIR后,就可以使用NLPIR的函数了。

(2)pynlpir.nlpir.Exit()​​​

当应用完成后,最好使用Exit()退出NLPIR API并释放占用的内存空间​

Returns: Whether or not the function executed successfully.​

Return type: bool​

(3)pynlpir.nlpir.ParagraphProcess(s, pos_tagging=True) ​

Segments a string of Chinese text (encoded using the encoding specified when Init() was called).​

Parameters:s (str) – The Chinese text to process.

                   pos_tagging (bool) – Whether or not to return part of speech tags with the segmented words..​

Returns:The segmented words.​

Return type:str​

(4)pynlpir.nlpir.ParagraphProcessA(s, size_pointer, user_dict=True) ​

Segments a string of Chinese text (encoded using the encoding specified when Init() was called).​

Here is an example of how to use this function:​

size = ctypes.c_int()

result = ParagraphProcessA(s, ctypes.byref(size), False)

result_t_vector = ctypes.cast(result, ctypes.POINTER(ResultT))

words = []

for i in range(0, size.value):

    r = result_t_vector[i]

    word = s[r.start:r.start+r.length]

    words.append((word, r.sPOS))​

Parameters:

s (str) – The Chinese text to process.

size_pointer – A pointer to a ctypes.c_int that will be set to the result vector’s size.

user_dict (bool) – Whether or not to use the user dictionary.​

Returns:

A pointer to the result vector. Each result in the result vector is an instance of ResultT.​

(5)pynlpir.nlpir.FileProcess(source_filename, result_filename, pos_tagging=True)

Processes a text file.

Parameters:

source_filename (str) – The name of the file that contains the source text.

result_filename (str) – The name of the file where the results should be written.

pos_tagging (bool) – Whether or not to include part of speech tags in the output.

Returns:

If the function executed successfully, the processing speed is returned (float). Otherwise, 0 is returned.​

(6)pynlpir.nlpir.ImportUserDict(filename)

Imports a user-defined dictionary from a text file.

Parameters: filename (str) – The filename of the user’s dictionary file.

Returns: The number of lexical entries successfully imported.

Return type: int​

(7)pynlpir.nlpir.AddUserWord(word)

Adds a word to the user’s dictionary.

Parameters: word (str) – The word to add to the dictionary.

Returns: 1 if the word was added successfully, otherwise 0.

Return type: int​

(8)pynlpir.nlpir.SaveTheUsrDic()

Writes the user’s dictionary to disk.

Returns: 1 if the dictionary was saved successfully, otherwise 0.

Return type: int​

(9)pynlpir.nlpir.DelUsrWord(word)

Deletes a word from the user’s dictionary.

Parameters: word (str) – The word to delete.

Returns: -1 if the word doesn’t exist in the dictionary. Otherwise, the pointer to the word deleted.

Return type: int​

(10)pynlpir.nlpir.GetKeyWords(s, max_words=50, weighted=False)

Extracts key words from a string of Chinese text.

Parameters:

s (str) – The Chinese text to process.

max_words (int) – The maximum number of key words to return.

weighted (bool) – Whether or not the key words’ weights are returned.

Returns:The key words.

Return type:str​

(11)pynlpir.nlpir.GetFileKeyWords(filename, max_words=50, weighted=False)

Extracts key words from Chinese text in a file.

Parameters:

filename (str) – The file to process.

max_words (int) – The maximum number of key words to return.

weighted (bool) – Whether or not the key words’ weights are returned.

Returns:The key words.

Return type:str​

(12)pynlpir.nlpir.GetNewWords(s, max_words=50, weighted=False)

Extracts new words from a string of Chinese text.

Parameters:

s (str) – The Chinese text to process.

max_words (int) – The maximum number of new words to return.

weighted (bool) – Whether or not the new words’ weights are returned.

Returns:The new words.

Return type:str​

(13)pynlpir.nlpir.GetFileNewWords(filename, max_words=50, weighted=False)

Extracts new words from Chinese text in a file.

Parameters:

filename (str) – The file to process.

max_words (int) – The maximum number of new words to return.

weighted (bool) – Whether or not the new words’ weights are returned.

Returns:The new words.

Return type:str


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值