首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用给定的上下文窗口计算PMI值

使用给定的上下文窗口计算PMI值
EN

Stack Overflow用户
提问于 2016-11-06 01:45:17
回答 1查看 609关注 0票数 0

假设有以下基础:

代码语言:javascript
复制
basis = "Each word of the text is converted as follows: move any consonant (or consonant cluster) that appears at the start of the word to the end, then append ay."

以及以下几个字:

代码语言:javascript
复制
words = "word, text, bank, tree"

如何计算“word”中每个单词与"basis“中每个单词的PMI值,其中我可以使用大小为5的上下文窗口(即目标单词之前和之后的两个位置)?

我知道如何计算PMI,但我不知道如何处理上下文窗口的事实。

我计算‘正常’PMI-值如下:

代码语言:javascript
复制
def PMI(ContingencyTable):
    (a,b,c,d,N) = ContingencyTable
    # avoid log(0)
    a += 1
    b += 1
    c += 1
    d += 1
    N += 4

    R_1 = a + b
    C_1 = a + c

    return log(float(a)/(float(R_1)*float(C_1))*float(N),2)
EN

回答 1

Stack Overflow用户

发布于 2016-11-06 12:04:29

我在PMI上做了一些搜索,看起来像是重型软件包在那里,“窗口”包括在内

PMI中,“相互”似乎指的是两个不同单词的联合概率,因此您需要根据问题陈述来确定这一概念

我处理了一个较小的问题,即在您的问题陈述中生成简短的窗口列表,这主要是为了我自己的练习

代码语言:javascript
复制
def wndw(wrd_l, m_l, pre, post):
    """
    returns a list of all lists of sequential words in input wrd_l
    that are within range -pre and +post of any word in wrd_l that matches
    a word in m_l

    wrd_l      = list of words
    m_l        = list of words to match on
    pre, post  = ints giving range of indices to include in window size      
    """
    wndw_l = list()
    for i, w in enumerate(wrd_l):
        if w in m_l:
           wndw_l.append([wrd_l[i + k] for k in range(-pre, post + 1)
                                           if 0 <= (i + k ) < len(wrd_l)])
    return wndw_l

basis = """Each word of the text is converted as follows: move any
             consonant (or consonant cluster) that appears at the start
             of the word to the end, then append ay."""

words = "word, text, bank, tree"

print(*wndw(basis.split(), [x.strip() for x in words.split(',')], 2, 2),
      sep="\n")
['Each', 'word', 'of', 'the']
['of', 'the', 'text', 'is', 'converted']
['of', 'the', 'word', 'to', 'the']
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40441327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档