Fix unknown poi type search

This commit is contained in:
crimean 2018-10-03 10:34:45 +03:00
parent d608fe291d
commit 751892e5dd
3 changed files with 101 additions and 59 deletions

View file

@ -626,6 +626,7 @@ public class SearchCoreFactory {
private TIntArrayList customPoiFiltersPriorites = new TIntArrayList(); private TIntArrayList customPoiFiltersPriorites = new TIntArrayList();
private MapPoiTypes types; private MapPoiTypes types;
private List<AbstractPoiType> foundPoiTypes = new ArrayList<>(); private List<AbstractPoiType> foundPoiTypes = new ArrayList<>();
private SearchPhrase lastSearchedPhrase;
public SearchAmenityTypesAPI(MapPoiTypes types) { public SearchAmenityTypesAPI(MapPoiTypes types) {
super(ObjectType.POI_TYPE); super(ObjectType.POI_TYPE);
@ -640,6 +641,14 @@ public class SearchCoreFactory {
return foundPoiTypes.size() > 0; return foundPoiTypes.size() > 0;
} }
public SearchPhrase getLastSearchedPhrase() {
return lastSearchedPhrase;
}
public void setLastSearchedPhrase(SearchPhrase lastSearchedPhrase) {
this.lastSearchedPhrase = lastSearchedPhrase;
}
public void clearCustomFilters() { public void clearCustomFilters() {
this.customPoiFilters.clear(); this.customPoiFilters.clear();
this.customPoiFiltersPriorites.clear(); this.customPoiFiltersPriorites.clear();
@ -702,7 +711,9 @@ public class SearchCoreFactory {
} }
} }
foundPoiTypes = new ArrayList<>(results); foundPoiTypes = new ArrayList<>(results);
lastSearchedPhrase = phrase;
if (resultMatcher != null) {
for (AbstractPoiType pt : results) { for (AbstractPoiType pt : results) {
SearchResult res = new SearchResult(phrase); SearchResult res = new SearchResult(phrase);
res.localeName = pt.getTranslation(); res.localeName = pt.getTranslation();
@ -724,7 +735,7 @@ public class SearchCoreFactory {
resultMatcher.publish(res); resultMatcher.publish(res);
} }
} }
}
return true; return true;
} }
@ -810,48 +821,35 @@ public class SearchCoreFactory {
} else { } else {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
QuadRect bbox = phrase.getRadiusBBoxToSearch(BBOX_RADIUS); searchPoi(phrase, resultMatcher, obj, null, ptf);
List<BinaryMapIndexReader> oo = phrase.getOfflineIndexes(); } else if (searchAmenityTypesAPI != null) {
Set<String> searchedPois = new TreeSet<>(); if (searchAmenityTypesAPI.lastSearchedPhrase == null
for (BinaryMapIndexReader o : oo) { || !searchAmenityTypesAPI.lastSearchedPhrase.getUnknownSearchPhrase().equals(phrase.getUnknownSearchPhrase())) {
ResultMatcher<Amenity> rm = getResultMatcher(phrase, resultMatcher, null, o, searchedPois); searchAmenityTypesAPI.search(phrase, null);
if (obj instanceof CustomSearchPoiFilter) {
rm = ((CustomSearchPoiFilter) obj).wrapResultMatcher(rm);
} }
searchPoi(phrase, resultMatcher, ptf, bbox, o, rm);
}
} else {
List<AbstractPoiType> poiTypes = searchAmenityTypesAPI.getFoundPoiTypes(); List<AbstractPoiType> poiTypes = searchAmenityTypesAPI.getFoundPoiTypes();
for (AbstractPoiType pt : poiTypes) { for (AbstractPoiType pt : poiTypes) {
SearchPoiTypeFilter ptf = getPoiTypeFilter(pt); SearchPoiTypeFilter ptf = getPoiTypeFilter(pt);
QuadRect bbox = phrase.getRadiusBBoxToSearch(BBOX_RADIUS); String customName = phrase.getPoiNameFilter(pt);
String customName = null; if (customName != null) {
NameStringMatcher nm = phrase.getNameStringMatcher(phrase.getUnknownSearchWord(), true); phrase.setUnknownSearchWordPoiType(pt);
String unknownSearchPhrase = phrase.getUnknownSearchPhrase(); searchPoi(phrase, resultMatcher, null, customName, ptf);
String enTranslation = pt.getEnTranslation(); break;
String translation = pt.getTranslation();
String synonyms = pt.getSynonyms();
if (unknownSearchPhrase.length() > enTranslation.length() && nm.matches(enTranslation)) {
customName = unknownSearchPhrase.substring(enTranslation.length()).trim();
} else if (unknownSearchPhrase.length() > translation.length() && nm.matches(translation)) {
customName = unknownSearchPhrase.substring(translation.length()).trim();
} else if (unknownSearchPhrase.length() > synonyms.length() && nm.matches(synonyms)) {
customName = unknownSearchPhrase.substring(synonyms.length()).trim();
}
if (!Algorithms.isEmpty(customName)) {
List<BinaryMapIndexReader> oo = phrase.getOfflineIndexes();
Set<String> searchedPois = new TreeSet<>();
for (BinaryMapIndexReader o : oo) {
ResultMatcher<Amenity> rm = getResultMatcher(phrase, resultMatcher, customName, o, searchedPois);
searchPoi(phrase, resultMatcher, ptf, bbox, o, rm);
}
} }
} }
} }
return true; return true;
} }
private void searchPoi(SearchPhrase phrase, SearchResultMatcher resultMatcher, SearchPoiTypeFilter ptf, QuadRect bbox, BinaryMapIndexReader o, ResultMatcher<Amenity> rm) throws IOException { private void searchPoi(SearchPhrase phrase, SearchResultMatcher resultMatcher, Object obj, String customName, SearchPoiTypeFilter ptf) throws IOException {
QuadRect bbox = phrase.getRadiusBBoxToSearch(BBOX_RADIUS);
List<BinaryMapIndexReader> oo = phrase.getOfflineIndexes();
Set<String> searchedPois = new TreeSet<>();
for (BinaryMapIndexReader o : oo) {
ResultMatcher<Amenity> rm = getResultMatcher(phrase, resultMatcher, customName, o, searchedPois);
if (obj instanceof CustomSearchPoiFilter) {
rm = ((CustomSearchPoiFilter) obj).wrapResultMatcher(rm);
}
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest( SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(
(int) bbox.left, (int) bbox.right, (int) bbox.left, (int) bbox.right,
(int) bbox.top, (int) bbox.bottom, -1, ptf, (int) bbox.top, (int) bbox.bottom, -1, ptf,
@ -859,6 +857,7 @@ public class SearchCoreFactory {
o.searchPoi(req); o.searchPoi(req);
resultMatcher.apiSearchRegionFinished(this, o, phrase); resultMatcher.apiSearchRegionFinished(this, o, phrase);
} }
}
private ResultMatcher<Amenity> getResultMatcher(final SearchPhrase phrase, final SearchResultMatcher resultMatcher, private ResultMatcher<Amenity> getResultMatcher(final SearchPhrase phrase, final SearchResultMatcher resultMatcher,
final String customName, final BinaryMapIndexReader selected, final String customName, final BinaryMapIndexReader selected,

View file

@ -5,10 +5,11 @@ import net.osmand.CollatorStringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode; import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.StringMatcher; import net.osmand.StringMatcher;
import net.osmand.binary.BinaryMapIndexReader; import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.CommonWords;
import net.osmand.binary.BinaryMapIndexReader.SearchRequest; import net.osmand.binary.BinaryMapIndexReader.SearchRequest;
import net.osmand.binary.CommonWords;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.QuadRect; import net.osmand.data.QuadRect;
import net.osmand.osm.AbstractPoiType;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
@ -33,6 +34,7 @@ public class SearchPhrase {
private List<NameStringMatcher> unknownWordsMatcher = new ArrayList<>(); private List<NameStringMatcher> unknownWordsMatcher = new ArrayList<>();
private String unknownSearchWordTrim; private String unknownSearchWordTrim;
private String unknownSearchPhrase = ""; private String unknownSearchPhrase = "";
private AbstractPoiType unknownSearchWordPoiType;
private NameStringMatcher sm; private NameStringMatcher sm;
private SearchSettings settings; private SearchSettings settings;
@ -224,6 +226,40 @@ public class SearchPhrase {
return unknownSearchWordTrim.length() ; return unknownSearchWordTrim.length() ;
} }
public AbstractPoiType getUnknownSearchWordPoiType() {
return unknownSearchWordPoiType;
}
public void setUnknownSearchWordPoiType(AbstractPoiType unknownSearchWordPoiType) {
this.unknownSearchWordPoiType = unknownSearchWordPoiType;
}
public boolean hasUnknownSearchWordPoiType() {
return unknownSearchWordPoiType != null;
}
public String getPoiNameFilter() {
return getPoiNameFilter(unknownSearchWordPoiType);
}
public String getPoiNameFilter(AbstractPoiType pt) {
String nameFilter = null;
if (pt != null) {
NameStringMatcher nm = getNameStringMatcher(getUnknownSearchWord(), true);
String unknownSearchPhrase = getUnknownSearchPhrase();
String enTranslation = pt.getEnTranslation();
String translation = pt.getTranslation();
String synonyms = pt.getSynonyms();
if (unknownSearchPhrase.length() > enTranslation.length() && nm.matches(enTranslation)) {
nameFilter = unknownSearchPhrase.substring(enTranslation.length()).trim();
} else if (unknownSearchPhrase.length() > translation.length() && nm.matches(translation)) {
nameFilter = unknownSearchPhrase.substring(translation.length()).trim();
} else if (unknownSearchPhrase.length() > synonyms.length() && nm.matches(synonyms)) {
nameFilter = unknownSearchPhrase.substring(synonyms.length()).trim();
}
}
return nameFilter;
}
public QuadRect getRadiusBBoxToSearch(int radius) { public QuadRect getRadiusBBoxToSearch(int radius) {
int radiusInMeters = getRadiusSearch(radius); int radiusInMeters = getRadiusSearch(radius);

View file

@ -342,6 +342,13 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
filter = app.getPoiFilters().getNominatimPOIFilter(); filter = app.getPoiFilters().getNominatimPOIFilter();
filter.setFilterByName(searchPhrase.getUnknownSearchPhrase()); filter.setFilterByName(searchPhrase.getUnknownSearchPhrase());
filter.clearCurrentResults(); filter.clearCurrentResults();
} else if (searchPhrase.hasUnknownSearchWordPoiType()) {
AbstractPoiType pt = searchPhrase.getUnknownSearchWordPoiType();
filter = new PoiUIFilter(pt, app, "");
String customName = searchPhrase.getPoiNameFilter();
if (!Algorithms.isEmpty(customName)) {
filter.setFilterByName(customName);
}
} else { } else {
filter = app.getPoiFilters().getSearchByNamePOIFilter(); filter = app.getPoiFilters().getSearchByNamePOIFilter();
if (!Algorithms.isEmpty(searchPhrase.getUnknownSearchWord())) { if (!Algorithms.isEmpty(searchPhrase.getUnknownSearchWord())) {