-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5401 from ibi-group/geocoder-fuzziness
Improve geocoding fuzziness, remove street corners
- Loading branch information
Showing
10 changed files
with
269 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/ext-test/java/org/opentripplanner/ext/geocoder/EnglishNgramAnalyzerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.opentripplanner.ext.geocoder; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.apache.lucene.analysis.Analyzer; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class EnglishNgramAnalyzerTest { | ||
|
||
@Test | ||
void ngram() throws IOException { | ||
var analyzer = new EnglishNGramAnalyzer(); | ||
List<String> result = analyze("Alexanderplatz", analyzer); | ||
|
||
//System.out.println(result.stream().collect(Collectors.joining("\",\"", "\"", "\""))); | ||
assertEquals( | ||
List.of( | ||
"Alex", | ||
"Alexa", | ||
"Alexan", | ||
"Alexand", | ||
"Alexande", | ||
"Alexander", | ||
"Alexanderp", | ||
"lexa", | ||
"lexan", | ||
"lexand", | ||
"lexande", | ||
"lexander", | ||
"lexanderp", | ||
"lexanderpl", | ||
"exan", | ||
"exand", | ||
"exande", | ||
"exander", | ||
"exanderp", | ||
"exanderpl", | ||
"exanderpla", | ||
"xand", | ||
"xande", | ||
"xander", | ||
"xanderp", | ||
"xanderpl", | ||
"xanderpla", | ||
"xanderplat", | ||
"ande", | ||
"ander", | ||
"anderp", | ||
"anderpl", | ||
"anderpla", | ||
"anderplat", | ||
"anderplatz", | ||
"nder", | ||
"nderp", | ||
"nderpl", | ||
"nderpla", | ||
"nderplat", | ||
"nderplatz", | ||
"derp", | ||
"derpl", | ||
"derpla", | ||
"derplat", | ||
"derplatz", | ||
"erpl", | ||
"erpla", | ||
"erplat", | ||
"erplatz", | ||
"rpla", | ||
"rplat", | ||
"rplatz", | ||
"plat", | ||
"platz", | ||
"latz", | ||
"Alexanderplatz" | ||
), | ||
result | ||
); | ||
} | ||
|
||
public List<String> analyze(String text, Analyzer analyzer) throws IOException { | ||
List<String> result = new ArrayList<>(); | ||
TokenStream tokenStream = analyzer.tokenStream("name", text); | ||
CharTermAttribute attr = tokenStream.addAttribute(CharTermAttribute.class); | ||
tokenStream.reset(); | ||
while (tokenStream.incrementToken()) { | ||
result.add(attr.toString()); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/ext/java/org/opentripplanner/ext/geocoder/EnglishNGramAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.opentripplanner.ext.geocoder; | ||
|
||
import org.apache.lucene.analysis.Analyzer; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.core.LowerCaseFilter; | ||
import org.apache.lucene.analysis.core.StopFilter; | ||
import org.apache.lucene.analysis.en.EnglishAnalyzer; | ||
import org.apache.lucene.analysis.en.EnglishPossessiveFilter; | ||
import org.apache.lucene.analysis.en.PorterStemFilter; | ||
import org.apache.lucene.analysis.miscellaneous.CapitalizationFilter; | ||
import org.apache.lucene.analysis.ngram.NGramTokenFilter; | ||
import org.apache.lucene.analysis.standard.StandardTokenizer; | ||
|
||
/** | ||
* A custom analyzer for stop names. It removes english stop words (at,the...) and splits | ||
* the input into NGrams (https://en.wikipedia.org/wiki/N-gram) so that the middle | ||
* of a stop name can be matched efficiently. | ||
* <p> | ||
* For example the query of "exanderpl" will match the stop name "Alexanderplatz". | ||
*/ | ||
class EnglishNGramAnalyzer extends Analyzer { | ||
|
||
@Override | ||
protected TokenStreamComponents createComponents(String fieldName) { | ||
StandardTokenizer src = new StandardTokenizer(); | ||
TokenStream result = new EnglishPossessiveFilter(src); | ||
result = new LowerCaseFilter(result); | ||
result = new StopFilter(result, EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); | ||
result = new PorterStemFilter(result); | ||
result = new CapitalizationFilter(result); | ||
result = new NGramTokenFilter(result, 4, 10, true); | ||
return new TokenStreamComponents(src, result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.