私はPandasのテキスト解析にstr.containsを使用しています。 「私の最新のデータジョブがアナリストだった」という文章の場合、「データ」という語の組み合わせが必要です&「アナリスト」同時に、組み合わせに使用される2つの単語の間の単語の数を指定したいここでは "データ"と "アナリスト"の間の2単語です。現在、私は "DataFile.XXX.str.contains( 'job')& DataFile.XXX.str.contains( 'Analyst')を使用して、ジョブアナリスト」。私はstr.contains構文で2つの単語間の単語の数を指定するにはどうすればよい 。事前に おかげPythonテキスト処理(str.contains)
-2
A
答えて
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の質問がその中にあります。
関連する問題
- 1. SQLite3のテキスト処理Pythonのデータベース
- 2. Pythonのテキスト処理:NLTKとpandas
- 3. 検索 - テキスト処理
- 4. LingPipeテキスト処理API
- 5. jQuery.keypress:インスタント・テキスト処理
- 6. パフォーマンスチューニングpowershellテキスト処理
- 7. テキスト処理(改行)
- 8. スピーチテキスト処理 - python
- 9. Pythonマルチスレッドメモ処理
- 10. 処理タイムアウトエラー(Python)
- 11. テキスト処理 - クリアテキストの抽出
- 12. OCRバッチ処理TIFFテキスト
- 13. Python例外処理
- 14. Python SOAPドキュメント処理
- 15. Python ..画像処理
- 16. Python GeoPyエラー処理
- 17. データ前処理Python
- 18. Python例外処理
- 19. Pythonのエラー処理
- 20. パンダ:str.containsとの条件()
- 21. OpenCV 2.4 in python - ビデオ処理
- 22. Python例外構文処理
- 23. Pythonルートの前処理ルートパラメータ
- 24. Pythonでのイベント処理Luigi
- 25. Pythonフォーム処理の問題
- 26. スクリーンショットの画像処理 - Python
- 27. Pythonソケットプログラミング - 例外処理
- 28. 反復処理 - パンダ/ Pythonの
- 29. AWS Lambda Pythonのフォルダ処理
- 30. Pythonのxml処理オプション
DataFrameのサンプルを共有してください。パンダは本当に便利なライブラリになることができますが、すべてを意味するものではありません。テキスト分析は...おそらく..おそらくそうではありません。そして、より多くの質問をする前に、これを読むのに30分かかります:[ask] –
ようこそ。残念ながら、これはディスカッションフォーラムやチュートリアルサービスではありません。時間をかけて[ask]とそのページの他のリンクを読んでください。 – wwii
私はこの質問に答える方法を知っています。あなたの質問はこのサイトの基準を満たしていないので、私は行きません。 [mcve]を提供し、[ask]も読んでください。 –