2016-11-05 2 views
0

Учитывая следующее основание:значения Расчет PMI с использованием заданного контекста окна

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" 

Как можно вычислить PMI-значения каждого слова в «слова» по сравнению с каждым словом в «базе», где я могу использовать размер окна контекста 5 (это две позиции до и два после целевого слова)?

Я знаю, как вычислить PMI, но я не знаю, как обрабатывать факт контекстного окна.

рассчитать «нормальные» PMI-значения следующим образом:

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) 

ответ

0

Я сделал немного поиск по PMI, выглядит как тяжелые пакеты дежурных там, «оконным» включен

В PMI «взаимный», по-видимому, относится к совместной вероятности двух разных слов, поэтому вам необходимо укрепить эту идею в отношении постановки проблемы.

Я взял на себя меньшую проблему только для создания коротких оконных списков в вашей проблеме statem в основном для собственных занятий

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']