Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def possible_nexts(word, chunks):
- result = []
- for chunk in chunks:
- if word.startswith(chunk): result.append(chunk)
- return result
- def generate_tree(word, chunks):
- result = []
- nexts = possible_nexts(word, chunks)
- for next in nexts:
- current = [next]
- current.extend(generate_tree(word[len(next):], chunks))
- result.append(current)
- if not nexts:
- if word: return [[False]]
- return result
- def is_valid(tree):
- value, *children = tree
- if value is False: return False
- elif not children: return True
- else: return any([is_valid(child) for child in children])
- def filter_valid(tree):
- value, *children = tree
- return [value] + list(map(filter_valid, [child for child in children if is_valid(child)]))
- def to_tree(word, chunks):
- return filter_valid([word] + generate_tree(word, list(set(chunks))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement