NLTK 中的无监督 HMM 训练

2024-05-03

我只是想进行非常简单的无监督 HMM 训练nltk http://www.nltk.org/.

考虑:

import nltk
trainer = nltk.tag.hmm.HiddenMarkovModelTrainer()
from nltk.corpus import gutenberg
emma = gutenberg.words('austen-emma.txt')
m = trainer.train_unsupervised(emma)
ValueError: A Uniform probability distribution must have at least one sample.

我能找到一个使用的例子吗nltk.tag.hmm.HiddenMarkovModelTrainer.train_unsupervised http://nltk.googlecode.com/svn/trunk/doc/api/nltk.tag.hmm-pysrc.html#HiddenMarkovModelTrainer.train_supervised?


显然,nltk要求我们手动指定观察到的符号和状态的集合,并且还要求未标记序列的形式为[ [(symb,tag),(symb,tag),...], [(symb,tag),(symb,tag),...], ...].

所以我们有

s = """"Your humble writer knows a little bit about a lot of things, but despite writing a fair amount about text processing (a book, for example), linguistic processing is a relatively novel area for me. Forgive me if I stumble through my explanations of the quite remarkable Natural Language Toolkit (NLTK), a wonderful tool for teaching, and working in, computational linguistics using Python. Computational linguistics, moreover, is closely related to the fields of artificial intelligence, language/speech recognition, translation, and grammar checking.\nWhat NLTK includes\nIt is natural to think of NLTK as a stacked series of layers that build on each other. Readers familiar with lexing and parsing of artificial languages (like, say, Python) will not have too much of a leap to understand the similar -- but deeper -- layers involved in natural language modeling.\nGlossary of terms\nCorpora: Collections of related texts. For example, the works of Shakespeare might, collectively, by called a corpus; the works of several authors, corpora.\nHistogram: The statistic distribution of the frequency of different words, letters, or other items within a data set.\nSyntagmatic: The study of syntagma; namely, the statistical relations in the contiguous occurrence of letters, words, or phrases in corpora.\nContext-free grammar: Type-2 in Noam Chomsky's hierarchy of the four types of formal grammars. See Resources for a thorough description.\nWhile NLTK comes with a number of corpora that have been pre-processed (often manually) to various degrees, conceptually each layer relies on the processing in the adjacent lower layer. Tokenization comes first; then words are tagged; then groups of words are parsed into grammatical elements, like noun phrases or sentences (according to one of several techniques, each with advantages and drawbacks); and finally sentences or other grammatical units can be classified. Along the way, NLTK gives you the ability to generate statistics about occurrences of various elements, and draw graphs that represent either the processing itself, or statistical aggregates in results.\nIn this article, you'll see some relatively fleshed-out examples from the lower-level capabilities, but most of the higher-level capabilities will be simply described abstractly. Let's now take the first steps past text processing, narrowly construed. """
sentences = s.split('.')[:-1]
seq = [map(lambda x:(x,''), ss.split(' ')) for ss in sentences]
symbols = list(set([ss[0] for sss in seq for ss in sss]))
states = range(5)
trainer = nltk.tag.hmm.HiddenMarkovModelTrainer(states=states,symbols=symbols)
m = trainer.train_unsupervised(seq)
m.random_sample(random.Random(),10)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NLTK 中的无监督 HMM 训练 的相关文章

  • 如何有效计算文档流中文档之间的相似度

    我收集文本文档 在 Node js 中 其中一个文档i表示为单词列表 考虑到新文档以文档流的形式出现 计算这些文档之间相似性的有效方法是什么 我目前对每个文档中单词的归一化频率使用余弦相似度 我不使用 TF IDF 词频 逆文档频率 因为我
  • 比较文本文档含义的最佳方法?

    我正在尝试找到使用人工智能和机器学习方法来比较两个文本文档的最佳方法 我使用了 TF IDF Cosine 相似度和其他相似度度量 但这会在单词 或 n gram 级别上比较文档 我正在寻找一种方法来比较meaning的文件 最好的方法是什
  • scikit加权f1分数计算及使用

    我有一个关于weightedsklearn metrics f1 score 中的平均值 sklearn metrics f1 score y true y pred labels None pos label 1 average weig
  • target_vocab_size 在方法 tfds.features.text.SubwordTextEncoder.build_from_corpus 中到底意味着什么?

    根据这个链接 https www tensorflow org datasets api docs python tfds features text SubwordTextEncoder build from corpus target
  • 在 Databricks 中的 pyspark 数据帧上下载 punkt 时出现 NLTK 查找错误

    我试图通过对 Databricks 中的 pyspark 数据框应用余弦相似度来查找文本列 标题 标题 的相似性 我的函数称为 cosine sim udf 为了能够使用它 我必须进行第一次 udf 转换 将函数应用于 df 后出现查找错误
  • 如何在 nltk 中使用 hunpos 标记文本文件?

    有人可以帮我解决在 nltk 中标记语料库的 hunpos 语法吗 我要导入什么hunpos HunPosTagger module http nltk googlecode com svn trunk doc api nltk tag h
  • 将 python NLTK 解析树保存到图像文件[重复]

    这个问题在这里已经有答案了 这可能会复制这个 stackoverflowquestion https stackoverflow com questions 23429117 saving nltk drawn parse tree to
  • 实时跟踪每分钟/小时/天的前 100 个 Twitter 单词

    我最近遇到这样一个面试问题 Given a continuous twitter feed design an algorithm to return the 100 most frequent words used at this min
  • ANEW 字典可以用于 Quanteda 中的情感分析吗?

    我正在尝试找到一种方法来实施英语单词情感规范 荷兰语 以便使用 Quanteda 进行纵向情感分析 我最终想要的是每年的 平均情绪 以显示任何纵向趋势 在数据集中 所有单词均由 64 名编码员按照 7 分李克特量表在四个类别上进行评分 这提
  • 如何下载 NLTK 数据?

    更新的答案 NLTK 适用于 2 7 我有3 2 我卸载了3 2并安装了2 7 现在可以了 我已经安装了 NLTK 并尝试下载 NLTK 数据 我所做的是按照该网站上的说明进行操作 http www nltk org data html h
  • 快速 shell 命令删除文本文件中的停用词

    我有一个 2GB 的文本文件 我正在尝试从此文件中删除经常出现的英语停用词 我有 stopwords txt 包含这样的 a an the for and I 使用 shell 命令 例如 tr sed 或 awk 执行此操作的快速方法是什
  • openNLP 与 Solr 集成时出现异常

    我正在尝试将 openNLP 与 Solr 6 1 0 集成 我配置了架构和 solrconfig 文件 详细信息请参见 wiki 链接 https wiki apache org solr OpenNLP https wiki apach
  • PyMC:马尔可夫系统中的参数估计

    简单的马尔可夫链 假设我们想要估计系统的参数 以便我们可以在给定时间步 t 的状态的情况下预测系统在时间步 t 1 的状态 PyMC 应该能够轻松处理这个问题 让我们的玩具系统由一维世界中的移动物体组成 状态是对象的位置 我们想要估计潜在变
  • SpaCy 中的自定义句子边界检测

    我正在尝试在 spaCy 中编写一个自定义句子分段器 它将整个文档作为单个句子返回 我编写了一个自定义管道组件 它使用以下代码来执行此操作here https github com explosion spaCy issues 1850 但
  • 缩短文本并仅保留重要句子

    德国网站 nandoo net 提供了缩短新闻文章的可能性 如果使用滑块更改百分比值 文本会发生变化并且某些句子会被遗漏 您可以在这里看到它的实际效果 http www nandoo net read article 299925 http
  • Python模块可以访问英语词典,包括单词的定义[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找一个 python 模块 它可以帮助我从英语词典中获取单词的定义 当然有enchant 这可以帮助我检查该单词是否存在于英语中
  • Lucene 标准分析器与 Snowball

    刚刚开始使用 Lucene Net 我使用标准分析器索引了 100 000 行 运行了一些测试查询 并注意到如果原始术语是单数 则复数查询不会返回结果 我知道雪球分析器增加了词干支持 这听起来不错 不过 我想知道 超过标准的雪球锣是否有任何
  • 如何从 Pandas DataFrame 转换为 Tensorflow BatchDataset 以进行 NLP?

    老实说 我想弄清楚如何转换数据集 格式 pandasDataFrame或 numpy 数组 转换为简单文本分类张量流模型可以训练用于情感分析的形式 我使用的数据集类似于 IMDB 包含文本和标签 正面或负面 我看过的每个教程要么以不同的方式
  • 获取 NLTK 索引的所有结果

    我正在使用 NLTK 来查找单词的一致性 但我不知道如何获取所有结果并将它们放入list or set 例如 text concordance word 仅打印前 25 个结果 TL DR text concordance lines 10
  • Java文本输出中的UTF-8编码问题

    我一直致力于测试高棉语 Unicode Wordbreaker 的各种解决方案 高棉语单词之间没有空格 这使得拼写检查和语法检查变得困难 以及从旧高棉语转换为高棉语 Unicode 我得到了一些源代码 现在在线 http www white

随机推荐