Changeset 33993 in osm for applications


Ignore:
Timestamp:
2018-01-11T00:07:20+01:00 (7 years ago)
Author:
donvip
Message:

handle multiple levels of wikipedia article redirections when searching for their wikidata items

Location:
applications/editors/josm/plugins/wikipedia
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java

    r33541 r33993  
    3535import org.openstreetmap.josm.tools.HttpClient;
    3636import org.openstreetmap.josm.tools.Logging;
     37import org.openstreetmap.josm.tools.Pair;
    3738import org.openstreetmap.josm.tools.Utils;
    3839import org.w3c.dom.Document;
     
    258259                .collect(Collectors.toList());
    259260        if (!unresolved.isEmpty()) {
    260             final Map<String, String> redirects = resolveRedirectsForArticles(unresolved);
    261             final Map<String, String> result2 = getWikidataForArticles0(redirects.values());
     261            doResolveWikidataItems(unresolved, result);
     262        }
     263        return result;
     264    }
     265
     266    private Pair<Map<String, String>, Map<String, String>> doResolveWikidataItems(List<String> unresolved, Map<String, String> result) {
     267        final Map<String, String> redirects = resolveRedirectsForArticles(unresolved);
     268        final Map<String, String> result2 = getWikidataForArticles0(redirects.values());
     269        final List<String> unresolved2 = new ArrayList<>();
     270        redirects.forEach((original, resolved) -> {
     271            if (result2.containsKey(resolved)) {
     272                result.put(original, result2.get(resolved));
     273            } else if (!Objects.equals(original, resolved)) {
     274                // Handle double redirection like USA -> United States of America -> United States (as of 9th January 2018)
     275                unresolved2.add(resolved);
     276            }
     277        });
     278        if (!unresolved2.isEmpty()) {
     279            Pair<Map<String, String>, Map<String, String>> p = doResolveWikidataItems(unresolved2, result);
    262280            redirects.forEach((original, resolved) -> {
    263                 if (result2.containsKey(resolved)) {
    264                     result.put(original, result2.get(resolved));
     281                if (!result2.containsKey(resolved)) {
     282                    p.a.forEach((original2, resolved2) -> {
     283                        if (p.b.containsKey(resolved2)) {
     284                            result.put(original, p.b.get(resolved2));
     285                        }
     286                    });
    265287                }
    266288            });
    267289        }
    268         return result;
     290        return Pair.create(redirects, result2);
    269291    }
    270292
  • applications/editors/josm/plugins/wikipedia/test/unit/org/wikipedia/WikipediaAppTest.java

    r33098 r33993  
    124124        assertThat(map.get("einstein"), is("Q937"));
    125125        assertThat(map.get("USA"), is("Q30"));
    126         assertThat(map.size(), is(2));
     126        assertThat(map.size(), is(3));
    127127    }
    128128
Note: See TracChangeset for help on using the changeset viewer.