This repository has been archived by the owner on Jan 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathask_3col.py
executable file
·76 lines (63 loc) · 2.37 KB
/
ask_3col.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
#!/usr/bin/env python
# Copyright (c) 2016 Hanson Robotics, Ltd.
import logging
import json
import requests
import random
from bs4 import BeautifulSoup, NavigableString, Tag
logger = logging.getLogger('hr.chatbot.server.solr_match')
solr_url = 'http://localhost:8983'
def solr3col(text):
# No match, try improving with SOLR
logger.info('SOLR start')
params = {
"fl":"title,body,score",
"indent":"true",
"wt":"json",
"rows":"20"
}
params['q'] = 'title:{}'.format(text)
lucText = requests.get(solr_url+'/solr/3colpattern/select', params=params).text
if len(lucText)>0:
logger.debug('RESPONSE: ' + lucText)
jResp = json.loads(lucText)
if jResp['response']['numFound'] > 0:
for resp in jResp['response']['docs']:
logger.debug(' SOLR pattern: {} {}'.format(resp['body'], resp['title']))
doc = jResp['response']['docs'][0]
query = doc['body'][0]
params['q'] = 'body:{}'.format(query)
templText = requests.get(solr_url+'/solr/3coltemplate/select', params=params).text
if len(templText) > 0:
templResp = json.loads(templText)
if templResp['response']['numFound'] > 0:
templ = templResp['response']['docs'][0]
meaning = templ['body']
candidates = [resp for resp in templResp['response']['docs'] if resp['body'] == meaning]
assert len(meaning) > 0
assert len(candidates) > 0
candidate = random.sample(candidates, 1)[0]
return meaning[0], parse_aiml_text(candidate['title'][0]), candidate['score']
def parse_aiml_text(text):
text = '<p>'+text+'</p>'
soup = BeautifulSoup(text, 'lxml')
tokens = []
try:
for c in soup.p.children:
if isinstance(c, NavigableString):
token = c.string.strip()
if token:
tokens.append(token)
except Exception as ex:
logger.warn(ex)
return text
return ' '.join(tokens)
if __name__ == '__main__':
logging.basicConfig()
import sys
if len(sys.argv) <= 1:
print "Usage {} query [query2] [query3] ...".format(sys.argv[0])
sys.exit(0)
for q in sys.argv[1:]:
print '>>>>', q
print '<<<<', solr3col(q)