[英]Python, NLP - finding the top document containing given list of words
[英]Python regexes: return a list of words containing a given substring
基於正則表達式的函數f
是什么,給定輸入文本和字符串,返回文本中包含該字符串的所有單詞。 例如:
f("This is just a simple text to test some basic things", "si")
會返回:
["simple", "basic"]
(因為這兩個詞包含子串"si"
)
怎么做?
對於這樣的事情,我不會使用正則表達式,我會使用這樣的東西:
def f(string, match):
string_list = string.split()
match_list = []
for word in string_list:
if match in word:
match_list.append(word)
return match_list
print f("This is just a simple text to test some basic things", "si")
我不相信沒有比我的方法更好的方法來做到這一點,但類似於:
import re
def f(s, pat):
pat = r'(\w*%s\w*)' % pat # Not thrilled about this line
return re.findall(pat, s)
print f("This is just a simple text to test some basic things", "si")
作品:
['simple', 'basic']
這是我對解決方案的嘗試。 我用“”分割輸入字符串,然后嘗試將每個單獨的單詞與模式匹配。 如果找到匹配項,則將該詞添加到結果集中。
import re
def f(str, pat):
matches = list()
str_list = str.split(' ');
for word in str_list:
regex = r'' + re.escape(word)
match = re.search(regex, word)
if match:
matches.append(word)
return matches
print f("This is just a simple text to test some basic things", "si")
import re
def func(s, pat):
pat = r'\b\S*%s\S*\b' % re.escape(pat)
return re.findall(pat, s)
print func("This is just a simple text to test some basic things", "si")
你需要這個 。 \\b
將通過在單詞邊界處切割來僅取出單詞。 \\S
不會選擇任何space
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.