执行示例代码

def npchunk_features(sentence, i, history):word, pos = sentence[i]return {"pos": pos}class ConsecutiveNPChunkTagger(nltk.TaggerI):def __init__(self, train_sents):train_set = []for tagged_sent in train_sents:untagged_sent = nltk.tag.untag(tagged_sent)history = []for i, (word, tag) in enumerate(tagged_sent):featureset = npchunk_features(untagged_sent, i, history)train_set.append((featureset, tag))history.append(tag)# self.classifier = nltk.NaiveBayesClassifier.train(train_set)self.classifier = nltk.MaxentClassifier.train(train_set, algorithm='MEGAM', trace=0)  #def tag(self, sentence):history = []for i, word in enumerate(sentence):featureset = npchunk_features(sentence, i, history)tag = self.classifier.classify(featureset)history.append(tag)return zip(sentence, history)class ConsecutiveNPChunker(nltk.ChunkParserI):def __init__(self, train_sents):tagged_sents = [[((w,t),c) for (w,t,c) innltk.chunk.tree2conlltags(sent)]for sent in train_sents]self.tagger = ConsecutiveNPChunkTagger(tagged_sents)def parse(self, sentence):tagged_sents = self.tagger.tag(sentence)conlltags = [(w, t, c) for ((w, t), c) in tagged_sents]return nltk.chunk.conlltags2tree(conlltags)test_sents = conll2000.chunked_sents('test.txt', chunk_types=['NP'])
train_sents = conll2000.chunked_sents('train.txt', chunk_types=['NP'])
chunker = ConsecutiveNPChunker(train_sents)
print(chunker.evaluate(test_sents))

报错

Traceback (most recent call last):File "E:/Python Practice/NLP/Chapter7.py", line 225, in <module>chunker = ConsecutiveNPChunker(train_sents)File "E:/Python Practice/NLP/Chapter7.py", line 216, in __init__self.tagger = ConsecutiveNPChunkTagger(tagged_sents)File "E:/Python Practice/NLP/Chapter7.py", line 201, in __init__train_set, algorithm='MEGAM', trace=0)  #File "D:\Anaconda3\lib\site-packages\nltk\classify\maxent.py", line 335, in traintrain_toks, trace, encoding, labels, gaussian_prior_sigma, **cutoffsFile "D:\Anaconda3\lib\site-packages\nltk\classify\maxent.py", line 1483, in train_maxent_classifier_with_megamstdout = call_megam(options)File "D:\Anaconda3\lib\site-packages\nltk\classify\megam.py", line 168, in call_megamconfig_megam()File "D:\Anaconda3\lib\site-packages\nltk\classify\megam.py", line 57, in config_megamurl="http://www.umiacs.umd.edu/~hal/megam/index.html",File "D:\Anaconda3\lib\site-packages\nltk\internals.py", line 690, in find_binaryname, path_to_bin, env_vars, searchpath, binary_names, url, verboseFile "D:\Anaconda3\lib\site-packages\nltk\internals.py", line 674, in find_binary_iterpath_to_bin or name, env_vars, searchpath, binary_names, url, verboseFile "D:\Anaconda3\lib\site-packages\nltk\internals.py", line 632, in find_file_iterraise LookupError("\n\n%s\n%s\n%s" % (div, msg, div))
LookupError: ===========================================================================
NLTK was unable to find the megam file!
Use software specific configuration paramaters or set the MEGAM environment variable.For more information on megam, see:<http://www.umiacs.umd.edu/~hal/megam/index.html>
===========================================================================

通过StackOverflow,在这里下载MEGAM源文件Source: megam_src.tgz, 下载之后解压,然后在代码中(程序的开始)为MEGAM添加环境变量

import os
os.environ["MEGAM"] = 'D:\Anaconda3\Lib\site-packages\MEGAM\megam-64'

再次运行,报错

  File "E:/Python Practice/NLP/Chapter7.py", line 204, in __init__train_set, algorithm='MEGAM', trace=0)  #File "D:\Anaconda3\lib\site-packages\nltk\classify\maxent.py", line 335, in traintrain_toks, trace, encoding, labels, gaussian_prior_sigma, **cutoffsFile "D:\Anaconda3\lib\site-packages\nltk\classify\maxent.py", line 1483, in train_maxent_classifier_with_megamstdout = call_megam(options)File "D:\Anaconda3\lib\site-packages\nltk\classify\megam.py", line 172, in call_megamp = subprocess.Popen(cmd, stdout=subprocess.PIPE)File "D:\Anaconda3\lib\subprocess.py", line 800, in __init__restore_signals, start_new_session)File "D:\Anaconda3\lib\subprocess.py", line 1207, in _execute_childstartupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application

该错误原因是python版本是64位,调用的库是32位的,所以解决方法便是安装32位的python。在官网下载一个32位的python,这里选择 python3.7.7 Windows x86 executable installer 安装成功,更换解释器之后还是报同样的错误。。。

由于本人使用了Anaconda,又在Anaconda官网安装了一个32位的Anaconda,依旧报错。。。

有时间再来更新

Python自然语言处理 7.3 Training Classifier-Based Chunkers 中,algorithm=‘MEGAM‘ 的示例报错相关推荐

  1. selenium+python,解决selenium弹出新页面,无法定位元素的问题(报错:Unable to locate element:元素)

    selenium+python,解决selenium弹出新页面,无法定位元素的问题(报错:Unable to locate element:元素) 参考文章: (1)selenium+python,解 ...

  2. python中引入包的时候报错AttributeError: module ‘sys‘ has no attribute ‘setdefaultencoding‘解决方法?

    python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法? 参考文章: (1)pyth ...

  3. pycharm中python调用百度aip所遇问题解决方案记录:pip安装时版本报错以及aip导入时报错no module named aip/requests)

    pycharm中python调用百度aip所遇问题解决方案记录:pip安装时版本报错以及aip导入时报错no module named aip/requests pip安装时版本报错 pip升级后安装 ...

  4. python字符串拼接数字_解决Python中字符串和数字拼接报错的方法

    解决Python中字符串和数字拼接报错的方法 前言 众所周知Python不像JS或者PHP这种弱类型语言里在字符串连接时会自动转换类型,如果直接将字符串和数字拼接会直接报错. 如以下的代码: # co ...

  5. python jupyter notebook运行没反应_jupyter notebook运行命令没有反应,右上报错这个

    有没有哪位 大佬知道这个该怎么解决呀,萌新一枚初学python,弄了一整天了 都还没有弄好 自己 也按照网上说的都做了都没用,也重新安装过了,还是报错 全部的内容是这样的 Traceback (mos ...

  6. python处理pdf提取指定数据_python从PDF中提取数据的示例

    01 前言 数据是数据科学中任何分析的关键,大多数分析中最常用的数据集类型是存储在逗号分隔值(csv)表中的干净数据.然而,由于可移植文档格式(pdf)文件是最常用的文件格式之一,因此每个数据科学家都 ...

  7. Python中的这几种报错你遇到过吗?

    总第139篇/叶枫 我新开了一个系列,叫做『读者分享系列』,这个系列主要用来发布一些我书的读者写的一些读书笔记和学习笔记的. 今天是第一篇,这一篇来自我们的学习委员叶枫同学.叶枫同学把书中所有代码已经 ...

  8. python中文乱码 def decode-python处理一些乱码的中文文本时decode('utf-8')报错的处理...

    用python写脚本时,遇到处理中文(乱码的中文)时,用decode("utf-8")会发现始终会报错 >>> txt_from = open("/ho ...

  9. python后面空格报错_python中空格和table混用报错原因

    python是一门严格遵守缩进的语言,缩进的规则代表着程序的层级关系.我们来看一段代码.class MyForm(Form): value1 = StringField('value1') value ...

  10. python上传文件到linux服务器_python上传大文件到服务器报错

    项目需要,在mac上将打包好的文件(四五百兆)自动上传到web后台,用了两种方式上传,都报了类似的错误,在windows和linux上测试不会报错,但是到了mac打包机上就会报错: ①第一种报错: T ...

最新文章

  1. SpringBoot配置文件YAML配置注入(详解)
  2. mysql怎么测试事物_mysql事务简单测试
  3. 【pmcaff】凯文-凯利:最大颠覆来自创业公司与边缘产业
  4. Leaflet中使用leaflet-cion-pulse插件实现波动的图标效果
  5. ArrayList使用内存映射文件
  6. 论文浅尝 | 用对抗学习做知识表示(NAACL2018)
  7. 计算机科学系小学教育专业就业前景,小学教育专业就业方向与就业前景
  8. 数据结构基础(11) --循环链表的设计与实现
  9. 自定义注解 相关知识汇总(转)
  10. Sharepoint 弹出消息提示框
  11. 论文笔记:Deep neural networks for YouTube recommendations
  12. 淘宝客商家如何加入内容商品库?
  13. android dropbox目录,通过Android上传文件到Dropbox文件夹
  14. 《雍正皇帝·九王夺嫡》生态文化专有词泰译研究(第一章)
  15. kryo java_使用Kryo序列化任意Java对象(获取IllegalAccessError)
  16. Eclipse 照亮Java众生
  17. ThinkPad S5 升级安装Windows10后 连上网络后系统假死
  18. 西安交大计算机应用技术分数线,西安交大计算机的进一下
  19. 基于阿里云的块存储介绍
  20. 算法改进有多快?是否比迭代硬件收益更大?这是 MIT 的结论

热门文章

  1. 中国古典家具行业市场消费预测及投资风险分析报告2022-2027年
  2. 【实验二】基尔霍夫定律和叠加定理
  3. 密码学与计算机网络安全、信息安全与密码学、解决区块链隐私问题的密码学
  4. SQL查询之查询成绩表中每一科最大成绩的学生的所有信息(之前遇到一个面试题当时想不起来,回来后自己用数据库试了出来)
  5. 《开放复杂智能系统——基础、概念、分析、设计与实施》—第1章参考文献
  6. 树莓派-字符设备驱动编写
  7. 知识图谱里的知识表示:RDF
  8. 英语练习day3 一般现在时,疑问词与主语相同
  9. 电力载波远程控制系统
  10. 森林火灾预警系统 毕业设计 websocket+JAVA+Vue+SpringBoot+MySQL