Making WordPress.org

Changeset 5100


Ignore:
Timestamp:
03/07/2017 01:28:35 AM (9 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.

Location:
sites/trunk/api.wordpress.org/public_html/events/1.0
Files:
2 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
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r5098 r5100  
    125125                ),
    126126
     127                /*
     128                 * This is matching a city inside the country before it the country searches run, but that's ok since it's
     129                 * good enough for our use cases
     130                 */
    127131                'country-exonym-2-words' => array(
    128132                        'input' => array(
     
    132136                        ),
    133137                        'expected' => array(
    134                                 'country' => 'BA'
     138                                'description' => 'pale',
     139                                'latitude'    => '43.817',
     140                                'longitude'   => '18.569',
     141                                'country'     => 'BA'
    135142                        ),
    136143                ),
     
    419426
    420427                /*
     428                 * A combination of city, region, and country are given, along with the locale and timezone
     429                 */
     430                '1-word-city-region' => array(
     431                        'input' => array(
     432                                'location_name' => 'Portland Maine',
     433                                'locale'        => 'en_US',
     434                                'timezone'      => 'America/New_York',
     435                        ),
     436                        'expected' => array(
     437                                'description' => 'portland',
     438                                'latitude'    => '43.661',
     439                                'longitude'   => '-70.255',
     440                                'country'     => 'US',
     441                        ),
     442                ),
     443
     444                '2-word-city-region' => array(
     445                        'input' => array(
     446                                'location_name' => 'São Paulo Brazil',
     447                                'locale'        => 'pt_BR',
     448                                'timezone'      => 'America/Sao_Paulo',
     449                        ),
     450                        'expected' => array(
     451                                'description' => 'são paulo',
     452                                'latitude'    => '-23.548',
     453                                'longitude'   => '-46.636',
     454                                'country'     => 'BR',
     455                        ),
     456                ),
     457
     458
     459                /*
    421460                 * Only the IP is given
    422461                 */
Note: See TracChangeset for help on using the changeset viewer.