gpt4 book ai didi

python - 在 python 中使用 bing 或 google API 获取位置坐标

转载 作者:太空宇宙 更新时间:2023-11-03 12:28:35 26 4
gpt4 key购买 nike

这是我的问题。我有一个示例文本文件,我通过抓取各种 html 页面来存储文本数据。此文本包含有关各种事件及其时间和地点的信息。我想获取这些位置的坐标。我不知道如何在 python 中做到这一点。我正在使用 nltk 来识别此示例文本中的命名实体。这是代码:

import nltk

with open('sample.txt', 'r') as f:
sample = f.read()

sentences = nltk.sent_tokenize(sample)
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
chunked_sentences = nltk.batch_ne_chunk(tagged_sentences, binary=True)

#print chunked_sentences
#print tokenized_sentences
#print tagged_sentences

def extract_entity_names(t):
entity_names = []

if hasattr(t, 'node') and t.node:
if t.node == 'NE':
entity_names.append(' '.join([child[0] for child in t]))
else:
for child in t:
entity_names.extend(extract_entity_names(child))

return entity_names

entity_names = []
for tree in chunked_sentences:
# Print results per sentence
# print extract_entity_names(tree)

entity_names.extend(extract_entity_names(tree))

# Print all entity names
#print entity_names

# Print unique entity names
print set(entity_names)

示例文件是这样的:

La bohème at Covent Garden

When: 18 Jan 2013 (various dates) , 7.30pm Where: Covent Garden, London, John Copley's perennially popular Royal Opera production of Puccini's La bohème is revived for the first of two times this season, aptly over the Christmas period. Sir Mark Elder conducts Rolando Villazón as Rodolfo and Maija Kovalevska as Mimì. Mimì meets poet Rodolfo (Dmytro Popov sings the role on 5 and 18 January) one cold Christmas Eve in Paris' Latin Quarter. Fumbling around in the dark after her candle has gone out, they fall in love. Rodolfo lives with three other lads: philosopher Colline (Nahuel di Pierro/Jihoon Kim on 18 January), musician Schaunard (David Bizic) and painter Marcello (Audun Iversen), who loves Musetta (Stefania Dovhan). Both couples break up and the opera ends in tragedy as Rodolfo finds Mimì dying of consumption in a freezing garret.

我想从此文本中获取伦敦考文特花园的坐标。我该怎么做?

最佳答案

自 2013 年 9 月起,Google Maps API v2 no longer works .这是很棒的@jimhark 代码的更新版本,适用于 API v3(我省略了 __main__ 部分):

import urllib
import simplejson

googleGeocodeUrl = 'http://maps.googleapis.com/maps/api/geocode/json?'

def get_coordinates(query, from_sensor=False):
query = query.encode('utf-8')
params = {
'address': query,
'sensor': "true" if from_sensor else "false"
}
url = googleGeocodeUrl + urllib.urlencode(params)
json_response = urllib.urlopen(url)
response = simplejson.loads(json_response.read())
if response['results']:
location = response['results'][0]['geometry']['location']
latitude, longitude = location['lat'], location['lng']
print query, latitude, longitude
else:
latitude, longitude = None, None
print query, "<no results>"
return latitude, longitude

参见 official documentation获取完整的参数列表和其他信息。

关于python - 在 python 中使用 bing 或 google API 获取位置坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com