类org.apache.lucene.search.spell.StringDistance源码实例Demo

下面列出了怎么用org.apache.lucene.search.spell.StringDistance的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: lucene-solr   文件: ValueSourceParser.java
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {

  ValueSource str1 = fp.parseValueSource();
  ValueSource str2 = fp.parseValueSource();
  String distClass = fp.parseArg();

  StringDistance dist = null;
  if (distClass.equalsIgnoreCase("jw")) {
    dist = new JaroWinklerDistance();
  } else if (distClass.equalsIgnoreCase("edit")) {
    dist = new LevenshteinDistance();
  } else if (distClass.equalsIgnoreCase("ngram")) {
    int ngram = 2;
    if (fp.hasMoreArguments()) {
      ngram = fp.parseInt();
    }
    dist = new NGramDistance(ngram);
  } else {
    dist = fp.req.getCore().getResourceLoader().newInstance(distClass, StringDistance.class);
  }
  return new StringDistanceFunction(str1, str2, dist);
}
 
源代码2 项目: lucene-solr   文件: ConjunctionSolrSpellChecker.java
@Override
protected StringDistance getStringDistance() {
  if (stringDistance == null) {
    return super.getStringDistance();
  }
  return stringDistance;
}
 
@Test
public void test() throws Exception {
  ConjunctionSolrSpellChecker cssc = new ConjunctionSolrSpellChecker();
  @SuppressWarnings("unchecked")
  Class<StringDistance> sameDistance = (Class<StringDistance>) AVAILABLE_DISTANCES[random().nextInt(AVAILABLE_DISTANCES.length)];
  
  StringDistance sameDistance1 = sameDistance.getConstructor().newInstance();
  StringDistance sameDistance2 = sameDistance.getConstructor().newInstance();
  
  //NGramDistance defaults to 2, so we'll try 3 or 4 to ensure we have one that is not-equal.
  StringDistance differentDistance = new NGramDistance(3);
  if(sameDistance1.equals(differentDistance)) {
    differentDistance = new NGramDistance(4);
    if(sameDistance1.equals(differentDistance)) {
      fail("Cannot set up test.  2 NGramDistances with different gram sizes should not be equal.");
    }
  }
  Assert.assertEquals("The distance " + sameDistance + " does not properly implement equals.", sameDistance1, sameDistance2);
  
  
  MockSolrSpellChecker checker1 = new MockSolrSpellChecker(sameDistance1);
  MockSolrSpellChecker checker2 = new MockSolrSpellChecker(sameDistance2);
  MockSolrSpellChecker checker3 = new MockSolrSpellChecker(differentDistance);
  
  cssc.addChecker(checker1);
  cssc.addChecker(checker2);
  expectThrows(IllegalArgumentException.class, () -> cssc.addChecker(checker3));
}
 
源代码4 项目: lucene-solr   文件: IndexBasedSpellCheckerTest.java
@Test
@SuppressWarnings({"unchecked"})
public void testAlternateDistance() throws Exception {
  TestSpellChecker checker = new TestSpellChecker();
  @SuppressWarnings({"rawtypes"})
  NamedList spellchecker = new NamedList();
  spellchecker.add("classname", IndexBasedSpellChecker.class.getName());

  File indexDir = createTempDir().toFile();
  spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
  spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title");
  spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
  spellchecker.add(AbstractLuceneSpellChecker.STRING_DISTANCE, JaroWinklerDistance.class.getName());
  SolrCore core = h.getCore();
  String dictName = checker.init(spellchecker, core);
  assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME,
          dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true);
  h.getCore().withSearcher(searcher -> {
    checker.build(core, searcher);
    SpellChecker sc = checker.getSpellChecker();
    assertTrue("sc is null and it shouldn't be", sc != null);
    StringDistance sd = sc.getStringDistance();
    assertTrue("sd is null and it shouldn't be", sd != null);
    assertTrue("sd is not an instance of " + JaroWinklerDistance.class.getName(), sd instanceof JaroWinklerDistance);
    return null;
  });

}
 
public StringDistance stringDistance() {
    return stringDistance;
}
 
public void stringDistance(StringDistance distance) {
    this.stringDistance = distance;
}
 
源代码7 项目: lucene-solr   文件: SolrSpellChecker.java
/**
 * Get the distance implementation used by this spellchecker, or NULL if not applicable.
 */
protected StringDistance getStringDistance()  {
  throw new UnsupportedOperationException();
}
 
源代码8 项目: lucene-solr   文件: DirectSolrSpellChecker.java
@Override
@SuppressWarnings({"unchecked"})
public String init(@SuppressWarnings({"rawtypes"})NamedList config, SolrCore core) {

  SolrParams params = config.toSolrParams();

  log.info("init: {}", config);
  String name = super.init(config, core);
  
  Comparator<SuggestWord> comp = SuggestWordQueue.DEFAULT_COMPARATOR;
  String compClass = (String) config.get(COMPARATOR_CLASS);
  if (compClass != null) {
    if (compClass.equalsIgnoreCase(SCORE_COMP))
      comp = SuggestWordQueue.DEFAULT_COMPARATOR;
    else if (compClass.equalsIgnoreCase(FREQ_COMP))
      comp = new SuggestWordFrequencyComparator();
    else //must be a FQCN
      comp = (Comparator<SuggestWord>) core.getResourceLoader().newInstance(compClass, Comparator.class);
  }
  
  StringDistance sd = DirectSpellChecker.INTERNAL_LEVENSHTEIN;
  String distClass = (String) config.get(STRING_DISTANCE);
  if (distClass != null && !distClass.equalsIgnoreCase(INTERNAL_DISTANCE))
    sd = core.getResourceLoader().newInstance(distClass, StringDistance.class);

  float minAccuracy = DEFAULT_ACCURACY;
  Float accuracy = params.getFloat(ACCURACY);
  if (accuracy != null)
    minAccuracy = accuracy;
  
  int maxEdits = DEFAULT_MAXEDITS;
  Integer edits = params.getInt(MAXEDITS);
  if (edits != null)
    maxEdits = edits;
  
  int minPrefix = DEFAULT_MINPREFIX;
  Integer prefix = params.getInt(MINPREFIX);
  if (prefix != null)
    minPrefix = prefix;
  
  int maxInspections = DEFAULT_MAXINSPECTIONS;
  Integer inspections = params.getInt(MAXINSPECTIONS);
  if (inspections != null)
    maxInspections = inspections;
  
  float minThreshold = DEFAULT_THRESHOLD_TOKEN_FREQUENCY;
  Float threshold = params.getFloat(THRESHOLD_TOKEN_FREQUENCY);
  if (threshold != null)
    minThreshold = threshold;
  
  int minQueryLength = DEFAULT_MINQUERYLENGTH;
  Integer queryLength = params.getInt(MINQUERYLENGTH);
  if (queryLength != null)
    minQueryLength = queryLength;

  int maxQueryLength = DEFAULT_MAXQUERYLENGTH;
  Integer overriddenMaxQueryLength = params.getInt(MAXQUERYLENGTH);
  if (overriddenMaxQueryLength != null)
    maxQueryLength = overriddenMaxQueryLength;
  
  float maxQueryFrequency = DEFAULT_MAXQUERYFREQUENCY;
  Float queryFreq = params.getFloat(MAXQUERYFREQUENCY);
  if (queryFreq != null)
    maxQueryFrequency = queryFreq;
  
  checker.setComparator(comp);
  checker.setDistance(sd);
  checker.setMaxEdits(maxEdits);
  checker.setMinPrefix(minPrefix);
  checker.setAccuracy(minAccuracy);
  checker.setThresholdFrequency(minThreshold);
  checker.setMaxInspections(maxInspections);
  checker.setMinQueryLength(minQueryLength);
  checker.setMaxQueryLength(maxQueryLength);
  checker.setMaxQueryFrequency(maxQueryFrequency);
  checker.setLowerCaseTerms(false);
  
  return name;
}
 
源代码9 项目: lucene-solr   文件: DirectSolrSpellChecker.java
@Override
public StringDistance getStringDistance() {
  return checker.getDistance();
}
 
源代码10 项目: lucene-solr   文件: AbstractLuceneSpellChecker.java
@Override
public StringDistance getStringDistance() {
  return sd;
}
 
MockSolrSpellChecker(StringDistance sd) {
  this.sd = sd;
}
 
@Override
protected StringDistance getStringDistance() {
  return sd;
}
 
源代码13 项目: lucene-solr   文件: StringDistanceFunction.java
public StringDistanceFunction(ValueSource str1, ValueSource str2, StringDistance measure) {
  this.str1 = str1;
  this.str2 = str2;
  dist = measure;


}
 
 类所在包
 类方法
 同包方法