This commit is contained in:
Victor Shcherb 2021-04-23 17:31:24 +02:00
parent bc3a37b65f
commit 3db30ffe8d
3 changed files with 82 additions and 29 deletions

View file

@ -1,20 +1,6 @@
package net.osmand.search.core;
import net.osmand.Collator;
import net.osmand.CollatorStringMatcher;import net.osmand.OsmAndCollator;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.StringMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapIndexReader.SearchRequest;
import net.osmand.binary.CommonWords;
import net.osmand.data.LatLon;
import net.osmand.data.QuadRect;
import net.osmand.util.Algorithms;
import net.osmand.util.LocationParser;
import net.osmand.util.MapUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@ -26,6 +12,20 @@ import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import net.osmand.Collator;
import net.osmand.CollatorStringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.OsmAndCollator;
import net.osmand.StringMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapIndexReader.SearchRequest;
import net.osmand.binary.CommonWords;
import net.osmand.data.LatLon;
import net.osmand.data.QuadRect;
import net.osmand.util.Algorithms;
import net.osmand.util.LocationParser;
import net.osmand.util.MapUtils;
// Immutable object !
public class SearchPhrase {
public static final String DELIMITER = " ";
@ -228,7 +228,17 @@ public class SearchPhrase {
return sp;
}
public int countWords(String w) {
public static void splitWords(String w, Collection<String> ws) {
if (!Algorithms.isEmpty(w)) {
String[] wrs = w.split(ALLDELIMITERS);
for (int i = 0; i < wrs.length; i++) {
String wd = wrs[i].trim();
ws.add(wd);
}
}
}
public static int countWords(String w) {
int cnt = 0;
if (!Algorithms.isEmpty(w)) {
String[] ws = w.split(ALLDELIMITERS);

View file

@ -1,5 +1,9 @@
package net.osmand.search.core;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.City;
import net.osmand.data.LatLon;
@ -11,8 +15,6 @@ import net.osmand.osm.PoiType;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import java.util.Collection;
public class SearchResult {
// search phrase that makes search result valid
public SearchPhrase requiredSearchPhrase;
@ -42,6 +44,8 @@ public class SearchResult {
public Object relatedObject;
public double distRelatedObjectName;
private double unknownPhraseMatchWeight = 0;
public SearchResult(SearchPhrase sp) {
this.requiredSearchPhrase = sp;
}
@ -49,30 +53,66 @@ public class SearchResult {
// maximum corresponds to the top entry
public double getUnknownPhraseMatchWeight() {
if (unknownPhraseMatchWeight != 0) {
return unknownPhraseMatchWeight;
}
// if result is a complete match in the search we prioritize it higher
return getSumPhraseMatchWeight() / Math.pow(MAX_TYPE_WEIGHT, getDepth() - 1);
double res = getSumPhraseMatchWeight() / Math.pow(MAX_TYPE_WEIGHT, getDepth() - 1);
unknownPhraseMatchWeight = res;
return res;
}
public double getSumPhraseMatchWeight() {
// if result is a complete match in the search we prioritize it higher
String name = alternateName != null ? alternateName : localeName;
int localWordsMatched = requiredSearchPhrase.countWords(name) ;
boolean match = localWordsMatched <= getSelfWordCount();
boolean fullCompleteMatch = false;
double res = 1;
if (match) {
res = ObjectType.getTypeWeight(match ? objectType : null);
fullCompleteMatch = getSelfPhrase().equalsIgnoreCase(name);
if (fullCompleteMatch) {
res = res * 10;
List<String> localResultNames = new ArrayList<>();
SearchPhrase.splitWords(name, localResultNames);
List<String> searchPhraseNames = new ArrayList<String>();
if (firstUnknownWordMatches) {
searchPhraseNames.add(requiredSearchPhrase.getFirstUnknownSearchWord());
}
if (otherWordsMatch != null) {
searchPhraseNames.addAll(otherWordsMatch);
}
int idxMatchedWord = -1;
boolean allWordsMatched = true;
if (!checkPhraseIsObjectType()) {
for (int i = 0; i < searchPhraseNames.size(); i++) {
boolean wordMatched = false;
for (int j = idxMatchedWord + 1; j < localResultNames.size(); j++) {
int r = requiredSearchPhrase.getCollator().compare(searchPhraseNames.get(i), localResultNames.get(j));
if (r == 0) {
wordMatched = true;
idxMatchedWord = j;
break;
}
}
if (!wordMatched) {
allWordsMatched = false;
break;
}
}
}
double res = ObjectType.getTypeWeight(allWordsMatched ? objectType : null);
if (parentSearchResult != null) {
res = res + parentSearchResult.getSumPhraseMatchWeight() / MAX_TYPE_WEIGHT;
}
return res;
}
private boolean checkPhraseIsObjectType() {
// if (object instanceof Amenity) {
// String poiType = ((Amenity) object).getType().getKeyName();
// String poiSubType = ((Amenity) object).getSubType();
// boolean isType = poiType.equalsIgnoreCase(phrase);
// boolean isSubType = poiSubType.equalsIgnoreCase(phrase);
// return isType || isSubType;
// }
return false;
}
public int getDepth() {
if (parentSearchResult != null) {
return 1 + parentSearchResult.getDepth();

View file

@ -132,6 +132,10 @@ public class SearchUICoreTest {
reader = new BinaryMapIndexReader(new RandomAccessFile(obfFile.getPath(), "r"), obfFile);
}
boolean disabled = settingsJson.optBoolean("disabled", false);
if (disabled) {
return;
}
List<List<String>> results = new ArrayList<>();
for (int i = 0; i < phrases.size(); i++) {
results.add(new ArrayList<String>());
@ -199,7 +203,6 @@ public class SearchUICoreTest {
System.out.println("EXPECTED : ");
for (String r : result) {
System.out.println(String.format("\t\"%s\",", r));
}
}
Assert.assertEquals(expected, present);