[英]How to split a string and match its substrings to a list of substrings? - Python
I need to split a string up into all possible ways without changing the order of the characters. 我需要在不改变字符顺序的情况下将字符串拆分成所有可能的方式。 I understand this task can be seen as tokenization or lemmatization in NLP but i am attempting it from a pure string search perspective which is simpler and more robust.
我理解这个任务可以看作是NLP中的标记化或词形还原,但我从纯粹的字符串搜索角度尝试它,它更简单,更健壮。 Given,
鉴于,
dictionary = ['train','station', 'fire', 'a','trainer','in']
str1 = "firetrainstation"
Task 1: How do i generate all possible substrings such that i get: 任务1:如何生成所有可能的子串,以便我得到:
all_possible_substrings = [['f','iretrainstation'],
['fo','retrainstation'], ...
['firetrainstatio','n'],
['f','i','retrainstation'], ... , ...
['fire','train','station'], ... , ...
['fire','tr','a','instation'], ... , ...
['fire','tr','a','in','station'], ... , ...
['f','i','r','e','t','r','a','i','n','s','t','a','t','i','o','n']
Task 2: Then from all_possible_substring
, how could i check through to see and say that the substring set that contains all elements from the dictionary is the correct output. 任务2:然后从
all_possible_substring
,我如何检查并查看包含字典中所有元素的子字符串集是正确的输出。 The desired output would be a list of substrings from the dictionary that matches the most number of characters from left to right. 所需的输出将是字典中从左到右匹配最多字符数的子字符串列表。 Desired output is where:
期望的输出是:
"".join(desire_substring_list) == str1 and \
[i for i desire_substring_list if in dictionary] == len(desire_substring_list)
#(let's assume, the above condition can be true for any input string since my english
#language dictionary is very big and all my strings are human language
#just written without spaces)
Desired output: 期望的输出:
'fire','train','station'
What have I done? 我做了什么?
For task 1 , i have done this but i know it wouldn't give me all possible whitespace insertions: 对于任务1 ,我已经这样做但我知道它不会给我所有可能的空格插入:
all_possible_substrings.append(" ".join(str1))
I've done this but this only does task 2 : 我已经这样做但这只做任务2 :
import re
seed = ['train','station', 'fire', 'a','trainer','in']
str1 = "firetrainstation"
all_possible_string = [['f','iretrainstation'],
['fo','retrainstation'],
['firetrainstatio','n'],
['f','i','retrainstation'],
['fire','train','station'],
['fire','tr','a','instation'],
['fire','tr','a','in','station'],
['f','i','r','e','t','r','a','i','n','s','t','a','t','i','o','n']]
pattern = re.compile(r'\b(?:' + '|'.join(re.escape(s) for s in seed) + r')\b')
highest_match = ""
for i in all_possible_string:
x = pattern.findall(" ".join(i))
if "".join(x) == str1 and len([i for i in x if i in seed]) == len(x):
print " ".join(x)
For the first part you could write a recursive generator similar to this: 对于第一部分,您可以编写类似于此的递归生成器:
>>> def all_substr(string):
for i in range(len(string)):
if i == len(string) - 1:
yield string
first_part = string[0:i+1]
second_part = string[i+1:]
for j in all_substr(second_part):
yield ','.join([first_part, j])
>>> for x in all_substr('apple'):
print(x)
a,p,p,l,e
a,p,p,le
a,p,pl,e
a,p,ple
a,pp,l,e
a,pp,le
a,ppl,e
a,pple
ap,p,l,e
ap,p,le
ap,pl,e
ap,ple
app,l,e
app,le
appl,e
apple
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.