def main(): options = cliargs.get_options(args) if options['input'] is '-': input_handle = sys.stdin else: try: input_handle = open(options['input'], 'rb') except: die("Couldn't open file '{}'".format(options['input'])) if options['output'] is '-': output_handle = sys.stdout else: try: output_handle = open(options['output'], 'wb') except: die("Couldn't write to file '{}'".format(options['output'])) text = input_handle.read() # cProfile.run('locations = geodict_lib.find_locations_in_text(text)') locations = geodict_lib.find_locations_in_text(text) output_string = '' if options['format'].lower() == 'json': output_string = json.dumps(locations) output_handle.write(output_string) elif format.lower() == 'text': for location in locations: found_tokens = location['found_tokens'] start_index = found_tokens[0]['start_index'] end_index = found_tokens[len(found_tokens) - 1]['end_index'] output_string += text[start_index:(end_index + 1)] output_string += "\n" output_handle.write(output_string) elif options['format'].lower() == 'csv': writer = csv.writer(output_handle) writer.writerow(['location', 'type', 'lat', 'lon']) for location in locations: found_tokens = location['found_tokens'] start_index = found_tokens[0]['start_index'] end_index = found_tokens[len(found_tokens) - 1]['end_index'] writer.writerow([ text[start_index:(end_index + 1)], found_tokens[0]['type'].lower(), found_tokens[0]['lat'], found_tokens[0]['lon'] ]) else: print("Unknown output format '{}'".format(format)) exit()
def geo_endpoint(): query=session['last_query']['query'] url='http://localhost:9200/dossiers/_search' q = { "fields" : ["file"], "query" : { "term" : { "file" : query } } } #r=requests.post(url,data=json.dumps(q)) r=es.search(body=q,index=DEFAULT_INDEX) data=r locations=[] for hit in data['hits']['hits']: for location in geodict_lib.find_locations_in_text(re.sub('\s', ' ', str(hit['fields']['file']))): for token in location['found_tokens']: locations.append({'lat':token['lat'],'lon':token['lon'],'name':token['matched_string']}) #geo=map(lambda x: x['found_tokens']) return json.dumps(locations)
def get(self): text = self.get_argument("text") locations = geodict_lib.find_locations_in_text(text) output_string = json.dumps(locations) self.write(output_string)
except: die("Couldn't open file '" + input + "'") if output is "-": output_handle = sys.stdout else: try: output_handle = open(output, "wb") except: die("Couldn't write to file '" + output + "'") text = input_handle.read() # cProfile.run('locations = geodict_lib.find_locations_in_text(text)') locations = geodict_lib.find_locations_in_text(text) output_string = "" if format.lower() == "json": output_string = json.dumps(locations) output_handle.write(output_string) elif format.lower() == "text": for location in locations: found_tokens = location["found_tokens"] start_index = found_tokens[0]["start_index"] end_index = found_tokens[len(found_tokens) - 1]["end_index"] output_string += text[start_index : (end_index + 1)] output_string += "\n" output_handle.write(output_string) elif format.lower() == "csv": writer = csv.writer(output_handle)
except: die("Couldn't open file '" + input + "'") if output is '-': output_handle = sys.stdout else: try: output_handle = open(output, 'wb') except: die("Couldn't write to file '" + output + "'") text = input_handle.read() #cProfile.run('locations = geodict_lib.find_locations_in_text(text)') locations = geodict_lib.find_locations_in_text(text) output_string = '' if format.lower() == 'json': output_string = json.dumps(locations) output_handle.write(output_string) elif format.lower() == 'text': for location in locations: found_tokens = location['found_tokens'] start_index = found_tokens[0]['start_index'] end_index = found_tokens[len(found_tokens) - 1]['end_index'] output_string += text[start_index:(end_index + 1)] output_string += "\n" output_handle.write(output_string) elif format.lower() == 'csv': writer = csv.writer(output_handle)