forked from iflyingboots/basement
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_detect.py
More file actions
61 lines (52 loc) · 1.54 KB
/
Copy pathnode_detect.py
File metadata and controls
61 lines (52 loc) · 1.54 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
from smallseg import SEG
from config import *
# seg = SEG()
QNA_ID = 300
SHARE_ID = 301
db = MySQLdb.connect(
host=mysql_host,
user=mysql_username,
db=mysql_database,
charset="utf8"
)
c = db.cursor()
# detect QNA node
def qna_detect(title):
question_marks = set([u'?', u'?'])
return title.strip()[-1] in question_marks or title.strip()[0] == u'求'
#detect share node
def share_detect(title):
share_marks = set([u'发现', u'分享', u'推荐', u'试用'])
return any([i for i in share_marks if title.find(i) > -1])
def city_detect(title):
nodes = set()
for city, node_id in CITIES.items():
if title.find(city) > -1:
nodes.add(node_id)
return nodes
def insert_nodes(topic_id, nodes):
for node_id in nodes:
try:
c.execute(
'INSERT INTO topics_nodes(topic_id, node_id) VALUES(%s,%s)'
% (topic_id, node_id)
)
except Exception, e:
print e
db.commit()
def overall_detect(topic_id, title):
nodes = set()
if qna_detect(title):
nodes.add(QNA_ID)
if share_detect(title):
nodes.add(SHARE_ID)
cities = city_detect(title)
nodes = nodes.union(cities)
insert_nodes(topic_id, nodes)
# if __name__ == '__main__':
# print qna_detect(u'听说国内apple实体店不给保修国外购买的apple产品?')
# print share_detect(u'求推荐一个很棒的音乐网站')
# print city_detect(u'北京哪有好吃的啊?')