1.4.2 安装StanfordNLP并编写Python接口类 1.4.3 执行Stanford词性标注-《NLP自然语言处理原理与实践》

这两节是相关的,所以放一起写

前提:

1.必须确保安装了jdk,且进行了环境配置(这部分内容自行百度,有很多解答)
2.下载stanford-corenlp语言包:
书上链接:http://stanfordnlp.github.io/CoreNLP/
在这里插入图片描述
我下载后传到百度网盘上了,也可用这个:
链接:https://pan.baidu.com/s/15_-P3wI8qjwKo0VaoeqoeA
提取码:p0s8
3.下载中文模型:stanford-corenlp-4.0.0-models-chinese.jar
书上链接:http://nlp.stanford.edu/software/CRF-NER.shtml
在这里插入图片描述
在这里插入图片描述
我下载后传到百度网盘上了,也可用这个:
链接:https://pan.baidu.com/s/1dLCMSYNZ8bVcPZyZOMrrng
提取码:9aqr
4.在X盘新建一个名为 stanford-corenlp 的文件夹,把2中下载文件解压到此处。在 stanford-corenlp 中新建一个名为 model 的文件夹,把3解压到此处。
5.下载stanford-tagger压缩包:
书上链接:http://nlp.stanford.edu/software/tagger.shtml
在这里插入图片描述
我下载后传到百度网盘上了,也可用这个:
链接:https://pan.baidu.com/s/1F1VCp6It2gjZyERcV0nZbA
提取码:dmgu

重点

1.给bat文件最后一行加上pause
bat文件实际上就是操作命令行,你把bat文件的内容直接复制粘贴到命令行也可正常执行,一般bat文件执行完成后会关闭命令行窗口,为了使它不关闭,我们可以在bat文件最后一行加上pause。方便观察错误。如下

java -mx300m -cp "stanford-postagger.jar;" edu.stanford.nlp.tagger.maxent.MaxentTagger -model %1 -textFile %2
pause

2.P26页bat文件解读
关于这段命令,我们只需要改三处,即:
java -mx300m -cp “stanford-postagger.jar;” edu.stanford.nlp.tagger.maxent.MaxentTagger -model %1 -textFile %2

①第一处放jar文件的路径:因为bat文件和jar放在同一个文件夹下所以不用写具体的路径
②第二处放模型的路径:最好写完整路径
③第三处用作声明从什么文件读取内容,最后结果输出到哪里去

3.P26页例子,使用bat实现
①首先新建一个postest.txt,里面写入:
在 包含 问题 的 所有 解 的 解空间树 中 , 按照 深度优先 搜索 的 策略 , 从 根结点 出发 深度 探索 解空间树 。

②编写bat文件,内容如下:

java -mx300m -cp "放三个jar的路径(stanford-tagger中),以分号分割:stanford-postagger.jar;slf4j-api.jar;slf4j-simple.jar" edu.stanford.nlp.tagger.maxent.MaxentTagger -model "放chinese-distsim.tagger路径(stanford-tagger中)" -textFile postest.txt > result.txt
pause

我都放在D盘了,写法如下

java -mx300m -cp "D:\\stanford-tagger-4.0.0\\stanford-postagger.jar;D:\\stanford-corenlp\\slf4j-api.jar;D:\\stanford-corenlp\\slf4j-simple.jar" edu.stanford.nlp.tagger.maxent.MaxentTagger -model "D:\\stanford-tagger-4.0.0\\models\\chinese-distsim.tagger" -textFile postest.txt > result.txt
pause

③双击bat运行,完成后会生成一个result文件,里面有我们分词的结果。

4.Python接口编写
实际上就是把bat中的内容进行拆分,最后我们只需提供jar路径、模型路径(输出文件可给可不给,但前两个必须得有),就能通过该接口生成命令,得到结果。

# -*- coding: utf-8 -*-

import sys
import os
import importlib
importlib.reload(sys)

class StanfordCoreNLP():                      # 所有StanfordNLP的父类

    def __init__(self, jarpath):

        self.root = jarpath

        self.tempsrcpath = "tempsrc"          # 输入临时文件路径

        #self.jarlist = ["ejml-0.23.jar","javax.json.jar","jollyday.jar","joda-time.jar","protobuf.jar","slf4j-api.jar","slf4j-simple.jar","stanford-corenlp-3.9.2.jar","xom.jar"]   书上给的这些jar文件,都不需要,只需要下面的一个“stanfor-postagger.jar”。

        self.jarlist = ["stanford-corenlp-4.0.0.jar"]

        self.jarpath = ""

        self.buildjars()

 

#java -mx300m -cp "D:\\stanford-tagger-4.0.0\\stanford-postagger.jar;" edu.stanford.nlp.tagger.maxent.MaxentTagger -model "D:\\stanford-tagger-4.0.0\\models\\chinese-distsim.tagger" -textFile postest.txt > result.txt

 

    def buildjars(self):                       # 根据root路径构建所有的jar包路径

        for jar in self.jarlist:

            self.jarpath += self.root + jar + ";" 
        print(self.jarpath)

 

    def savefile(self,path,sent):              # 创建临时文件存储路径

        fp = open(path, "wb")

        fp.write(sent.encode('utf-8'))

        fp.close()

 

    def delfile(self,path):                    # 删除临时文件

        os.remove(path)

 

class StanfordPOSTagger(StanfordCoreNLP):      # 词性标注子类

    def __init__(self,jarpath,modelpath):

        StanfordCoreNLP.__init__(self,jarpath)

        self.modelpath = modelpath             # 模型文件路径

        self.classifier = "edu.stanford.nlp.tagger.maxent.MaxentTagger"

        self.delimiter = "/"                   # 标注分隔符

        self.__buildcmd()

 

    def __buildcmd(self):                      # 构建命令行

        self.cmdline = 'java -mx300m -cp "'+self.jarpath + '" ' + self.classifier + ' -model "' + self.modelpath + '"'

        print("self.cmdline is : " + self.cmdline)

 

    def tag(self,sent):                        # 标注句子

        self.savefile(self.tempsrcpath,sent)
        tagtxt1 = os.popen(self.cmdline + " -textFile " + self.tempsrcpath, 'r')
        tagtxt = tagtxt1.buffer.read().decode(encoding='utf8')                # 结果输出到变量中
        self.delfile(self.tempsrcpath)
        return tagtxt

 

    def tagfile(self,inputpath,outpath):       # 标注文件

        os.system(self.cmdline + ' -textFile ' + inputpath + ' > ' + outpath)

5. 【1.4.3执行Stanford词性标注】
这一节实际上就是调用上一节的接口,可以通过观察命令行的输出是否和我们之前编写的bat文件一致,来观察哪里出错了。最后结果在命令行输出。

# -*- coding: utf-8 -*-

import sys

import os

from stanford import StanfordPOSTagger

import importlib
importlib.reload(sys)

root = 'D:/stanford-tagger-4.0.0/' #若采用全套语言包则:D:/stanford-corenlp/,由于init()函数对jar地址进行了拼接,所以只需写到jar上一级的路径即可,注意观察bat命令行的输出应该就能发现

modelpath = "D:/stanford-tagger-4.0.0/models/chinese-distsim.tagger"
#若采用全套语言包则:modelpath = root+"models/pos-tagger/chinese-distsim.tagger"
st = StanfordPOSTagger(root,modelpath)

seg_sent = '在 包含 问题 的 所有 解 的 解空间 树 中 ,按照 深度优先 搜索 的 策略 ,从 根节点 出发 深度 搜索 解空间 树 。'

taglist = st.tag(seg_sent)

print(taglist)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
About A Part-Of-Speech Tagger (POS Tagger) is a piece of software that reads text in some language and assigns parts of speech to each word (and other token), such as noun, verb, adjective, etc., although generally computational applications use more fine-grained POS tags like 'noun-plural'. This software is a Java implementation of the log-linear part-of-speech taggers described in these papers (if citing just one paper, cite the 2003 one): Kristina Toutanova and Christopher D. Manning. 2000. Enriching the Knowledge Sources Used in a Maximum Entropy Part-of-Speech Tagger. In Proceedings of the Joint SIGDAT Conference on Empirical Methods in Natural Language Processing and Very Large Corpora (EMNLP/VLC-2000), pp. 63-70. Kristina Toutanova, Dan Klein, Christopher Manning, and Yoram Singer. 2003. Feature-Rich Part-of-Speech Tagging with a Cyclic Dependency Network. In Proceedings of HLT-NAACL 2003, pp. 252-259. The tagger was originally written by Kristina Toutanova. Since that time, Dan Klein, Christopher Manning, William Morgan, Anna Rafferty, Michel Galley, and John Bauer have improved its speed, performance, usability, and support for other languages. The system requires Java 1.6+ to be installed. Depending on whether you're running 32 or 64 bit Java and the complexity of the tagger model, you'll need somewhere between 60 and 200 MB of memory to run a trained tagger (i.e., you may need to give java an option like java -mx200m). Plenty of memory is needed to train a tagger. It again depends on the complexity of the model but at least 1GB is usually needed, often more. Several downloads are available. The basic download contains two trained tagger models for English. The full download contains three trained English tagger models, an Arabic tagger model, a Chinese tagger model, and a German tagger model. Both versions include the same source and other required files. The tagger can be retrained on any language, given POS-annotated training text for the language.
以下是一个学生信息管理系统的数据字典,并从数据项、数据结构、数据流、数据存储和处理过程几个方面进行分析。 1.4.1 数据项 学生基本信息: - 姓名:字符串,长度不超过20个字符 - 性别:字符串,只能为“男”或“女” - 出生日期:日期类型,格式为YYYY-MM-DD - 身份证号码:字符串,长度为18个字符 - 联系电话:字符串,长度不超过11个字符 - 电子邮件:字符串,长度不超过50个字符 学籍信息: - 学号:字符串,长度不超过20个字符 - 入学日期:日期类型,格式为YYYY-MM-DD - 班级:字符串,长度不超过20个字符 - 专业:字符串,长度不超过30个字符 - 学院:字符串,长度不超过30个字符 - 学制:整型,如4年制、5年制等 - 学位:字符串,如本科、硕士等 学业信息: - 课程信息:包括课程名称、课程编号、学分、开课学期、授课教师等 - 成绩信息:包括课程成绩、绩点、学分绩点、排名等 - 奖励信息:包括奖励名称、获奖时间、奖励级别等 - 处分信息:包括处分名称、处分时间、处分级别等 1.4.2 数据结构 学生信息管理系统的数据结构包括学生基本信息、学籍信息、学业信息等,其中学籍信息和学业信息可以看作是学生基本信息的扩展。 1.4.3 数据流 学生信息管理系统的数据流包括学生信息的输入、输出和修改等,其中输入包括学生基本信息、学籍信息、学业信息等的录入,输出包括学生信息的查询和统计分析等。 1.4.4 数据存储 学生信息管理系统的数据存储包括学生基本信息、学籍信息、学业信息等的存储,一般采用关系型数据库进行存储管理。 1.4.5 处理过程 学生信息管理系统的处理过程包括学生信息的录入、查询、修改、删除等,其中录入需要对输入的数据进行验证和格式化处理,查询需要对查询条件进行检索和匹配,修改和删除需要对要修改或删除的数据进行验证和确认。同时,在进行数据处理的过程中需要注意保护学生信息的安全性和隐私性,遵循相关的法律法规和学校的规定。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值