2017-12-30 83 views
-2

私はPandasのテキスト解析にstr.containsを使用しています。 「私の最新のデータジョブがアナリストだった」という文章の場合、「データ」という語の組み合わせが必要です&「アナリスト」同時に、組み合わせに使用される2つの単語の間の単語の数を指定したいここでは "データ"と "アナリスト"の間の2単語です。現在、私は "DataFile.XXX.str.contains( 'job')& DataFile.XXX.str.contains( 'Analyst')を使用して、ジョブアナリスト」。私はstr.contains構文で2つの単語間の単語の数を指定するにはどうすればよい 。事前に おかげPythonテキスト処理(str.contains)

+1

DataFrameのサンプルを共有してください。パンダは本当に便利なライブラリになることができますが、すべてを意味するものではありません。テキスト分析は...おそらく..おそらくそうではありません。そして、より多くの質問をする前に、これを読むのに30分かかります:[ask] –

+0

ようこそ。残念ながら、これはディスカッションフォーラムやチュートリアルサービスではありません。時間をかけて[ask]とそのページの他のリンクを読んでください。 – wwii

+0

私はこの質問に答える方法を知っています。あなたの質問はこのサイトの基準を満たしていないので、私は行きません。 [mcve]を提供し、[ask]も読んでください。 –

答えて

0

あなたがすることはできません。少なくとも、ないシンプルなあるいは標準化された方法で。

「単語」を定義する方法のように、基本的にはlotおそらく想像以上に複雑です。単語の構文解析と語彙近接の両方(例えば、「文s内の互いに距離D以内にある2つの単語は?」)はnatural language processing (NLP)の領域である。 NLPと近接検索は、基本的なPandasやPythonの標準的な文字列処理の一部ではありません。 NLTK, the Natural Language Toolkitのようなものを一般的な方法でこの問題を解決するためにインポートすることができますが、それは全体的な話です。

簡単なアプローチを見てみましょう。まず文字列を単語に分解する方法が必要です。以下は、NLPの基準では荒いですが、単純な例のために動作します:

def parse_words(s): 
    """ 
    Simple parser to grab English words from string. 
    CAUTION: A simplistic solution to a hard problem. 
      Many possibly-important edge- and corner-cases 
      not handled. Just one example: Hyphenated words. 
    """ 
    return re.findall(r"\w+(?:'[st])?", s, re.I) 

例:

>>> parse_words("and don't think this day's last moment won't come ") 
['and', "don't", 'think', 'this', "day's", 'last', 'moment', "won't", 'come'] 

次にあなたが目標単語リスト内のすべてのインデックスを見つけるための方法が必要です発見された:

def list_indices(target, seq): 
    """ 
    Return all indices in seq at which the target is found. 
    """ 
    indices = [] 
    cursor = 0 
    while True: 
     try: 
      index = seq.index(target, cursor) 
     except ValueError: 
      return indices 
     else: 
      indices.append(index) 
      cursor = index + 1 

そして最後にラッパーを意思決定:

def words_within(target_words, s, max_distance, case_insensitive=True): 
    """ 
    Determine if the two target words are within max_distance positiones of one 
    another in the string s. 
    """ 
    if len(target_words) != 2: 
     raise ValueError('must provide 2 target words') 

    # fold case for case insensitivity 
    if case_insensitive: 
     s = s.casefold() 
     target_words = [tw.casefold() for tw in target_words] 
     # for Python 2, replace `casefold` with `lower` 

    # parse words and establish their logical positions in the string 
    words = parse_words(s) 
    target_indices = [list_indices(t, words) for t in target_words] 

    # words not present 
    if not target_indices[0] or not target_indices[1]: 
     return False 

    # compute all combinations of distance for the two words 
    # (there may be more than one occurance of a word in s) 
    actual_distances = [i2 - i1 for i2 in target_indices[1] for i1 in target_indices[0]] 

    # answer whether the minimum observed distance is <= our specified threshold 
    return min(actual_distances) <= max_distance 

はそれでは:

>>> s = "and don't think this day's last moment won't come at last" 
>>> words_within(["THIS", 'last'], s, 2) 
True 

>>> words_within(["think", 'moment'], s, 2) 
False 

唯一のことはやって左パンダにバックアップマップされます。これにより、問題を解決したいどのように基本的に

df = pd.DataFrame({'desc': [ 
    'My latest Data job was an Analyst', 
    'some day my prince will come', 
    'Oh, somewhere over the rainbow bluebirds fly', 
    "Won't you share a common disaster?", 
    'job! rainbow! analyst.' 
]}) 

df['ja2'] = df.desc.apply(lambda x: words_within(["job", 'analyst'], x, 2)) 
df['ja3'] = df.desc.apply(lambda x: words_within(["job", 'analyst'], x, 3)) 

です。覚えておいてください、それは大まかで単純な解決策です。単純に提起された質問の中には、単純に答えられないものもあります。多くの場合、NLPの質問がその中にあります。