2017-10-23 2 views
-1

私は基本的なテキストマイニングアプリケーションをやっているので、明確な単語(キーワード)を見つけてこの単語の後にn個の単語だけをキャプチャする必要があります。たとえば、このテキストでは私がキーワードの人口の後に3つの言葉をキャッチしたいと思います:テキストファイル内のキーワードを見つけ、この単語の後にn個の単語をキャッチする

補足の表は、集団 2万人のと地域のために2016年1年間の票に集計59個の詳細テーブルで構成さ以上。これらの補足見積もりは、米国ファクトファインダーと国勢調査局のアプリケーションプログラミングインターフェースを通じて、米国共同体調査と同じ地理的要約レベルで利用可能です。

次のステップは、文字列を分割して番号を見つけることですが、これが解決した点です。私は成功していない別の方法(正規表現など)で試した。どうしたらいいですか?

答えて

2

スプリット言葉にテキスト、次の指標で単語をつかむ、キーワードのインデックスを見つける:あなたが戻って文字列に三つの言葉wanted_wordsのリストを作成したい場合は

text = 'The Supplemental Tables consist of 59 detailed tables tabulated on the 2016 1-year microdata for geographies with populations of 20,000 people or more. These Supplemental Estimates are available through American FactFinder and the Census Bureau’s application programming interface at the same geographic summary levels as those in the American Community Survey.' 
keyword = 'populations' 
words = text.split() 
index = words.index(keyword) 
wanted_words = words[index + 1:index + 4] 

を使用
wanted_text = ' '.join(wanted_words) 
+0

このソリューションは正常に動作します。 – Edison2020

1

nltkライブラリを使用できます。

from nltk.tokenize import word_tokenize 

def sample(string, keyword, n): 
    output = [] 
    word_list = word_tokenize(string.lower()) 
    indices = [i for i, x in enumerate(word_list) if x==keyword] 
    for index in indices: 
     output.append(word_list[index+1:index+n+1]) 
    return output 


>>>print sample(string, 'populations', 3) 
>>>[['of', '20,000', 'people']] 
>>>print sample(string, 'tables', 3) 
>>>[['consist', 'of', '59'], ['tabulated', 'on', 'the']] 
+0

_nlktライブラリを使用すると素晴らしいオプション_、ありがとう! – Edison2020

1

あなたはそれだけで '集団' を見つけて、次の3つを取得言葉

にあなたの文章をこぼしできjieba

jieba.cut

を使用して

1を、それを解決するには、2つの方法を持っています言葉

2こぼれた

raw = 'YOUR_TEXT_CONTENT' 
raw_list = raw.split(' ') 
start = raw_list.index('populations') 
print(raw_list[start:start+4]) 
+0

素晴らしい!どうもありがとう!!! – Edison2020

関連する問題