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

View file

@ -5,10 +5,11 @@ import net.osmand.CollatorStringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.StringMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.CommonWords;
import net.osmand.binary.BinaryMapIndexReader.SearchRequest;
import net.osmand.binary.CommonWords;
import net.osmand.data.LatLon;
import net.osmand.data.QuadRect;
import net.osmand.osm.AbstractPoiType;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
@ -33,6 +34,7 @@ public class SearchPhrase {
private List<NameStringMatcher> unknownWordsMatcher = new ArrayList<>();
private String unknownSearchWordTrim;
private String unknownSearchPhrase = "";
private AbstractPoiType unknownSearchWordPoiType;
private NameStringMatcher sm;
private SearchSettings settings;
@ -224,6 +226,40 @@ public class SearchPhrase {
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) {
int radiusInMeters = getRadiusSearch(radius);

View file

@ -342,6 +342,13 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC
filter = app.getPoiFilters().getNominatimPOIFilter();
filter.setFilterByName(searchPhrase.getUnknownSearchPhrase());
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 {
filter = app.getPoiFilters().getSearchByNamePOIFilter();
if (!Algorithms.isEmpty(searchPhrase.getUnknownSearchWord())) {