1

我需要在不更改字符顺序的情况下将字符串拆分为所有可能的方式。我知道这个任务可以被视为 NLP 中的标记化或词形还原,但我正在从更简单、更健壮的纯字符串搜索角度尝试它。鉴于,

dictionary = ['train','station', 'fire', 'a','trainer','in']
str1 = "firetrainstation"

任务 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']

任务 2:然后从all_possible_substring,我如何检查以查看并说包含字典中所有元素的子字符串集是正确的输出。所需的输出将是字典中从左到右匹配最多字符的子字符串列表。所需的输出是:

"".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)

期望的输出:

'fire','train','station'

我做了什么?

对于任务 1,我已经这样做了,但我知道它不会给我所有可能的空白插入:

all_possible_substrings.append(" ".join(str1))

我已经这样做了,但这仅执行任务 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)
4

1 回答 1

3

对于第一部分,您可以编写一个类似于此的递归生成器:

>>> 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
于 2013-03-19T04:17:31.287 回答