我有两个清单清单:
text = [['hello this is me'], ['oh you know u']]
phrases = [['this is', 'u'], ['oh you', 'me']]
我需要将使短语中出现的单词组合的文本拆分为单个字符串:
result = [['hello', 'this is', 'me'], ['oh you', 'know', 'u']
我尝试使用zip(),但它会连续遍历列表,而我需要检查每个列表。我也尝试过find()方法,但是从本示例中,它还将找到所有字母“ u”并使其成为字符串(就像单词“ you”一样,使其变为“ yo”,“ u”)。我希望replace()在用列表替换字符串时也能起作用,因为它会让我做类似的事情:
for line in text:
line = line.replace('this is', ['this is'])
但是尝试一切,在这种情况下我仍然找不到适合我的任何东西。你能帮我吗?
您能否澄清一下:
给定文字pack my box with five dozen liquor jugs
和短语five dozen
结果应该是:
(1)
['pack', 'my', 'box', 'with', 'five dozen', 'liquor', 'jugs']
要么
(2)
['pack my box with', 'five dozen', 'liquor jugs']
谢谢!
在下面的代码(当前实现选项1)中,每个文本和短语都转换为Python单词列表,['this', 'is', 'an', 'example']
从而防止在单词内匹配“ u”。
文本的所有可能子短语均由生成compile_subphrases()
。较长的短语(更多的单词)会首先生成,因此在较短的短语之前会被匹配。'five dozen jugs'
总是优先于'five dozen'
或匹配'five'
。
短语和子短语使用列表切片进行比较,大致如下:
text = ['five', 'dozen', 'liquor', 'jugs']
phrase = ['liquor', 'jugs']
if text[2:3] == phrase:
print('matched')
使用此方法比较短语,脚本将遍历原始文本,并用挑选出的短语重写它。
texts = [['hello this is me'], ['oh you know u']]
phrases_to_match = [['this is', 'u'], ['oh you', 'me']]
from itertools import chain
def flatten(list_of_lists):
return list(chain(*list_of_lists))
def compile_subphrases(text, minwords=1, include_self=True):
words = text.split()
text_length = len(words)
max_phrase_length = text_length if include_self else text_length - 1
# NOTE: longest phrases first
for phrase_length in range(max_phrase_length + 1, minwords - 1, -1):
n_length_phrases = (' '.join(words[r:r + phrase_length])
for r in range(text_length - phrase_length + 1))
yield from n_length_phrases
def match_sublist(mainlist, sublist, i):
if i + len(sublist) > len(mainlist):
return False
return sublist == mainlist[i:i + len(sublist)]
phrases_to_match = list(flatten(phrases_to_match))
texts = list(flatten(texts))
results = []
for raw_text in texts:
print(f"Raw text: '{raw_text}'")
matched_phrases = [
subphrase.split()
for subphrase
in compile_subphrases(raw_text)
if subphrase in phrases_to_match
]
phrasal_text = []
index = 0
text_words = raw_text.split()
while index < len(text_words):
for matched_phrase in matched_phrases:
if match_sublist(text_words, matched_phrase, index):
phrasal_text.append(' '.join(matched_phrase))
index += len(matched_phrase)
break
else:
phrasal_text.append(text_words[index])
index += 1
results.append(phrasal_text)
print(f'Phrases to match: {phrases_to_match}')
print(f"Results: {results}")
结果:
$python3 main.py
Raw text: 'hello this is me'
Raw text: 'oh you know u'
Phrases to match: ['this is', 'u', 'oh you', 'me']
Results: [['hello', 'this is', 'me'], ['oh you', 'know', 'u']]
要使用较大的数据集测试此答案和其他答案,请在代码开头尝试一下。它在一个长句子上生成100多种变体,以模拟100多种文本。
from itertools import chain, combinations
import random
#texts = [['hello this is me'], ['oh you know u']]
theme = ' '.join([
'pack my box with five dozen liquor jugs said',
'the quick brown fox as he jumped over the lazy dog'
])
variations = list([
' '.join(combination)
for combination
in combinations(theme.split(), 5)
])
texts = random.choices(variations, k=500)
#phrases_to_match = [['this is', 'u'], ['oh you', 'me']]
phrases_to_match = [
['pack my box', 'quick brown', 'the quick', 'brown fox'],
['jumped over', 'lazy dog'],
['five dozen', 'liquor', 'jugs']
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句