【问题标题】:Backtracking/Search Pruning- Combinatorial Word Search in Python回溯/搜索修剪——Python 中的组合词搜索
【发布时间】:2014-10-23 22:03:06
【问题描述】:
entry="Where in the world is Carmen San Diego"
goal=["Where in the", "world is", "Carmen San Diego"]

我正在尝试创建一个过程,该过程将在“条目”中搜索属于“目标”列表成员的词块。我想保留这些子集中的词序。

这是我目前所拥有的。我不确定如何完成此操作,或者我是否以正确的方式接近它。

span=1
words = entry.split(" ")
initial_list= [" ".join(words[i:i+span]) for i in range(0, len(words), span)]
x=len(initial_list)
initial_string= " ".join(initial_list)
def backtrack(A,k):
    if A in goal:
        print
    else:
        while A not in goal:
            k=k-1
            A= " ".join(initial_list[0:k])
            if A in goal:
                print A
                words=A.split(" ")
                firstmatch= [" ".join(words[i:i+span]) for i in range(0, len(words), span)]
                newList = []
                for item in initial_list:
                    if item not in firstmatch:
                        newList.append(item)
                nextchunk=" ".join(newList)             

backtrack(initial_string,x)

目前的输出是这样的:

"Where in the"

期望的输出:

"Where in the"
"world is"
"Carmen San Diego"

我一直在努力寻找合适的算法,我认为它需要回溯或搜索修剪,我不太确定。理想情况下,解决方案适用于任何“条目”和“目标”列表。非常感谢任何 cmets。

【问题讨论】:

  • 您的示例对于理解您要做什么并不是特别有帮助。如果您有entry = "abcabcdefdef"goal = ["ab", "dd", "c"],您希望输出什么?
  • @BrandonHumpert。在那种情况下,我不希望打印任何内容。总的来说,这是一个原型。这个“目标”列表实际上代表了一组成功的 JSON 查询。 “entry”将是用户输入的字符串。我想以我所描述的“回溯”方式将此用户条目分解为多个查询字符串。希望这更清楚。

标签: python search backtracking pruning


【解决方案1】:

这是你想要的吗?

entry="Where in the world is Carmen San Diego"
goal=["Where in the", "world is", "Carmen San Diego"]


for word in goal:
    if word in entry:
        print(word)

它只是搜索每个单词的条目,如果找到它就会打印出来。

如果您想将它们保存到列表或其他内容中,您可以执行以下操作:

entry="Where in the world is Carmen San Diego"
goal=["Where in the", "world is", "Carmen San Diego"]
foundwords = []

for word in goal:
    if word in entry:
        foundwords.append(word)

【讨论】:

  • 感谢您的帮助。不幸的是,这里的“目标”只是我用来制作原型的列表;实际上,它将代表对 api 的成功查询,所以我不能真正循环遍历所有可能成功的查询集
【解决方案2】:

这里有一个想法:将您的目标列表放入树中。在 trie 中查找当前条目字符串的最长匹配前缀,如果找到则将其添加到输出中。

然后在当前条目字符串中找到下一个空格(单词分隔符),将当前条目字符串设置为空格后索引的子字符串,并重复直到为空。

编辑:这里有一些代码。

import string
import datrie

entry="Where in the world is Carmen San Diego"
goal=["Where in the", "world is", "Carmen San Diego"]

dt = datrie.BaseTrie(string.printable)
for i, s in enumerate(goal):
    dt[s] = i

def find_prefix(current_entry):
    try:
        return dt.longest_prefix(current_entry)
    except KeyError:
        return None

def find_matches(entry):
    current_entry = entry

    while(True):
        match = find_prefix(current_entry)
        if match:
            yield match
        space_index = current_entry.find(' ')
        if space_index > 0:
             current_entry = current_entry[space_index + 1:]
        else:
            return

print(list(find_matches(entry)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多