-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexpress.py
64 lines (31 loc) · 986 Bytes
/
express.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
#coding=utf-8
import sqlite3
import jieba
poem_count = {}
poem_base = sqlite3.connect('poems.db')
try:
poem_base.execute('''CREATE TABLE EXPRESS(id INT PRIMARY KEY NOT NULL,express TEXT NOT NULL,count INT NOT NULL);''')
except:
print 'table "EXPRESS" is already exist'
poem_tablet = poem_base.execute("select * from POEMS")
poems = poem_tablet.fetchall()
poem_list = []
for poem in poems:
sentence = poem[1]
result = jieba.lcut(sentence,cut_all=True)
poem_list.extend(result)
print '分词完毕,正在写入数据库...'
for i in poem_list:
if i in poem_count:
poem_count[i] = poem_count[i]+1
else:
poem_count[i] = 1
ident = 0
for key in poem_count:
values = (ident,key,poem_count[key])
poem_base.execute("insert into EXPRESS values(?,?,?)",values)
ident = ident+1
poem_base.execute("delete from EXPRESS where id = 0")
poem_base.commit()
poem_base.close()
print '写入完毕,退出'