-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster.py
141 lines (119 loc) · 4.58 KB
/
master.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python3
import sys
from PyQt5.QtSql import QSqlQueryModel, QSqlDatabase, QSqlQuery
from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtCore import QDate, QDateTime, QRegExp, QSortFilterProxyModel, Qt, QTime
from PyQt5.QtWidgets import QApplication, QCheckBox, QComboBox, QGridLayout, QGroupBox, QHBoxLayout, QMessageBox
from PyQt5.QtWidgets import QLineEdit, QTreeView, QVBoxLayout, QPushButton, QWidget, QLabel, QAbstractItemView, QTableView
class MyWin(QWidget):
def __init__(self, mydb):
super(MyWin, self).__init__()
main_lt = QVBoxLayout()
self.view = QTableView()
main_lt.addWidget(self.view)
self.view.setWindowTitle("Table Model (View 1)")
self.view.setSelectionBehavior(QAbstractItemView.SelectRows)
self.mydb = mydb
button = QPushButton("Join")
button.clicked.connect(self.merge)
main_lt.addWidget(button)
button = QPushButton("Exit")
button.clicked.connect(lambda: exit())
main_lt.addWidget(button)
self.setLayout(main_lt)
self.setWindowTitle('QT1')
def set_model(self):
self.view.setModel(self.mydb.get_model())
def merge(self):
ind_list = self.view.selectedIndexes()
out = list(self.view.model().data(i) for i in ind_list)
out = [out[i] for i in range(len(out)) if i % 2 == 0]
if len(out) < 2:
return
self.mydb.merge(out)
mb = QMessageBox()
mb.setText('Merged %s' % str(out))
mb.exec_()
self.set_model()
class MyDB:
def __init__(self):
self.db = None
pass
def merge(self, out):
query = QSqlQuery(db=self.db)
main_name = out[0]
for current_name in out[1:]:
query.exec('update domains set name="%s" where name="%s"' % (main_name, current_name))
def get_model(self):
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('sports.db')
self.db.open()
model = QSqlQueryModel()
query_str = """ select name, count(name) as counts from domains group by name order by counts desc
"""
model.setQuery(query_str, db=self.db)
model.setHeaderData(0, Qt.Horizontal, "Word")
model.setHeaderData(1, Qt.Horizontal, "Count")
return model
def fill_db(self):
query = QSqlQuery(db=self.db)
with open('/home/fenrir/tmp/sha.txt') as book:
text = book.read()
text = text[:int(len(text)/20)]
num_s = 0
num_w = 0
while text:
ends = '.!?'
endn = [text.find(e) for e in ends if e in text]
if endn:
sentence = text[:min(endn) + 1]
text = text[min(endn) + 1:]
else:
sentence = text
text = ''
q4 = 'insert into sentences values (%s, "%s", %s)' % (num_s, sentence, 0)
query.exec_(q4)
words = sentence.split(' ')
for word in words:
q5 = 'insert into words values (%s, "%s", %s)' % (num_w, word, num_s)
query.exec_(q5)
q6 = 'insert into domains values (%s, "%s", %s)' % (num_w, word, num_w)
query.exec_(q6)
num_w += 1
num_s += 1
def create_db(self):
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('sports.db')
if not self.db.open():
return False
query = QSqlQuery(db=self.db)
q1 = ("CREATE TABLE sentences(" +
"id INTEGER, " +
"content TEXT, " +
"page INTEGER, " +
"PRIMARY KEY(id))")
query.exec_(q1)
q2 = ("CREATE TABLE words(" +
"id INTEGER, " +
"word TEXT, " +
"sentence_id INTEGER, " +
"PRIMARY KEY(id), "
"FOREIGN KEY (sentence_id) REFERENCES sentences(id))")
query.exec_(q2)
q3 = ("CREATE TABLE domains(" +
"id INTEGER, " +
"name TEXT, " +
"word_id INTEGER, " +
"PRIMARY KEY(id), " +
"FOREIGN KEY (word_id) REFERENCES words(id))")
query.exec_(q3)
return True
if __name__ == '__main__':
app = QApplication(sys.argv)
base = MyDB()
#base.create_db()
#base.fill_db()
window = MyWin(base)
window.set_model()
window.show()
sys.exit(app.exec_())