python - nltk中句子的概率树同时使用前瞻和回顾依赖

标签 python dictionary nlp nltk linguistics

nltk或其他任何NLP工具是否允许根据输入句子构建概率树从而将输入文本的语言模型存储在字典树中,以下example给出了粗略的想法,但我需要相同的功能,这样一个词 Wt 不仅可以根据过去的输入词(历史)Wt-n 进行概率建模,还可以像 Wt+m 这样的前瞻词进行建模。此外,回溯和前瞻字数也应为 2 或更多,即双字母或更多。 python 中是否有任何其他库可以实现此目的?

from collections import defaultdict
import nltk
import math

ngram = defaultdict(lambda: defaultdict(int))
corpus = "The cat is cute. He jumps and he is happy."
for sentence in nltk.sent_tokenize(corpus):
    tokens = map(str.lower, nltk.word_tokenize(sentence))
    for token, next_token in zip(tokens, tokens[1:]):
        ngram[token][next_token] += 1
for token in ngram:
    total = math.log10(sum(ngram[token].values()))
    ngram[token] = {nxt: math.log10(v) - total for nxt, v in ngram[token].items()}

该解决方案需要向前看和向后看,一个特殊的子类字典可能有助于解决这个问题。还可以指向讨论实现此类系统的相关资源。 nltk.models 似乎在做类似的事情,但不再可用。 NLP 中是否有任何现有的设计模式来实现这个想法?基于 skip gram 的模型也类似于这个想法,但我觉得这应该已经在某个地方实现了。

最佳答案

如果我正确理解你的问题,你正在寻找一种方法来预测给定周围上下文(不仅是后向上下文,还有前向上下文)的单词的概率。 一种快速破解方法是训练两种不同的语言模型。一个从右到左,另一个从左到右,然后给定上下文的单词概率将是前向和后向上下文的归一化总和。

扩展你的代码:

from collections import defaultdict
import nltk
from nltk.tokenize import word_tokenize
import numpy as np


ngram = defaultdict(lambda: defaultdict(int))
ngram_rev = defaultdict(lambda: defaultdict(int)) #reversed n-grams
corpus = "The cat is cute. He jumps and he is happy."

for sentence in nltk.sent_tokenize(corpus):
    tokens = map(str.lower, nltk.word_tokenize(sentence))
    for token, next_token in zip(tokens, tokens[1:]):
        ngram[token][next_token] += 1
    for token, rev_token in zip(tokens[1:], tokens):
        ngram_rev[token][rev_token] += 1
for token in ngram:
    total = np.log(np.sum(ngram[token].values()))
    total_rev = np.log(np.sum(ngram_rev[token].values()))
    ngram[token] = {nxt: np.log(v) - total 
                    for nxt, v in ngram[token].items()}
    ngram_rev[token] = {prv: np.log(v) - total_rev 
                    for prv, v in ngram_rev[token].items()}

现在上下文在 ngram 和 ngram_rev 中,分别包含前向和后向上下文。

您还应该考虑平滑度。也就是说,如果在你的训练语料库中没有看到给定的短语,你只会得到零概率。为了避免这种情况,有许多平滑技术,其中最简单的是 add-on。平滑。

关于python - nltk中句子的概率树同时使用前瞻和回顾依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31986466/

相关文章:

python - 如何在 Python 3 中使用 readlines 读取由空格分隔的整数输入文件?

swift - 将字典变成自定义类型

python - Python 上所有键及其嵌套字典的深度

python - 当字典键相同时,为什么 Python 只保留最后一个键值对?

nlp - 从哪里开始 : Natural language processing and AI using Python

python - 字典键中的多值搜索

python - 设置 Statsmodels 线性回归的数据格式

python - 如何线程 Tweepy 流

machine-learning - 当我使用文本文件输入时,syntaxnet demo.sh 挂起

python - scikit-learn 中的 TFIDFVectorizer 应该如何工作?