forked from danohu/apihackday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities.py
executable file
·37 lines (32 loc) · 1.04 KB
/
entities.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
try:
from doh.private import KEY_CALAIS
except ImportError:
from private import KEY_CALAIS
from calais import Calais
calais = Calais(KEY_CALAIS, submitter="python-calais demo")
def places_from_text(text):
place_types = ('City', 'Continent', 'Country', 'ProvinceOrState')
result = calais.analyze(text)
places = []
for item in result.entities:
if item['_type'] in place_types:
places.append(item)
return places
def placelocations(places):
return [placelocation(place) for place in places]
def context(place):
instance = place['instances'][0]
return '%s <b>%s</b> %s' % (instance['prefix'], instance['exact'], instance['suffix'])
def placelocation(place):
try:
latitude = place['resolutions'][0]['latitude']
longitude = place['resolutions'][0]['longitude']
except KeyError:
latitude = None
longitude = None
return {
'name' : place['name'],
'latitude' : latitude,
'longitude' : longitude,
'context' : context(place),
}