2017-02-10 4 views
1

私の問題はかなりシンプルに見えますが、私はきれいな(そして効率的な)解決策を見つけられません。与えられたリストにある単語のグループに参加する

私は言葉の共通のグループに対応するタプルのリストを持っている:

ngrams = [("data", "scientist"), 
      ("machine", "learning"), 
      ("c", "+"), 
      ("+", "+"), 
      ("c", "+", "+"), 
      ("research", "and", "development"), 
      ("research", "and")] 

と文:

"i am a data scientist . i do machine learning and c + + but no deep learning . i like research and development" 

私のような単一のトークン内の単語の一般的なグループを統合したいと思いますそれ:

"i am a data_scientist . i do machine_learning and c_+_+ but no deep_learning . i like research_and_development" 

私はそうするエレガントな方法があると確信していますが、私はできませんでしたind any any ..

2タプルしかない場合は、zip(sentence, sentence[:1]を繰り返しますが、ngramsに最大8タプルがあり、この解決法は扱いにくいです!

+0

#のstr.joinに参加//docs.python.org/3/library/stdtypes.htmlハイライト=: Pythonのドキュメント

ttpsを?あなたは達成しようとしていますか? TF-IDFのバリエーションを試している場合は、Python http://scikit-learn.org/0.18/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html#sklearn.feature_extractionのsklearnパッケージをチェックアウトしてください。 text.TfidfVectorizer –

+0

'joined_ngrams = ['_' ngramsのための '_' join(t)] ' – acushner

+0

それは役に立ちません。私が望むのは、与えられた文の入力を、 'ngrams 'に現れる単語のグループが結合されている文に変換することです。 –

答えて

1

あなたはngramsにあなたの言葉から置換文字列のリストを構築することができます。

replace = [" ".join(x) for x in ngrams] 

をそして、そのリスト内の各要素に対して、str.replaceを使用します。

for r in replace: 
    sentence = sentence.replace(r, r.replace(" ", "_")) 

以上があるかもしれませんそれを行うには一流の方法ですが、それは私にとっては比較的簡潔で分かりやすいようです。

1

Haldeanブラウンの答えは単純ですが、私は、これはより構造的なアプローチだと思う:

ngrams = [("data", "scientist"), 
      ("machine", "learning"), 
      ("c", "+"), 
      ("+", "+"), 
      ("c", "+", "+"), 
      ("research", "and", "development"), 
      ("research", "and")] 
sent = """ 
    i am a data scientist . i do machine learning and c + + but no deep 
    learning . i like research and development 
""" 

ngrams.sort(key=lambda x: -len(x)) 
tokens = sent.split() 

out_ngrams = [] 
i_token = 0 
while i_token < len(tokens): 
    for ngram in ngrams: 
     if ngram == tuple(tokens[i_token : i_token + len(ngram)]): 
      i_token += len(ngram) 
      out_ngrams.append(ngram) 
      break 
    else: 
     out_ngrams.append((tokens[i_token],)) 
     i_token += 1 

print(' '.join('_'.join(ngram) for ngram in out_ngrams)) 

出力:

i am a data_scientist . i do machine_learning and c_+_+ but no deep learning . i like research_and_development 

ngramsソートした後:

[('c', '+', '+'), 
('research', 'and', 'development'), 
('data', 'scientist'), 
('machine', 'learning'), 
('c', '+'), 
('+', '+'), 
('research', 'and')] 

に必要なのより前に("c", "+", "+")を適用してみてください(または、通常、接頭辞よりも前のシーケンスを適用しようとする)。実際には[('c', '+'), ('+', 'a')]のような非貪欲なものが[('c', '+', '+'), ('a',)]よりも望ましいかもしれませんが、それは別の話です。上で

+1

それは動作します、ありがとう! '' ngrams''をセットして、O(len(文))で動くような解決策を見つけようとしていたので、私は立ち往生していたと思います。私はまだそれが可能かもしれないと思うが多分あまりエレガント! –

+0

私は、家族の中の最短のngramを家族(家族は共通の接頭辞を持つngramのリスト)に写像する辞書のようなものによって、O(len(sentence)* len(longest_ngram))で行うことができると思います。 –

0
s = '' 
seq = ("c", "+", "+") 
print(s.join(seq)) 

その他の方法で参加する:何ですか

+0

問題はまったく解決しません。 –

関連する問題