如何拆分字符串并将其子字符串与子字符串列表相匹配? - Python

时间:2013-03-19 01:18:19

标签: python string dictionary substring string-matching

我需要在不改变字符顺序的情况下将字符串拆分为所有可能的方式。 我理解这个任务可以被视为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)

1 个答案:

答案 0 :(得分: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