algorithm - Word breaking in python doesn't work -


i have string list of words. want keywords in list exists in string. doesn't work more 1 keyword

ks = ['voices', 'home']  def find_tag(long_string, size, result):     idx, s in enumerate(range(0, size + 1)):         prefix = long_string[0:idx + 1]         if prefix in ks:             if idx + 1 == size:                 result += prefix                 print(result)             find_tag(long_string[idx: size - idx], size - idx, result + prefix + ' ')   find_tag('voices', len('voices'), '')  text = 'voicesandhome' find_tag(text, len(text), '') 

the sample input string 'iliveinlondonandiusedtogotonewyork'

given dictionary ['london', 'new york']

output london , new york

a few comments code:

for idx, s in enumerate(range(0, size + 1)): 

does not make sense, because enumerate(range(0,k+1)) yield (0,0), (1,1), ..., (k,k) (why having same value in idx , s?

your code appears iterative (uses for loop), uses recursion in branch.

usually (for example in moderately simple example), either have dead simple recursive code, or more complex iterative code. appears wrong in code.


depending if keywords can overlap or not, i'd suggest simple , pythonic:

[word word in ks if word.replace(' ','') in text] 

for overlapping keywords, or recursive method non-overlapping keywords:

def keywords(text):     if text == '': return []     k in ks:         if text.startswith(k.replace(' ','')):             return [k]+keywords(text[len(k):])     return keywords(text[1:]) 

e.g.:

>>> text='iliveinlondonandiusedtogotonewyork' >>> ks=['london', 'new york'] >>> keywords(text) ['london', 'new york']  >>> text='foop' >>> ks=['foo','oop'] >>> keywords(text) ['foo'] 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -