基于trie树的具有联想功能的⽂本编辑器
之前的软件设计与开发实践课程中,⾃⼰构思的⼤作业题⽬。做的具有核⼼功能,但是还⽋缺边边⾓⾓的⼩功能和持久化数据结构,先放出来,有机会⼀点点改。github:
数据结构,使⽤过程截图以及源代码如下:
#数据结构
**trie树**
trie树相应的介绍点击链接
trie树在python⽂件中的类型定义
Node定义
#GUI设计界⾯
⾸先,⽤较⼤的⽂本⽂件进⾏训练,完成trie的树的训练。本⽂件夹下提供了⼀个⽂件作为⽰例。
在之后的输⼊会弹出提⽰框
#!/usr/bin/python
# -*- coding: utf-8 -*-
import PyQt4
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import PyQt4.QtCore
import sys
import pickle
print '===================start======================='
class Node:
def __init__(self):
self.value = None
self.children = {} # children is of type {char, Node}
self.fre = 0
self.father = None
def CMP(a, b):
return b.fre - a.fre
class Trie:
def __init__(self):
< = Node()
self.choose = []
self.__OpenCorrect__ = 0
def insert(self, key): # key is of type string
# key should be a low-case string, this must be checked here! node =
for char in key:
if char not in node.children:
child = Node()
node.children[char] = child
child.value = char
child.father = node
node = child
else:
node = node.children[char]
# node.value = key
node.fre += 1
def search(self, key):
node =
for char in key:
if char not in node.children:
return None
else:
node = node.children[char]
return node.value
def display_node(self, node):
if (node.value != None):
print node.value
for char in 'abcdefghijklmnopqrstuvwxyz':
if char in node.children:
self.display_node(node.children[char])
return
def fallback(self, node):
f_char = ''
while node != :
f_char = node.value + f_char
node = node.father
# print f_char
return f_char
def display(self):
self.display_)
def find_node(self, string):
res_node =
for i in string:
res_node = res_node.children[i]
return res_node
def association(self, node):
# 调⽤此函数前应该先将self.choose恢复成空列表
if (node.value != None):
if node.fre > 0:
self.choose.append(node)
for char in 'abcdefghijklmnopqrstuvwxyz':
if char in node.children:
self.association(node.children[char])
def output_association(self, char):
char = str(char).lower()
self.choose = []
result_list = []
self.association(self.find_node(char))
self.choose.sort(cmp=CMP)
if len(self.choose) > 0:
for i in self.choose:
result_list.append(self.fallback(i))
if self.__OpenCorrect__ == 0:
result_list.insert(1, t(char))
# print 'result_list',result_list
return result_list
def correct(self, string):
self.choose = []
p = self.find_node(string[:-1])
self.association(p)
self.choose.sort(cmp=CMP)
if len(self.choose) > 1:
return self.fallback(self.choose[0])
def train(trie, path):
# f = open(')
f = open(path)
word = f.read()
f.close()
word = word.split(' ')
for i in word:
trie.insert(i)
trie = Trie()
trie.__OpenCorrect__ = 0
def save_model(T):
f1 = open("trie.pkl", 'wb')
pickle.dump(T, f1)
f1.close()
def load_model(path):
f2 = open(path, 'rb')
trie = pickle.load(f2)
f2.close()
print '================= END ====================='
class UI(QDialog):
def __init__(self, parent=None):
super(UI, self).__init__(parent)
QSList = QStringList()
# default
# QSList<<'One'<<'Tow'<<'Three'<<'Four'<<'Five'
# instance of Completer class
cmp = Completer(QSList)
global edit
edit = TextEdit()
getsavefilenameedit.setFontPointSize(80)
edit.setCompleter(cmp)
self.setWindowTitle(u"智能⽂本编辑器")
button1 = QPushButton(u"训练模型")
button2 = QPushButton(u"保存⽂本⽂件")
button3 = QPushButton(u"打开⽂本⽂件")
'''
buttons = QushButton()
'''
'''
定义按钮,()内为按钮名称
'''
'''
关联按钮与函数格式同上,触发为clicked()单击,最后⼀个参数为
类内函数
'''
layout = QGridLayout()
layout.addWidget(edit, 0, 1, 1, 5)
layout.addWidget(button1, 2, 1)
layout.addWidget(button2, 2, 3)
layout.addWidget(button3, 2, 5)
'''
按钮布局
'''
self.setLayout(layout)
<()
def center(self):
screen = QDesktopWidget().screenGeometry()
size = ry()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论