Created
September 9, 2018 16:47
-
-
Save cpulvermacher/93c0009cdfea5dcc65d1134e037479f3 to your computer and use it in GitHub Desktop.
Get use count for string prefixes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Find some suggestions on which shell commands you actually want an alias for: | |
# cat ~/.bash_history | python3 find_common_prefixes.py | sort -n | |
# cat ~/.zsh_history | cut -d ';' -f 2 | python3 find_common_prefixes.py | sort -n | |
import fileinput | |
class Node(): | |
def __init__(self, text = ''): | |
self.text = text | |
self.use_count = 0 | |
self.children = [] | |
def insert_string(node, s): | |
for part in s.split(' '): | |
matching_node = None | |
for c in node.children: | |
if c.text == part: | |
matching_node = c | |
break | |
if not matching_node: | |
matching_node = Node(part) | |
node.children.append(matching_node) | |
matching_node.use_count += 1 | |
node = matching_node | |
def print_prefix_and_use_count(node, prefix=''): | |
print ('{}\t{}'.format(node.use_count, prefix + ' ' + node.text)) | |
for child in node.children: | |
print_prefix_and_use_count(child, prefix + ' ' + node.text) | |
def main(): | |
trie = Node() | |
for line in fileinput.input(files=('-')): | |
insert_string(trie, line.strip('\n')) | |
print_prefix_and_use_count(trie) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment