java - 如何检查 Twitter 用户声明的位置是否存在?

标签 java twitter lucene geolocation information-retrieval

我正在做一个 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

Red Light District

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/

相关文章:

java - 如何停止方法启动直到 JFrame 关闭

java - Lombok 各种构造函数的注释?

javascript - 使用 javascript 或 jquery [twitter 卡片] 动态添加元标记

jquery - 使用 jQuery 实现类似 Twitter 的输入字段

sharepoint - 我可以在不使用 Lucene 连接器框架的情况下将 Solr 与 Sharepoint 集成吗

elasticsearch - 为什么同一查询中某些结果分数包含 queryWeight,而其他分数则不包含 queryWeight?

java - 使用 twitter4j 和 java 获取所有用户时间线推文

java - 如何让服务器运行我的java程序?

python - 无效或过期的 token 。通过 Tweepy 请求新 token ?

search - 使用Lucene作为反向索引