我正在做一个 Twitter 爬虫,并使用 Lucene 在其上构建了一个搜索引擎。由于许多用户提交的位置不存在(例如“在我的厨房”、“仙境”、“从洛杉矶到巴黎”...),我认为我应该根据用户的位置检查要索引的用户,以便通过位置搜索使他们更容易到达。我通过对英语推文进行采样来检索用户(使用 TwitterStream.sample("en"))。
我的第一个想法是从一些网站下载世界上所有城市并检查是否有匹配。然而,这种方法存在一个问题:很难找到包含世界上所有城市的所有可能语言拼写的文档。事实上,用户可以用英语或他自己的语言提交他的城市(或国家)的名称。
最佳答案
您需要使用地理编码google maps , yandex maps .
I'm facing the fact that the first link tells google API look for cities in USA by default. So...if a user says he's in "Paris", google API will response me NO_REPONSE
I have read the first link with much attention and the second link with less attention, because the latter seems to be useful just for javascript application (I'm doing all in java).
没有。这是不正确的。您可以通过HTTP请求获取信息,引用HTTP request parameters .
yandex maps 的小代码片段使用apache http client
private void request(String geocode) throws IOException {
HttpResponse response = Request.Post(SEARCH_URL).version(HttpVersion.HTTP_1_1)
.bodyForm(createForm(geocode).build(), Charsets.UTF_8).useExpectContinue()
.connectTimeout(CONNECTION_TIMEOUT_MILS)
.socketTimeout(CONNECTION_TIMEOUT_MILS)
.execute().returnResponse();
assertStatus(response, geocode);
getCoordinatesFromResponse(response, geocode);
}
private Form createForm(String geocode) {
return Form.form().add("format", "json").add("results", "1").add("geocode", geocode);
}
private void assertStatus(HttpResponse response, String requestString) {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= ERROR_STATUS_MIN) {
throw new RuntimeException(String.format(
"Error sending request '%s' to the map service, server response: %s",
requestString, response));
}
}
关于java - 如何检查 Twitter 用户声明的位置是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35314987/