| import gradio as gr |
| from collections import defaultdict |
| import jieba |
|
|
| def get_words(): |
| with open('./data.txt', 'r', encoding='utf-8') as f: |
| data = f.read() |
| |
| words = jieba.cut(data) |
|
|
| |
| words_list = list(words) |
| words_list = [word for word in words_list if word.replace(' ', '') not in ['', '\n', ',', '.', '"', 'β', 'β', 'β', 'β', '!', '-', '#', '`', '*']] |
|
|
| return words_list |
|
|
| class TrieNode: |
| def __init__(self): |
| self.children = defaultdict(TrieNode) |
| self.is_end_of_word = False |
| self.frequency = 0 |
|
|
| class Trie: |
| def __init__(self): |
| self.root = TrieNode() |
|
|
| def insert(self, word): |
| node = self.root |
| for char in word: |
| node = node.children[char] |
| node.is_end_of_word = True |
| node.frequency += 1 |
|
|
| def search(self, prefix): |
| node = self.root |
| for char in prefix: |
| if char not in node.children: |
| return [] |
| node = node.children[char] |
| return self._find_words_from_node(node, prefix) |
|
|
| def _find_words_from_node(self, node, prefix): |
| words = [] |
| if node.is_end_of_word: |
| words.append((prefix, node.frequency)) |
| for char, next_node in node.children.items(): |
| words.extend(self._find_words_from_node(next_node, prefix + char)) |
| words.sort(key=lambda x: x[1], reverse=True) |
| return words |
|
|
| |
| def autocomplete(input_text): |
| if ' ' in input_text: |
| input_text = input_text.split(' ')[-1] |
| if len(input_text) == 0: |
| return '' |
| suggestions = trie.search(input_text) |
| |
| formatted_suggestions = "\n".join(s[0] for s in suggestions) |
| |
| return formatted_suggestions if len(formatted_suggestions) > 0 else input_text |
|
|
| if __name__ == '__main__': |
| words = get_words() |
| trie = Trie() |
| for word in words: |
| trie.insert(word) |
|
|
| |
| iface = gr.Interface( |
| fn=autocomplete, |
| inputs=gr.Textbox(lines=1, placeholder="Type something..."), |
| outputs=gr.Textbox(lines=5, label="Suggestions"), |
| live=True, |
| title="String Autocompletion Demo", |
| description="Type a prefix to see autocomplete suggestions based on word frequency." |
| ) |
|
|
| |
| iface.launch() |
|
|