基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目。做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改。github:https://github.com/chuxiuhong/smarteditor
数据结构,使用过程截图以及源代码如下:

#数据结构
**trie树**
trie树相应的介绍点击链接 https://en.wikipedia.org/wiki/Trie
trie树在python文件中的类型定义
Node定义
#GUI设计界面
首先,用较大的文本文件进行训练,完成trie的树的训练。本文件夹下提供了一个big2.txt文件作为示例。

在之后的输入会弹出提示框
#!/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):
self.root = 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 = self.root
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 = self.root
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 != self.root:
f_char = node.value + f_char
node = node.father
# print f_char
return f_char def display(self):
self.display_node(self.root) def find_node(self, string):
res_node = self.root
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, self.correct(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(r'big2.txt')
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()
edit.setFontPointSize(80)
edit.setCompleter(cmp)
self.setWindowTitle(u"智能文本编辑器")
button1 = QPushButton(u"训练模型")
button2 = QPushButton(u"保存文本文件")
button3 = QPushButton(u"打开文本文件")
'''
buttons = QushButton()
'''
'''
定义按钮,()内为按钮名称
'''
self.connect(button1, SIGNAL("clicked()"), self.get_file)
self.connect(button2, SIGNAL("clicked()"), self.func2)
self.connect(button3, SIGNAL("clicked()"), self.func3)
'''
关联按钮与函数格式同上,触发为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)
self.center() def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) def get_file(self):
s = QFileDialog.getOpenFileName(self, "Open file dialog", "/", "TXT Files(*.txt)")
train(trie, s) def func2(self):
s = QFileDialog.getSaveFileName(self, "文件保存", "C:/", "All Files (*);Text Files (*.txt)")
f = open(s, 'w')
f.write(edit.toPlainText())
f.close() def func3(self):
s = QFileDialog.getOpenFileName(self, "Open file dialog", "/", "TXT Files(*.txt)")
f = open(s)
edit.setText(PyQt4.QtCore.QString(f.read()))
f.close() class TextEdit(QTextEdit):
def __init__(self, parent=None):
super(TextEdit, self).__init__(parent)
self.cmp = None
self.p = ''
self.count = 0 def setCompleter(self, completer):
if self.cmp:
self.disconnect(self.cmp, 0, 0)
self.cmp = completer
if (not self.cmp):
return
self.cmp.setWidget(self)
self.cmp.setCompletionMode(QCompleter.PopupCompletion)
self.cmp.setCaseSensitivity(Qt.CaseInsensitive)
self.connect(self.cmp, SIGNAL('activated(QString)'), self.insertCompletion) def completer(self):
return self.cmp def insertCompletion(self, string): # get cursor position
tc = self.textCursor()
# selectd ranges
tc.movePosition(QTextCursor.StartOfWord, QTextCursor.KeepAnchor) # replace selected ranges
tc.insertText(string)
self.p += str(string)
# set cursor pos back to original pos
self.setTextCursor(tc) def textUnderCursor(self):
tc = self.textCursor()
tc.select(QTextCursor.WordUnderCursor)
return tc.selectedText() def keyPressEvent(self, e):
'''
if e.key() != Qt.Key_Backspace:
self.p = self.p + e.text()
self.count+=1
print 'yes'
else:
self.p = self.p[:-1]
self.count-=1
'''
print 'p ', self.p
print 'pressed >> ', e.text()
if (self.cmp and self.cmp.popup().isVisible()):
if e.key() in (Qt.Key_Enter, Qt.Key_Return, Qt.Key_Escape, Qt.Key_Tab, Qt.Key_Backtab):
e.ignore()
return isShortcut = ((e.modifiers() & Qt.ControlModifier) and e.key() == Qt.Key_E)
if (not self.cmp or not isShortcut):
super(TextEdit, self).keyPressEvent(e) ctrlOrShift = e.modifiers() & (Qt.ControlModifier | Qt.ShiftModifier)
if (not self.cmp or (ctrlOrShift and e.text().isEmpty())):
return eow = QString("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")
hasModifier = (e.modifiers() != Qt.NoModifier) and not ctrlOrShift
completionPrefix = self.textUnderCursor()
# hide popup while matching invalid cases
if (not isShortcut and (hasModifier or e.text().isEmpty() or completionPrefix.length() < 1
or eow.contains(e.text().right(1)))):
self.cmp.popup().hide()
return self.cmp.update(completionPrefix)
self.cmp.popup().setCurrentIndex(self.cmp.completionModel().index(0, 0)) cr = self.cursorRect()
cr.setWidth(self.cmp.popup().sizeHintForColumn(0)
+ self.cmp.popup().verticalScrollBar().sizeHint().width())
word = self.p.split(' ')[:-1]
self.p = ''
for k in xrange(len(word)):
self.p += word[k]
print 'self.p=', self.p
self.cmp.complete(cr) class Completer(QCompleter):
def __init__(self, stringlist, parent=None):
super(Completer, self).__init__(parent)
self.stringlist = stringlist
self.setModel(QStringListModel()) # update function will trigger while the text has been modified
def update(self, completionText):
# generate a new QStringList instance
qsList = QStringList() # generate hint lists which returns by customatic definitions
newList = genMyStrList(completionText)
for item in newList:
qsList.append(item) self.stringlist = qsList
# filteredList = self.stringlist.filter(completionText, Qt.CaseInsensitive)
self.model().setStringList(self.stringlist)
self.popup().setCurrentIndex(self.model().index(0, 0)) # the function below defined a way to generate a string list
def genMyStrList(key):
my_str_list = trie.output_association(key)
return my_str_list def main():
app = QApplication(sys.argv)
app.processEvents()
form = UI()
form.show()
# splash.finish(form)
app.exec_()
window = QMainWindow()
window.statusBar().showMessage('Yuan Ziqi')
menubar = window.menuBar window.setWindowTitle('Auto Complete Demo')
window.resize(400, 200)
window.move(400, 100) edit = TextEdit()
edit.setFontPointSize(40)
edit.setCompleter(cmp)
# bt = addbutton()
# window.addDockWidget(bt)
window.setCentralWidget(edit)
window.show()
sys.exit(app.exec_()) if __name__ == '__main__':
print 'app is running successfully'
main()
基于trie树的具有联想功能的文本编辑器的更多相关文章
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 用trie树实现输入提示功能,输入php函数名,提示php函数
参照刘汝佳的trie树 结构体 #include "stdio.h" #include "stdlib.h" #include "string.h&q ...
- [转] Trie树详解及其应用
一.知识简介 最近在看字符串算法了,其中字典树.AC自动机和后缀树的应用是最广泛的了,下面将会重点介绍下这几个算法的应用. 字典树(Trie)可以保存一些字符串->值 ...
- Trie树详解及其应用
一.知识简介 最近在看字符串算法了,其中字典树.AC自动机和后缀树的应用是最广泛的了,下面将会重点介绍下这几个算法的应用. 字典树(Trie)可以保存一些字符串->值的对 ...
- Trie树也称字典树
Trie树 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 一.Trie树的原理 利用串构建一个字典树,这个字典树保存了串的公共前缀信息, ...
- LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)
题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! class TrieNode { public: TrieNode* chd[]; bool flag; // Initiali ...
- Trie 树(转)
看了很多 Trie 树的介绍, 这篇讲的最好,简单易懂(特别是代码部分),直接转载:http://www.cnblogs.com/dolphin0520/archive/2011/10/11/2207 ...
- 【动画】看动画轻松理解「Trie树」
Trie树 Trie这个名字取自“retrieval”,检索,因为Trie可以只用一个前缀便可以在一部字典中找到想要的单词. 虽然发音与「Tree」一致,但为了将这种 字典树 与 普通二叉树 以示区别 ...
- Trie树(Prefix Tree)介绍
本文用尽量简洁的语言介绍一种树形数据结构 -- Trie树. 一.什么是Trie树 Trie树,又叫字典树.前缀树(Prefix Tree).单词查找树 或 键树,是一种多叉树结构.如下图: 上图是一 ...
随机推荐
- 04.LoT.UI 前后台通用框架分解系列之——轻巧的弹出框
LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...
- PHP赋值运算
1. 赋值运算:= ,意思是右边表达式的值赋给左边的运算数. $int1=10; $int1=$int1-6; //$int1=4 echo $int1,"<br>"; ...
- Android和JavaScript相互调用的方法
转载地址:http://www.jb51.net/article/77206.htm 这篇文章主要介绍了Android和JavaScript相互调用的方法,实例分析了Android的WebView执行 ...
- 使用mybatis-generator在自动生成Model类和Mapper文件
使用mybatis-generator插件可以很轻松的实现mybatis的逆向工程,即,能通过表结构自动生成对应的java类及mapper文件,可以大大提高工作效率,并且它提供了很多自定义的设置可以应 ...
- BPM配置故事之案例7-公式计算
行政主管发来邮件.要求物资明细表增加"单价""总价"."单价"由其审批时填写,"总价"根据"单价"与 ...
- 【转】组件化的Web王国
本文由 埃姆杰 翻译.未经许可,禁止转载!英文出处:Future Insights. 内容提要 使用许多独立组件构建应用程序的想法并不新鲜.Web Component的出现,是重新回顾基于组件的应用程 ...
- RMS:Microsoft Office检测到您的信息权限管理配置有问题。有关详细信息,请与管理员联系。(转)
原文:https://zhidao.baidu.com/question/435088233.html RMS有两种方式: 1.使用微软的服务器,这个是连接到微软的服务器上面做权限控制,在今年5月份之 ...
- 数据分布转换:非正态 -> 正态
来源:丁香园论坛:SPSS上的把非正态分布数据转换为正态分布数据 一楼 可以应用变量变换的方法,将不服从正态分布的资料转化为非正态分布或近似正态分布.常用的变量变换方法有对数变换.平方根变换.倒数变换 ...
- 如何让spring mvc web应用启动时就执行特定处理
Asp.Net的应用中通过根目录下的Global.asax,在Application_Start方法中做一些初始化操作,比如:预先加载缓存项对网站热点数据进行预热,获取一些远程的配置信息等等. Spr ...
- caffe的python接口学习(7):绘制loss和accuracy曲线
使用python接口来运行caffe程序,主要的原因是python非常容易可视化.所以不推荐大家在命令行下面运行python程序.如果非要在命令行下面运行,还不如直接用 c++算了. 推荐使用jupy ...