2017-05-11 1 views
2

私は15グラムの単語のテキストを持っています。私は、ウィンドウの固定サイズの単語の同時発生回数を計算し、それを処理する必要があります。例えば、ここに私のテキストがあります。NLPタスクのための単語の共起行列を計算するためのツール

「フーはフーだよ、バーは何を言ってる?」

このテキストからウィンドウサイズ= 4の共起頻度を持つbigramを構築するには、出力は次のようになります。

単語1-WORD2カウント

がfoo、言う、1つの

FOO、ホー、1

のfoo、バー、

1は、ホー、と言うが、2

は言います、バー、2

は言う、1

ホー、バー、1つの

ホー、1

は、私はすでに、このようなNLTKとしてこれを行うためのツールがあります知っている、1

、言うことを、どのような、1つの

バー、マルチスレッド化されていないため、サイズが15GBのテキストでは機能しません。与えられたウィンドウサイズと速い方法で単語の共起行列を私に与えることができるツールはありますか?

答えて

0

私はこのようなツールを自分で探しましたが、それを見つけたことはありません。私は通常、それを行うためのスクリプトを書くだけです。ここであなたに使用であるかもしれないいくつかの制限のサンプルです:

import concurrent.futures 
from collections import Counter 

tokens = [] 

for _ in range(10): 
    tokens.extend(['lazy', 'old', 'fart', 'lying', 'on', 'the', 'bed']) 


def cooccurrances(idx, tokens, window_size): 

    # beware this will backfire if you feed it large files (token lists) 
    window = tokens[idx:idx+window_size]  
    first_token = window.pop(0) 

    for second_token in window: 
     yield first_token, second_token 

def harvest_cooccurrances(tokens, window_size=3, n_workers=5): 
    l = len(tokens) 
    harvest = [] 
    with concurrent.futures.ThreadPoolExecutor(max_workers=n_workers) as executor: 
     future_cooccurrances = { 
      executor.submit(cooccurrances, idx, tokens, window_size): idx 
      for idx 
      in range(l) 
     } 
     for future in concurrent.futures.as_completed(future_cooccurrances): 
      try: 
       harvest.extend(future.result()) 
      except Exception as exc: 
       # you may want to add some logging here 
       continue 


    return harvest 

def count(harvest): 
    return [ 
     (first_word, second_word, count) 
     for (first_word, second_word), count 
     in Counter(harvest).items() 
    ] 


harvest = harvest_cooccurrances(tokens, 3, 5) 
counts = count(harvest) 

print(counts) 

あなただけのコードを実行する場合は、これを参照する必要があります

[('lazy', 'old', 10), 
('lazy', 'fart', 10), 
('fart', 'lying', 10), 
('fart', 'on', 10), 
('lying', 'on', 10), 
('lying', 'the', 10), 
('on', 'the', 10), 
('on', 'bed', 10), 
('old', 'fart', 10), 
('old', 'lying', 10), 
('the', 'bed', 10), 
('the', 'lazy', 9), 
('bed', 'lazy', 9), 
('bed', 'old', 9)] 

制限

  • このスクリプトは、スライスのために大きなトークンリストを処理しません。
  • windowリストのチョップアップはここでは機能しますが、ウィンドウリストスライスで何かを行う場合は注意してください。
  • チョーク(やはり大きくなる場合はCounterオブジェクトを置き換えるために、あなたはspaCyMatcherを使用して、このような何かを書くことができるかもしれ

    hereを参照)、しかし、私はこれはワイルドカードとしてだけではまだ動作することを確認していない:リストの制限)

WILD GUESSあなたはまだ(私の経験では)少し不安定である必要があります。

関連する問題