public Map<String,String> getLocation(String text){
		
		Map<String, String> locationMap = new HashMap<String, String>();
		
		List<String> tokenList = getTokens(text);
		
		String detectedCountryCode = null;
		// check mentioned countries
		for (int i = 0; i < tokenList.size(); i++) {
			String countryCode = countryNames.get(tokenList.get(i));
			if (countryCode != null){
				detectedCountryCode = countryCode;
				break;
			}
		}
		if (tokenList.size() > 1) {
			for (int i = 0; i < tokenList.size()-1; i++) {
				String countryCode = countryNames.get(tokenList.get(i) + " " + tokenList.get(i+1));
				if (countryCode != null){
					detectedCountryCode = countryCode;
					break;
				}
			}
		}
		
		String detectedCountryCodeByAdminName = null;
		String detectedAdminName = null;
		// check mentioned administrative regions
		for (int i = 0; i < tokenList.size(); i++) {
			String countryCode = gnAdminNamesMap.get(tokenList.get(i));
			if (countryCode != null){
				detectedAdminName = tokenList.get(i);
				detectedAdminName = detectedAdminName.substring(0, 1).toUpperCase() + detectedAdminName.substring(1);
				detectedCountryCodeByAdminName = countryCode;
				break;
			}
		}
		if (tokenList.size() > 1) {
			for (int i = 0; i < tokenList.size()-1; i++) {
				String countryCode = gnAdminNamesMap.get(tokenList.get(i) + " " + tokenList.get(i+1));
				if (countryCode != null){
					detectedAdminName = tokenList.get(i) + " " + tokenList.get(i+1);
					detectedAdminName = detectedAdminName.substring(0, 1).toUpperCase() + detectedAdminName.substring(1);
					detectedCountryCodeByAdminName = countryCode;
					break;
				}
			}
		}
		
		// check mentioned cities
		List<GeoObject> gnObjects = getMentionedGnObjects(tokenList);
		long maxPopulation = 0;
		GeoObject maxGnObject = null;
		for (GeoObject gnObject : gnObjects){
			if (gnObject.getPopulation() > maxPopulation) {
				maxPopulation = gnObject.getPopulation();
				maxGnObject = gnObject;
			}
		}
		
		// if nothing was found, return unknown
		if (maxGnObject == null && detectedCountryCode == null && detectedCountryCodeByAdminName == null) {
			return locationMap;
		}
		
		// if only country detected, return country
		if (maxGnObject == null && detectedCountryCode != null && detectedCountryCodeByAdminName == null) {
			locationMap.put(COUNTRY, getCountryByCountryCode(detectedCountryCode));
			return locationMap;
		}
		
		// if administrative name detected, return administrative name + country 
		if (maxGnObject == null && detectedCountryCodeByAdminName != null) {
			locationMap.put(AREA, detectedAdminName);
			locationMap.put(COUNTRY, getCountryByCountryCode(detectedCountryCodeByAdminName));
			return locationMap;
		}
		
		// if both administrative name detected and city detected, check for consistency
		if (maxGnObject != null && detectedCountryCodeByAdminName != null && detectedCountryCode == null) {
			if (maxGnObject.getCountryCode().equals(detectedCountryCodeByAdminName)){
				locationMap.put(CITY, maxGnObject.getName());
				locationMap.put(COUNTRY, getCountryByCountryCode(maxGnObject.getCountryCode()));
				return locationMap;
			} else {
				//logger.info("Inconsistency found: While country was found to be " + detectedCountryCodeByAdminName + ", city was found to be " + maxGnObject.getName() + ", " + maxGnObject.getCountryCode());
				locationMap.put(AREA, detectedAdminName);
				locationMap.put(COUNTRY, getCountryByCountryCode(detectedCountryCodeByAdminName));
				return locationMap;
			}
		}
		
		// if a city is detected, check for consistency with the detected country (if any) and
		// return city only if it agrees with country, otherwise send only country
		if (maxGnObject != null && detectedCountryCode == null) {
			locationMap.put(CITY, maxGnObject.getName());
			locationMap.put(COUNTRY, getCountryByCountryCode(maxGnObject.getCountryCode()));
			return  locationMap;
		}
		if (maxGnObject != null && detectedCountryCode != null) {
			if (maxGnObject.getCountryCode().equals(detectedCountryCode)){
				locationMap.put(CITY, maxGnObject.getName());
				locationMap.put(COUNTRY, getCountryByCountryCode(maxGnObject.getCountryCode()));
				return  locationMap;
			} else {
				//logger.info("Inconsistency found: While country was found to be " + detectedCountryCode + ", city was found to be " + maxGnObject.getName() + ", " + maxGnObject.getCountryCode());
				locationMap.put(COUNTRY, getCountryByCountryCode(detectedCountryCode));
				return locationMap;
			}
		}
		return locationMap;
	}
public Map < String , String > getLocation ( String text ) { Map < String , String > locationMap = new HashMap < String , String > ( ) ; List < String > tokenList = getTokens ( text ) ; String detectedCountryCode = null ; for ( int i = 0 ; i < tokenList . size ( ) ; i ++ ) { String countryCode = countryNames . get ( tokenList . get ( i ) ) ; if ( countryCode != null ) { detectedCountryCode = countryCode ; break ; } } if ( tokenList . size ( ) > 1 ) { for ( int i = 0 ; i < tokenList . size ( ) - 1 ; i ++ ) { String countryCode = countryNames . get ( tokenList . get ( i ) + " " + tokenList . get ( i + 1 ) ) ; if ( countryCode != null ) { detectedCountryCode = countryCode ; break ; } } } String detectedCountryCodeByAdminName = null ; String detectedAdminName = null ; for ( int i = 0 ; i < tokenList . size ( ) ; i ++ ) { String countryCode = gnAdminNamesMap . get ( tokenList . get ( i ) ) ; if ( countryCode != null ) { detectedAdminName = tokenList . get ( i ) ; detectedAdminName = detectedAdminName . substring ( 0 , 1 ) . toUpperCase ( ) + detectedAdminName . substring ( 1 ) ; detectedCountryCodeByAdminName = countryCode ; break ; } } if ( tokenList . size ( ) > 1 ) { for ( int i = 0 ; i < tokenList . size ( ) - 1 ; i ++ ) { String countryCode = gnAdminNamesMap . get ( tokenList . get ( i ) + " " + tokenList . get ( i + 1 ) ) ; if ( countryCode != null ) { detectedAdminName = tokenList . get ( i ) + " " + tokenList . get ( i + 1 ) ; detectedAdminName = detectedAdminName . substring ( 0 , 1 ) . toUpperCase ( ) + detectedAdminName . substring ( 1 ) ; detectedCountryCodeByAdminName = countryCode ; break ; } } } List < GeoObject > gnObjects = getMentionedGnObjects ( tokenList ) ; long maxPopulation = 0 ; GeoObject maxGnObject = null ; for ( GeoObject gnObject : gnObjects ) { if ( gnObject . getPopulation ( ) > maxPopulation ) { maxPopulation = gnObject . getPopulation ( ) ; maxGnObject = gnObject ; } } if ( maxGnObject == null && detectedCountryCode == null && detectedCountryCodeByAdminName == null ) { return locationMap ; } if ( maxGnObject == null && detectedCountryCode != null && detectedCountryCodeByAdminName == null ) { locationMap . put ( COUNTRY , getCountryByCountryCode ( detectedCountryCode ) ) ; return locationMap ; } if ( maxGnObject == null && detectedCountryCodeByAdminName != null ) { locationMap . put ( AREA , detectedAdminName ) ; locationMap . put ( COUNTRY , getCountryByCountryCode ( detectedCountryCodeByAdminName ) ) ; return locationMap ; } if ( maxGnObject != null && detectedCountryCodeByAdminName != null && detectedCountryCode == null ) { if ( maxGnObject . getCountryCode ( ) . equals ( detectedCountryCodeByAdminName ) ) { locationMap . put ( CITY , maxGnObject . getName ( ) ) ; locationMap . put ( COUNTRY , getCountryByCountryCode ( maxGnObject . getCountryCode ( ) ) ) ; return locationMap ; } else { locationMap . put ( AREA , detectedAdminName ) ; locationMap . put ( COUNTRY , getCountryByCountryCode ( detectedCountryCodeByAdminName ) ) ; return locationMap ; } } if ( maxGnObject != null && detectedCountryCode == null ) { locationMap . put ( CITY , maxGnObject . getName ( ) ) ; locationMap . put ( COUNTRY , getCountryByCountryCode ( maxGnObject . getCountryCode ( ) ) ) ; return locationMap ; } if ( maxGnObject != null && detectedCountryCode != null ) { if ( maxGnObject . getCountryCode ( ) . equals ( detectedCountryCode ) ) { locationMap . put ( CITY , maxGnObject . getName ( ) ) ; locationMap . put ( COUNTRY , getCountryByCountryCode ( maxGnObject . getCountryCode ( ) ) ) ; return locationMap ; } else { locationMap . put ( COUNTRY , getCountryByCountryCode ( detectedCountryCode ) ) ; return locationMap ; } } return locationMap ; }
Get an estimate of the referred location in a piece of text
@param text Arbitrary text string, e.g. the text of a tweet
@return Estimated location in the form of a map with keys
"country", "city", "area", or empty map if no location is detected