Natural Language Processing with Python

Charpter 6.1

 import nltk
from nltk.corpus import brown def pos_features(sentence,i,history):
features = {"suffix(1)":sentence[i][-1:],
"suffix(2)":sentence[i][-2:],
"suffix(3)":sentence[i][-3:]}
if i == 0:
features["prev-word"]="<STAR>"
features["prev_tag"] ="<STAR>"
else:
features["prev_word"]=sentence[i-1]
features["prev_tag"]=history[i-1]
return features class ConsecutivePosTagger(nltk.TaggerI):
def __init__(self,train_sents):
train_set=[]
for tagged_sent in train_sents:
history=[]
untagged_sent = nltk.tag.untag(tagged_sent)
for i,(word,tag) in enumerate(tagged_sent):
featureset=pos_features(untagged_sent,i,history)
train_set.append((featureset,tag))
history.append(tag)
self.classifier=nltk.NaiveBayesClassifier.train(train_set) def tag(self,sentence):
history=[]
for i,word in enumerate(sentence):
featureset=pos_features(sentence,i,history)
tag=self.classifier.classify(featureset)
history.append(tag)
return zip(sentence,history) def test_ConsecutivePosTagger():
tagged_sents=brown.tagged_sents(categories='news')
size = int(len(tagged_sents) * 0.1)
train_sents, test_sents = tagged_sents[size:], tagged_sents[:size]
tagger = ConsecutivePosTagger(train_sents) print tagger.evaluate(test_sents)

流程为:

结果为:

0.796940194715

Sequence Classification的更多相关文章

  1. Kraken taxonomic sequence classification system

    kraken:是一个将分类标签打到短DNAreads上的分类序列器.

  2. .NET平台开源项目速览(13)机器学习组件Accord.NET框架功能介绍

    Accord.NET Framework是在AForge.NET项目的基础上封装和进一步开发而来.因为AForge.NET更注重与一些底层和广度,而Accord.NET Framework更注重与机器 ...

  3. RNN,写起来真的烦

    曾经,为了处理一些序列相关的数据,我稍微了解了一点递归网络 (RNN) 的东西.由于当时只会 tensorflow,就从官网上找了一些 tensorflow 相关的 demo,中间陆陆续续折腾了两个多 ...

  4. 文本分类实战(十)—— BERT 预训练模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  5. Accord.NET Framework 介绍

    阅读目录 1.基本功能与介绍 Accord.NET Framework是在AForge.NET项目的基础上封装和进一步开发而来.因为AForge.NET更注重与一些底层和广度,而Accord.NET  ...

  6. [Tensorflow] RNN - 02. Movie Review Sentiment Prediction with LSTM

    From: Predicting Movie Review Sentiment with TensorFlow and TensorBoard Ref: http://www.cnblogs.com/ ...

  7. 自然语言处理领域重要论文&资源全索引

    自然语言处理(NLP)是人工智能研究中极具挑战的一个分支.随着深度学习等技术的引入,NLP领域正在以前所未有的速度向前发展.但对于初学者来说,这一领域目前有哪些研究和资源是必读的?最近,Kyubyon ...

  8. [转]NLP Tasks

    Natural Language Processing Tasks and Selected References I've been working on several natural langu ...

  9. .NET数据挖掘与机器学习开源框架

    1.    数据挖掘与机器学习开源框架 1.1 框架概述 1.1.1 AForge.NET AForge.NET是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经 ...

随机推荐

  1. lepus bug

    ------------------------------------------------BUG fix-------------------------------------------- ...

  2. mysql 不能插入中文

    三.#vim /etc/mysql/my.cnf .(5.5以前系统)在[client]下面加入 default-character-set=utf8 在[mysqld]下面加入default-cha ...

  3. Spring Boot 系列教程13-注解定时任务

    注解 @Scheduled(cron = "0/5 * * * * ?") 相当于原来的xml版本的如下配置 <task:scheduled ref="schedu ...

  4. 新手引导-ugui

    http://www.unitymanual.com/thread-38287-1-1.html 我已经在 干货区发布了,所以 这里就记录一下地址,懒得再贴了 新年第一贴,大家 看完代码 ,是不是发现 ...

  5. js数组总结

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. Struts的前世今身

    1.Struts1的运行原理 a.初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件( ...

  7. android 代码优化:封锁输出日志

    可以使用 ProGuard 完全地删除任何在发布版中无用的语句,关于 ProGuard 参见: http://developer.android.com/guide/developing/tools/ ...

  8. 对比C#中==与equal方法

    C#中equal与==的区别 收藏 对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false.对于string 以外的引用类型,如果两个对象引用同一个对象,则 == ...

  9. Web爬去的C#请求发送

    public class HttpControler { //post请求发送 private Encoding m_Encoding = Encoding.GetEncoding("gb2 ...

  10. codeforces 665B Shopping

    暴力 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #incl ...