Making WordPress.org


Ignore:
Timestamp:
03/07/2017 01:28:35 AM (10 years ago)
Author:
iandunn
Message:

Events API: Extract city names from multi-word inputs to improve matching

This allows inputs like "Portland Maine" or "Sao Paulo Brazil" to successfully match a location.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/index.php

    r5099 r5100  
    140140 * @param string $country_code
    141141 *
    142  * @return false|array false on failure; an array on success
     142 * @return false|object false on failure; an object on success
    143143 */
    144144function guess_location_from_city( $location_name, $timezone, $country_code ) {
    145145        $guess = guess_location_from_geonames( $location_name, $timezone, $country_code );
    146146        $location_word_count = str_word_count( $location_name );
     147        $location_name_parts = explode( ' ', $location_name );
     148
     149        /*
     150         * Multi-word queries may contain cities, regions, and countries, so try to extract just the city
     151         */
     152        if ( ! $guess && $location_word_count >= 2 ) {
     153                // Catch input like "Portland Maine"
     154                $guess = guess_location_from_geonames( $location_name_parts[0], $timezone, $country_code );
     155        }
     156
     157        if ( ! $guess && $location_word_count >= 3 ) {
     158                // Catch input like "Sao Paulo Brazil"
     159                $city_name = sprintf( '%s %s', $location_name_parts[0], $location_name_parts[1] );
     160                $guess     = guess_location_from_geonames( $city_name, $timezone, $country_code );
     161        }
    147162
    148163        // Normalize all errors to boolean false for consistency
Note: See TracChangeset for help on using the changeset viewer.