Small changes

This commit is contained in:
Victor Shcherb 2011-10-01 11:23:13 +02:00
parent 0a96d65468
commit 89f8885aa9
5 changed files with 44 additions and 139 deletions

View file

@ -8,6 +8,7 @@ import gnu.trove.map.hash.TIntObjectHashMap;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.Collator;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@ -15,9 +16,11 @@ import java.util.Locale;
import java.util.Map;
import net.osmand.Algoritms;
import net.osmand.CollatorStringMatcher;
import net.osmand.LogUtil;
import net.osmand.ResultMatcher;
import net.osmand.StringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion;
import net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion;
import net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex;
@ -933,6 +936,39 @@ public class BinaryMapIndexReader {
return req.getSearchResults();
}
public Map<AmenityType, List<String>> searchPoiCategoriesByName(SearchRequest<Amenity> req, Map<AmenityType, List<String>> map)
throws IOException {
if (req.nameQuery == null || req.nameQuery.length() == 0) {
throw new IllegalArgumentException();
}
Collator collator = Collator.getInstance();
collator.setStrength(Collator.PRIMARY);
for (PoiRegion poiIndex : poiIndexes) {
for(int i= 0; i< poiIndex.categories.size(); i++){
String cat = poiIndex.categories.get(i);
AmenityType catType = poiIndex.categoriesType.get(i);
if(CollatorStringMatcher.cmatches(collator, cat, req.nameQuery, StringMatcherMode.CHECK_STARTS_FROM_SPACE)){
map.put(catType, null);
} else {
List<String> subcats = poiIndex.subcategories.get(i);
for(int j=0; j< subcats.size(); j++){
if(CollatorStringMatcher.cmatches(collator, subcats.get(j), req.nameQuery, StringMatcherMode.CHECK_STARTS_FROM_SPACE)){
if(!map.containsKey(catType)){
map.put(catType, new ArrayList<String>());
}
List<String> list = map.get(catType);
if(list != null){
list.add(subcats.get(j));
}
}
}
}
}
}
return map;
}
public List<Amenity> searchPoi(SearchRequest<Amenity> req) throws IOException {
req.numberOfVisitedObjects = 0;
req.numberOfAcceptedObjects = 0;

View file

@ -1,132 +0,0 @@
package net.osmand.plus;
import java.text.Collator;
import net.osmand.StringMatcher;
/**
* Abstract collator matcher that basically supports subclasses with some collator
* matching.
*
* @author pavol.zibrita
*/
public class CollatorStringMatcher implements StringMatcher {
private final Collator collator;
private final StringMatcherMode mode;
private final String part;
public enum StringMatcherMode {
CHECK_ONLY_STARTS_WITH,
CHECK_STARTS_FROM_SPACE,
CHECK_STARTS_FROM_SPACE_NOT_BEGINNING,
CHECK_CONTAINS
}
public CollatorStringMatcher(Collator collator, String part, StringMatcherMode mode) {
this.collator = collator;
this.part = part;
this.mode = mode;
}
public Collator getCollator() {
return collator;
}
@Override
public boolean matches(String name) {
return cmatches(collator, name, part, mode);
}
public static boolean cmatches(Collator collator, String base, String part, StringMatcherMode mode){
switch (mode) {
case CHECK_CONTAINS:
return ccontains(collator, base, part);
case CHECK_STARTS_FROM_SPACE:
return cstartsWith(collator, base, part, true, true);
case CHECK_STARTS_FROM_SPACE_NOT_BEGINNING:
return cstartsWith(collator, base, part, false, true);
case CHECK_ONLY_STARTS_WITH:
return cstartsWith(collator, base, part, true, false);
}
return false;
}
/**
* Check if part contains in base
*
* @param collator Collator to use
* @param part String to search
* @param base String where to search
* @return true if part is contained in base
*/
public static boolean ccontains(Collator collator, String base, String part) {
int pos = 0;
if (part.length() > 3) {
// improve searching by searching first 3 characters
pos = cindexOf(collator, pos, part.substring(0, 3), base);
if (pos == -1) {
return false;
}
}
pos = cindexOf(collator, pos, part, base);
if (pos == -1) {
return false;
}
return true;
}
private static int cindexOf(Collator collator, int start, String part, String base) {
for (int pos = start; pos <= base.length() - part.length(); pos++) {
if (collator.equals(base.substring(pos, pos + part.length()), part)) {
return pos;
}
}
return -1;
}
/**
* Checks if string starts with another string.
* Special check try to find as well in the middle of name
*
* @param collator
* @param searchIn
* @param theStart
* @return true if searchIn starts with token
*/
public static boolean cstartsWith(Collator collator, String searchIn, String theStart,
boolean checkBeginning, boolean checkSpaces) {
int startLength = theStart.length();
int searchInLength = searchIn.length();
if (startLength == 0) {
return true;
}
if (startLength > searchInLength) {
return false;
}
// simulate starts with for collator
if (checkBeginning) {
boolean starts = collator.equals(searchIn.substring(0, startLength), theStart);
if (starts) {
return true;
}
}
if (checkSpaces) {
for (int i = 1; i <= searchInLength - startLength; i++) {
if (considerAsSpace(searchIn.charAt(i - 1)) && !considerAsSpace(searchIn.charAt(i))) {
if (collator.equals(searchIn.substring(i, i + startLength), theStart)) {
return true;
}
}
}
}
return false;
}
private static boolean considerAsSpace(char c){
return !Character.isLetter(c) && !Character.isDigit(c);
}
}

View file

@ -1,6 +1,5 @@
package net.osmand.plus;
import static net.osmand.plus.CollatorStringMatcher.cmatches;
import java.io.IOException;
import java.text.Collator;
@ -12,8 +11,10 @@ import java.util.Map;
import java.util.TreeMap;
import net.osmand.Algoritms;
import net.osmand.CollatorStringMatcher;
import net.osmand.LogUtil;
import net.osmand.ResultMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.Building;
import net.osmand.data.City;
@ -21,7 +22,6 @@ import net.osmand.data.MapObject;
import net.osmand.data.PostCode;
import net.osmand.data.Street;
import net.osmand.osm.LatLon;
import net.osmand.plus.CollatorStringMatcher.StringMatcherMode;
import org.apache.commons.logging.Log;
@ -174,7 +174,7 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
name = name.toLowerCase();
for (City c : cities.values()) {
String cName = c.getName(useEnglishNames); // lower case not needed, collator ensures that
if (cmatches(collator, cName, name, StringMatcherMode.CHECK_STARTS_FROM_SPACE)) {
if (CollatorStringMatcher.cmatches(collator, cName, name, StringMatcherMode.CHECK_STARTS_FROM_SPACE)) {
if (resultMatcher.publish(c)) {
citiesToFill.add(c);
}

View file

@ -4,11 +4,11 @@ import java.text.Collator;
import java.util.ArrayList;
import java.util.List;
import net.osmand.CollatorStringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.osm.LatLon;
import net.osmand.plus.CollatorStringMatcher;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.CollatorStringMatcher.StringMatcherMode;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;

View file

@ -1,16 +1,17 @@
package net.osmand.plus.activities.search;
import java.util.ArrayList;
import java.util.List;
import net.osmand.CollatorStringMatcher;
import net.osmand.ResultMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.data.City;
import net.osmand.data.PostCode;
import net.osmand.data.Street;
import net.osmand.plus.CollatorStringMatcher;
import net.osmand.plus.R;
import net.osmand.plus.RegionAddressRepository;
import net.osmand.plus.CollatorStringMatcher.StringMatcherMode;
import net.osmand.plus.activities.OsmandApplication;
import android.os.AsyncTask;
import android.os.Message;