-
Notifications
You must be signed in to change notification settings - Fork 0
/
dossier.py
281 lines (225 loc) · 8.83 KB
/
dossier.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import json
import time
import requests
import collections
from alchemyapi import AlchemyAPI
from iodpython.iodindex import IODClient
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
ALCHEMYAPI_KEY = os.environ["DOSSIER_ALCHEMY_KEY"]
ALCHEMY_RELEVANCE_THRESHOLD = 0.7
alchemyapi = AlchemyAPI()
client = IODClient("http://api.idolondemand.com/", os.environ["DOSSIER_IDOL_KEY"])
index = client.getIndex("conversations")
cardIndex = client.getIndex("cards")
#index a conversation
def dossierConversation(transcript):
information = extractInformation(transcript)
if "name" in information:
title = "Conversation with " + information["name"]
addCardToIndex(information)
else:
information["name"] = ""
title = "Conversation"
preprocessedTranscript = preprocess(transcript)
concepts = getTopicsFromConversation(preprocessedTranscript)
segmentedTranscript = generateSegmentedTranscript(transcript, information)
indexConversation(segmentedTranscript, information["name"], "Conversation with " + information["name"])
return information
def addCardToIndex(information):
cards = getCard(information["name"])
doc = { "content": str(information), "title": information["name"], "reference": information["name"] }
result = cardIndex.addDoc(doc, async=True).json()
return result
#title should contain persons name
def indexConversation(transcript, person_id, title):
topics = getTopicsFromConversation(transcript)
doc = { "content": transcript, "title": title, "reference": str(time.time()),
"topics": topics, "date": time.strftime("%m/%d/%y"), "person_id": str(person_id) }
print doc
result = index.addDoc(doc, async=True).json()
return result
#queries
#all conversations
def getConversations():
r = client.post('querytextindex', {'indexes': 'conversations', 'text':"*", 'print': 'all'})
return r.json()
def getConversationAbout(topic):
r = client.post('querytextindex', {'indexes': 'conversations', 'text':topic, 'print': 'all'})
return r.json()
def getConversationWith(person_id):
fieldText = 'MATCH{' + person_id + '}:person_id'
r = client.post('querytextindex', {'indexes': 'conversations', 'text':'*', 'field_text': fieldText, 'print': 'all'})
return r.json()
#all cards
def getCards():
r = client.post('querytextindex', {'indexes': 'cards', 'text':'*', 'print': 'all'})
return r.json()
#card of person_id
def getCard(person_id):
query = person_id + ":title"
r = client.post('querytextindex', {'indexes': 'cards', 'text':query, 'print': 'all'})
return r.json()
### indexer
def generateSegmentedTranscript(transcript, information):
segmentedTranscript = ""
meName = "Me: "
if information["name"]:
youName = information["name"] + ": "
else:
youName = "You: "
for segment in transcript:
if segment.keys()[0] == "me":
segmentedTranscript += meName
else:
segmentedTranscript += youName
segmentedTranscript += segment.values()[0]+"\n"
return convert(segmentedTranscript)
def getTopicsFromConversation(transcript):
concepts = []
response = alchemyapi.concepts("text", transcript)
response = convert(response)
if response['status'] == 'OK':
for concept in response['concepts']:
if float(concept['relevance']) > ALCHEMY_RELEVANCE_THRESHOLD:
concepts.append(concept['text'])
else:
print('Error in concept tagging call: ', response['statusInfo'])
return -1
return concepts
#remove all segmentation
def preprocess(transcript):
processedTranscript = ""
for segment in transcript:
processedTranscript += segment.values()[0]+".\n"
return processedTranscript
def getEntities(preprocessedTranscript):
response = alchemyapi.entities('text', preprocessedTranscript)
response = convert(response)
entities = response['entities']
return entities
#returns the text of valid entities
def entityOfTypeInSegment(types, segment, entities):
validEntities = []
for entity in entities:
if entity['type'] in types:
if entity['text'] not in validEntities:
validEntities.append(entity['text'])
return validEntities
def extractInformation(transcript):
preprocessedTranscript = preprocess(transcript)
entities = getEntities(preprocessedTranscript)
information = {}
#prepare keywords
response = alchemyapi.keywords('text', preprocessedTranscript, {'sentiment': 1})
response = convert(response)
keywords = []
for kw in response["keywords"]:
if float(kw["relevance"]) > 0.5:
keywords.append(kw)
for segment in transcript:
if 'you' in segment:
segment = segment["you"]
#name
element = listElementInString(["I'm ", "I am", "My name is ", "my name is ",
"people call me ", "call me ", "Call me "], segment)
if element:
people = entityOfTypeInSegment(['Person'], segment, entities)
firstWord = segment.split(element)[1].split()[0]
name = entityTextForWord(firstWord, people)
if name:
information["name"] = name
#school
element = listElementInString(["I go to ", "I study at ", "I attend ", "I went to ",
"I studied at ", "I attended ", "I graduated from ", "I go to the ", "I go to a ",
"I study at the ", "I study at a ", "I attend a ", "I attend the ", "I graduated from the ",
"I graduated from a ", "I attended a ", "I attended the ", "I went to ", "I went to the ",
"I went to a ", "I studied at the ", "I studied at a "], segment)
if element:
schools = entityOfTypeInSegment(['Organization'], segment, entities)
firstWord = segment.split(element)[1].split()[0]
school = entityTextForWord(firstWord, schools)
if school:
information["school"] = school
#employer
element = listElementInString(["I work at ", "I work for ", "I work at a", "I work for a ",
"I work at the ", "I work for the ", "I work in ", "I work in the ", "I work in a"], segment)
if element:
employers = entityOfTypeInSegment(['Company', 'Organization'], segment, entities)
firstWord = segment.split(element)[1].split()[0]
employer = entityTextForWord(firstWord, employers)
if employer:
information["employer"] = employer
restOfSentence = segment.split(element)[1].replace("?", ".").replace("!", ".").split('.')[0]
jobs = entityOfTypeInSegment(['JobTitle'], restOfSentence, entities)
if jobs and "job" not in information:
information["job"] = jobs[0]
#jobtitle
element = listElementInString(["I work as a", "I work as an ", "I work as, ", "I work as the " "I am a ",
"I am an ", "I am the ", "I am ", "I'm", "I'm a ", "I'm an "], segment)
if element:
jobs = entityOfTypeInSegment(['JobTitle'], segment, entities)
firstWord = segment.split(element)[1].split()[0]
job = entityTextForWord(firstWord, jobs)
if job:
information["job"] = job
restOfSentence = segment.split(element)[1].replace("?", ".").replace("!", ".").split('.')[0]
employers = entityOfTypeInSegment(['Company', 'Organization'], restOfSentence, entities)
if employers and "employer" not in information:
information["employer"] = employers[0]
#hometown/homecountry
element = listElementInString(["I went to school in ", "I spent my childhood in ",
"I'm from ", "I am from ", "I lived in", "I live in ", "I grew up in "], segment)
if element:
homes = entityOfTypeInSegment(['City', 'Country', 'StateOrCounty'], segment, entities)
firstWord = segment.split(element)[1].split()[0]
home = entityTextForWord(firstWord, homes)
if home:
information["home"] = home
#extract what I like and hate
keywordsInSegment = findKeywordsInSegment([k["text"] for k in keywords], segment)
for kwsgm in keywordsInSegment:
for keyword in keywords:
if kwsgm == keyword["text"]:
if keyword["sentiment"]["type"] == "positive":
if float(keyword["sentiment"]["score"]) < 0.7:
continue
if "interests" in information and kwsgm not in information["interests"]:
information["interests"].append(kwsgm)
else:
information["interests"] = [kwsgm]
elif keyword["sentiment"]["type"] == "negative":
if float(keyword["sentiment"]["score"]) > -0.7:
continue
if "dislikes" in information and kwsgm not in information["dislikes"]:
information["dislikes"].append(kwsgm)
else:
information["dislikes"] = [kwsgm]
return information
def entityTextForWord(word, entities):
for entity in entities:
if word in entity:
return entity
return False
def listElementInString(list, string):
for element in list:
if element in string:
return element
return False
def findKeywordsInSegment(keywords, segment):
keywordsInSegment = []
for keyword in keywords:
if keyword in segment:
keywordsInSegment.append(keyword)
return keywordsInSegment
def convert(data):
if isinstance(data, basestring):
return str(data)
elif isinstance(data, collections.Mapping):
return dict(map(convert, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(convert, data))
else:
return data