#数据结构

**trie树**

trie树相应的介绍点击链接 https://en.wikipedia.org/wiki/Trie

trie树在python文件中的类型定义

Node定义

#GUI设计界面

```#!/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)
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()

f2 = open(path, 'rb')
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()
'''
按钮布局
'''
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)
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')

window.setWindowTitle('Auto Complete Demo')
window.resize(400, 200)
window.move(400, 100)

edit = TextEdit()
edit.setFontPointSize(40)
edit.setCompleter(cmp)
window.setCentralWidget(edit)
window.show()
sys.exit(app.exec_())

if __name__ == '__main__':
print 'app is running successfully'
main()```

## 基于trie树的具有联想功能的文本编辑器的更多相关文章

1. 基于trie树做一个ac自动机

基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

2. 用trie树实现输入提示功能，输入php函数名，提示php函数

参照刘汝佳的trie树 结构体 #include "stdio.h" #include "stdlib.h" #include "string.h&q ...

3. [转] Trie树详解及其应用

一.知识简介         最近在看字符串算法了,其中字典树.AC自动机和后缀树的应用是最广泛的了,下面将会重点介绍下这几个算法的应用.       字典树(Trie)可以保存一些字符串->值 ...

4. Trie树详解及其应用

一.知识简介        最近在看字符串算法了,其中字典树.AC自动机和后缀树的应用是最广泛的了,下面将会重点介绍下这几个算法的应用.      字典树(Trie)可以保存一些字符串->值的对 ...

5. Trie树也称字典树

Trie树 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 一.Trie树的原理 利用串构建一个字典树,这个字典树保存了串的公共前缀信息, ...

6. LeetCode Implement Trie (Prefix Tree) （实现trie树3个函数：插入，查找，前缀）

题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! class TrieNode { public: TrieNode* chd[]; bool flag; // Initiali ...

7. Trie 树(转)

看了很多 Trie 树的介绍, 这篇讲的最好,简单易懂(特别是代码部分),直接转载:http://www.cnblogs.com/dolphin0520/archive/2011/10/11/2207 ...

8. 【动画】看动画轻松理解「Trie树」

Trie树 Trie这个名字取自“retrieval”,检索,因为Trie可以只用一个前缀便可以在一部字典中找到想要的单词. 虽然发音与「Tree」一致,但为了将这种 字典树 与 普通二叉树 以示区别 ...

9. Trie树（Prefix Tree）介绍

本文用尽量简洁的语言介绍一种树形数据结构 -- Trie树. 一.什么是Trie树 Trie树,又叫字典树.前缀树(Prefix Tree).单词查找树 或 键树,是一种多叉树结构.如下图: 上图是一 ...

## 随机推荐

1. SharePoint 2013 托管导航 无法被开启的解决办法

在阅读了园子中霖雨的一片博文<SharePoint 2013 托管导航及相关配置>之后,非常想尝试一下SharePoint 2013 中的这个新功能,但是我的网站集包括样式是从2010升级 ...

2. ubuntu包管理

3. LoRaWAN协议(五)--OTAA入网方式详述

前言 OTAA(Over-The-Air Activation),是LoRaWAN的一种空中入网方式.当node在上电的时候处于非入网状态时,需要先入网才能和服务器进行通信.其操作就是node发送jo ...

4. 关于SQL递归查询在不同数据库中的实现方法

比如表结构数据如下: Table:Tree ID Name ParentId 1 一级  0 2  二级 1 3  三级 2 4 四级 3 SQL SERVER 2005查询方法: //上查 with ...

5. 关于java IO 过程当中同时读写的问题

今天在写一个linux的java守护进程的时候,无意间就用到了java同时读写的功能. 看错误代码: package cn.sunchuanzhen.main; import java.io.Buff ...

6. Ruby的语法糖

发现Ruby的语法糖好多,比如函数调用,参数列表可以写括号和不写括号.代码块可以用do end 或者 {}.   还有 if,unless后置.等等. 如果看Ruby代码看多了,你会发现,它很多地方的 ...

7. Oracle 12C 新特性之 sqlplus查看History命令

12c里,Oracle推出了 History 命令,这很像 Shell 中的 history ,减少了重敲 SQL ,带来了很多便利. 1. 查看history帮助SQL> help histo ...

8. python2.7.5 安装pip