WikiCrawler/Utility/CategoryTranslation.cs
/// <summary>
/// Translates a location string into a set of categories.
/// </summary>
Translates a location string into a set of categories.
Translates a location string into a set of categories .
public static IEnumerable<string> TranslateLocationCategory(string input)
		{
			if (string.IsNullOrEmpty(input)) return null;
			IEnumerable<string> existingMapping = GetMappedCategories(input);
			if (existingMapping != null)
			{
				return existingMapping;
			}
			string category = TryFetchCategoryName(s_commonsApi, input);
			if (!string.IsNullOrEmpty(category))
			{
				return MapCategory(input, category);
			}
			string[] pieces = input.Split(StringUtility.DashDash, StringSplitOptions.RemoveEmptyEntries);
			for (int c = 0; c < pieces.Length; c++)
			{
				pieces[c] = pieces[c].Replace("(state)", "").Trim();
				pieces[c] = pieces[c].Replace("(State)", "").Trim();
			}
			Console.WriteLine("Attempting to map location '" + input + "'.");
			if (pieces.Length >= 2)
			{
				string attempt = pieces[pieces.Length - 1].Trim() + ", " + pieces[pieces.Length - 2].Trim();
				category = TryFetchCategoryName(s_commonsApi, attempt);
				if (!string.IsNullOrEmpty(category))
				{
					return MapCategory(input, category);
				}
			}
			string[] entities = s_wikidataApi.SearchEntities(pieces.Last());
			for (int d = 0; d < Math.Min(5, entities.Length); d++)
			{
				MediaWiki.Entity place = s_wikidataApi.GetEntity(entities[d]);
				if (place.HasClaim("P17"))
				{
					throw new Exception();
					IEnumerable<MediaWiki.Entity> parents = place.GetClaimValuesAsEntity("P17", s_wikidataApi);
					if (place.HasClaim("P131"))
					{
						parents = parents.Concat(place.GetClaimValuesAsEntity("P131", s_wikidataApi));
					}
					foreach (MediaWiki.Entity parent in parents)
					{
						for (int c = 0; c < pieces.Length - 1; c++)
						{
							bool countrySuccess = false;
							if (parent.aliases != null && parent.aliases.ContainsKey("en"))
							{
								foreach (string s in parent.aliases["en"])
								{
									if (string.Compare(pieces[c], s, true) == 0)
									{
										countrySuccess = true;
									}
								}
							}
							if (parent.labels != null && parent.labels.ContainsKey("en")
								&& string.Compare(pieces[c], parent.labels["en"], true) == 0)
							{
								countrySuccess = true;
							}
							if (countrySuccess)
							{
								if (place.HasClaim("P373"))
								{
									return MapCategory(input, place.GetClaimValueAsString("P373"));
								}
								else
								{
									return MapCategory(input);
								}
							}
						}
					}
				}
			}
			return MapCategory(input);
		}
public static IEnumerable < string > TranslateLocationCategory ( string input ) { if ( string . IsNullOrEmpty ( input ) ) return null ; IEnumerable < string > existingMapping = GetMappedCategories ( input ) ; if ( existingMapping != null ) { return existingMapping ; } string category = TryFetchCategoryName ( s_commonsApi , input ) ; if ( ! string . IsNullOrEmpty ( category ) ) { return MapCategory ( input , category ) ; } string [ ] pieces = input . Split ( StringUtility . DashDash , StringSplitOptions . RemoveEmptyEntries ) ; for ( int c = 0 ; c < pieces . Length ; c ++ ) { pieces [ c ] = pieces [ c ] . Replace ( " " , " " ) . Trim ( ) ; pieces [ c ] = pieces [ c ] . Replace ( " " , " " ) . Trim ( ) ; } Console . WriteLine ( " " + input + " " ) ; if ( pieces . Length >= 2 ) { string attempt = pieces [ pieces . Length - 1 ] . Trim ( ) + " " + pieces [ pieces . Length - 2 ] . Trim ( ) ; category = TryFetchCategoryName ( s_commonsApi , attempt ) ; if ( ! string . IsNullOrEmpty ( category ) ) { return MapCategory ( input , category ) ; } } string [ ] entities = s_wikidataApi . SearchEntities ( pieces . Last ( ) ) ; for ( int d = 0 ; d < Math . Min ( 5 , entities . Length ) ; d ++ ) { MediaWiki . Entity place = s_wikidataApi . GetEntity ( entities [ d ] ) ; if ( place . HasClaim ( " " ) ) { throw new Exception ( ) ; IEnumerable < MediaWiki . Entity > parents = place . GetClaimValuesAsEntity ( " " , s_wikidataApi ) ; if ( place . HasClaim ( " " ) ) { parents = parents . Concat ( place . GetClaimValuesAsEntity ( " " , s_wikidataApi ) ) ; } foreach ( MediaWiki . Entity parent in parents ) { for ( int c = 0 ; c < pieces . Length - 1 ; c ++ ) { bool countrySuccess = false ; if ( parent . aliases != null && parent . aliases . ContainsKey ( " " ) ) { foreach ( string s in parent . aliases [ " " ] ) { if ( string . Compare ( pieces [ c ] , s , true ) == 0 ) { countrySuccess = true ; } } } if ( parent . labels != null && parent . labels . ContainsKey ( " " ) && string . Compare ( pieces [ c ] , parent . labels [ " " ] , true ) == 0 ) { countrySuccess = true ; } if ( countrySuccess ) { if ( place . HasClaim ( " " ) ) { return MapCategory ( input , place . GetClaimValueAsString ( " " ) ) ; } else { return MapCategory ( input ) ; } } } } } } return MapCategory ( input ) ; }
Translates a location string into a set of categories.
Translates a location string into a set of categories .
//simple mapping //try flipping last two, with a comma //check for only the last on wikidata //get country for place //TODO: instead, verify ALL pieces are present //look for the parent in the earlier pieces // find Commons cat from the Wikidata entity
input
string