Python 如何递归生成多词术语?
假设我有一个字符串:Python 如何递归生成多词术语?,python,recursion,Python,Recursion,假设我有一个字符串:'abcdef'。我想从这个字符串生成一个多单词术语列表 语序很重要。术语'f e d'不应从上述示例中生成 编辑:此外,不应跳过单词“a c”,或“b d f” 我现在拥有的: doc = 'a b c d e f' terms= [] one_before = None two_before = None for word in doc.split(None): terms.append(word) if one_before: terms
'abcdef'
。我想从这个字符串生成一个多单词术语列表
语序很重要。术语'f e d'
不应从上述示例中生成
编辑:此外,不应跳过单词<不应生成代码>“a c”,或“b d f”
我现在拥有的:
doc = 'a b c d e f'
terms= []
one_before = None
two_before = None
for word in doc.split(None):
terms.append(word)
if one_before:
terms.append(' '.join([one_before, word]))
if two_before:
terms.append(' '.join([two_before, one_before, word]))
two_before = one_before
one_before = word
for term in terms:
print term
a
b
a b
c
b c
a b c
d
c d
b c d
e
d e
c d e
f
e f
d e f
打印:
doc = 'a b c d e f'
terms= []
one_before = None
two_before = None
for word in doc.split(None):
terms.append(word)
if one_before:
terms.append(' '.join([one_before, word]))
if two_before:
terms.append(' '.join([two_before, one_before, word]))
two_before = one_before
one_before = word
for term in terms:
print term
a
b
a b
c
b c
a b c
d
c d
b c d
e
d e
c d e
f
e f
d e f
我如何使它成为一个递归函数,这样我就可以给它传递一个变量,每个词的最大字数
应用程序:
doc = 'a b c d e f'
terms= []
one_before = None
two_before = None
for word in doc.split(None):
terms.append(word)
if one_before:
terms.append(' '.join([one_before, word]))
if two_before:
terms.append(' '.join([two_before, one_before, word]))
two_before = one_before
one_before = word
for term in terms:
print term
a
b
a b
c
b c
a b c
d
c d
b c d
e
d e
c d e
f
e f
d e f
我将使用它从HTML文档中的可读文本生成多单词术语。总体目标是对大型语料库(约200万个文档)进行潜在语义分析。这就是为什么保持词序很重要(自然语言处理等等)。我建议您将函数设置为生成器,然后生成所需数量的术语。您需要将
print
更改为yield
(显然,还需要使整个块功能正常)
您也可以看看模块,它对您所做的工作非常有用。为什么要这样做?您可以只使用for循环和。这不是递归的,但我认为它可以满足您的需要
doc = 'a b c d e f'
words = doc.split(None)
max = 3
for index in xrange(len(words)):
for n in xrange(max):
if index + n < len(words):
print ' '.join(words[index:index+n+1])
这是递归函数,其中包含一些解释变量和注释
def find_terms(words, max_words_per_term):
# If there are no words, you've reached the end. Stop.
if len(words) == 0:
return []
# What's the max term length you could generate from the remaining
# words? It's the lesser of max_words_per_term and how many words
# you have left.
max_term_len = min(len(words), max_words_per_term)
# Find all the terms that start with the first word.
initial_terms = [" ".join(words[:i+1]) for i in xrange(max_term_len)]
# Here's the recursion. Find all of the terms in the list
# of all but the first word.
other_terms = find_terms(words[1:], max_words_per_term)
# Now put the two lists of terms together to get the answer.
return initial_terms + other_terms
你要找的是N-gram算法。这将给你[a,ab,b,bc,c,cd,…]。为了简单起见,我用单个字母代替了单词。你的意思是“每个单词的最大可变术语数”吗?因为它在当前的形式下对我来说没有意义。我认为这里真正的问题是,它需要递归才能完成工作吗?这里有递归的要求吗?@SilentGhost,如果我传递函数“max_words=4”,我想返回len(term.split(None))@Dan Coates,不,这不是要求,我觉得递归函数可以比循环更快地执行分析,但经过进一步研究,我发现情况可能并非如此。这是一个很好的建议,但我需要保持秩序。例如:“abc”创建['a','b','abb','c','bc','abc'],但不创建'baa'或'cba'。很抱歉造成混淆,它也不应该跳过单词。“敏捷的棕色狐狸跳过篱笆”博士不应该用“棕色篱笆”这个词。有没有一种方法可以使用itertools来实现这一点?目前我想不出。在我回答后我确实注意到了,但是是的。不过,itertools还是不错的。它应该始终是第一个查看的地方。看起来我必须使用您提供的第一个解决方案。Python不会让函数递归超过999次。我的测试文档大约有1750个单词,而且篇幅较小。这很有意义。递归解决方案很有趣,但并不实用。如果您真的想进行深度递归,可以使用sys.setrecursionlimit增加递归限制。但是这里的迭代解可能更好。