use of com.tyndalehouse.step.core.data.common.TermsAndMaxCount in project step by STEPBible.
the class SubjectSuggestionServiceImpl method collectNonExactMatches.
@Override
public SubjectSuggestion[] collectNonExactMatches(final TermsAndMaxCount<SubjectSuggestion> collector, final SuggestionContext context, final SubjectSuggestion[] alreadyRetrieved, final int leftToCollect) {
final Map<String, SubjectSuggestion> suggestions = new TreeMap<String, SubjectSuggestion>();
final PorterStemmer stemmer = new PorterStemmer();
addExistingMappings(suggestions, stemmer, alreadyRetrieved);
final String input = context.getInput();
final TermsAndMaxCount termsFromHeadings = LuceneUtils.getAllTermsPrefixedWith(false, false, this.jSwordSearchService.getIndexSearcher(JSwordPassageService.REFERENCE_BOOK), LuceneIndex.FIELD_HEADING, input, leftToCollect);
final TermsAndMaxCount termsFromSimpleNave = this.naves.findSetOfTermsWithCounts(false, true, input, leftToCollect, "root");
final TermsAndMaxCount termsFromFullNave = this.naves.findSetOfTermsWithCounts(false, true, input, leftToCollect, "fullTerm");
addSubjectTerms(suggestions, stemmer, termsFromHeadings.getTerms(), SearchType.SUBJECT_SIMPLE);
addSubjectTerms(suggestions, stemmer, termsFromSimpleNave.getTerms(), SearchType.SUBJECT_EXTENDED);
addSubjectTerms(suggestions, stemmer, termsFromFullNave.getTerms(), SearchType.SUBJECT_FULL);
// termsFromHeadings.setTotalCount(termsFromHeadings.getTotalCount() - addSubjectTerms(suggestions, stemmer, termsFromHeadings.getTerms(), SearchType.SUBJECT_SIMPLE));
// termsFromSimpleNave.setTotalCount(termsFromSimpleNave.getTotalCount() - addSubjectTerms(suggestions, stemmer, termsFromSimpleNave.getTerms(), SearchType.SUBJECT_EXTENDED));
// termsFromFullNave.setTotalCount(termsFromFullNave.getTotalCount() - addSubjectTerms(suggestions, stemmer, termsFromFullNave.getTerms(), SearchType.SUBJECT_FULL));
TermsAndMaxCount countsAndResults = new TermsAndMaxCount();
countsAndResults.setTerms(new HashSet<SubjectSuggestion>(suggestions.values()));
collector.setTotalCount(suggestions.size());
return suggestions.values().toArray(new SubjectSuggestion[countsAndResults.getTerms().size()]);
}
use of com.tyndalehouse.step.core.data.common.TermsAndMaxCount in project step by STEPBible.
the class MeaningSuggestionServiceImpl method collectNonExactMatches.
@Override
public String[] collectNonExactMatches(final TermsAndMaxCount<String> collector, final SuggestionContext context, final String[] alreadyRetrieved, final int leftToCollect) {
TermsAndMaxCount countsAndResults = this.definitions.findSetOfTermsWithCounts(false, true, context.getInput(), collector.getTotalCount(), ANCIENT_MEANING_FIELDS);
final Set<String> resultTerms = countsAndResults.getTerms();
collector.setTotalCount(countsAndResults.getTotalCount());
collector.setTerms(countsAndResults.getTerms());
return resultTerms.toArray(new String[resultTerms.size()]);
}
use of com.tyndalehouse.step.core.data.common.TermsAndMaxCount in project step by STEPBible.
the class LuceneUtils method getAllTermsPrefixedWith.
/**
* Returns all terms starting with a particular prefix
*
* @param exact indicates we want 'exact' matches only
* @param fieldName the name of the fields
* @param searchTerm the search term
* @return the list of terms matching searchTerm as a prefix
*/
public static TermsAndMaxCount getAllTermsPrefixedWith(final boolean exact, final boolean trackMax, IndexSearcher searcher, final String fieldName, final String searchTerm, final int max) {
final String lastTerm = getLastTerm(searchTerm);
if (StringUtils.isBlank(lastTerm)) {
return getBlankTermsAndMaxCount();
}
TermEnum termEnum = null;
try {
final Term term = new Term(fieldName, QueryParser.escape(lastTerm.toLowerCase().trim()));
termEnum = exact ? new SingleTermEnum(searcher.getIndexReader(), term) : new PrefixTermEnum(searcher.getIndexReader(), term);
int count = 0;
if (termEnum.term() == null) {
return getBlankTermsAndMaxCount();
}
final Set<String> terms = new HashSet<String>();
do {
if (count < max) {
// when inexact, don't include exact terms
final String termValue = termEnum.term().text();
if (!exact && termValue.equalsIgnoreCase(searchTerm)) {
// we didn't really find a term after all, since it's the exact same term
count--;
} else {
terms.add(termValue);
}
}
count++;
// we continue round the loop until we've got enough, or in case we're wanting to keep track of the total number
} while (termEnum.next() && ((count < max) || trackMax || count < MAX_TRACK));
// finalise and return
TermsAndMaxCount termsAndMaxCount = new TermsAndMaxCount();
termsAndMaxCount.setTotalCount(count);
termsAndMaxCount.setTerms(terms);
return termsAndMaxCount;
} catch (IOException ex) {
throw new StepInternalException(ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(termEnum);
}
}
use of com.tyndalehouse.step.core.data.common.TermsAndMaxCount in project step by STEPBible.
the class EntityIndexReaderImpl method findSetOfTermsWithCounts.
@Override
public TermsAndMaxCount findSetOfTermsWithCounts(final boolean exact, final boolean trackMax, String searchTerm, int maxReturned, final String... fieldNames) {
TermsAndMaxCount hits = new TermsAndMaxCount();
if (fieldNames.length == 0) {
hits.setTerms(new HashSet<String>(0));
return hits;
}
if (fieldNames.length == 1) {
return LuceneUtils.getAllTermsPrefixedWith(exact, trackMax, this.searcher, fieldNames[0], searchTerm, maxReturned);
}
hits.setTerms(new HashSet<String>(32));
for (int ii = 0; ii < fieldNames.length; ii++) {
final TermsAndMaxCount termsByField = LuceneUtils.getAllTermsPrefixedWith(exact, trackMax, this.searcher, fieldNames[ii], searchTerm, maxReturned);
hits.getTerms().addAll(termsByField.getTerms());
hits.setTotalCount(hits.getTotalCount() + termsByField.getTotalCount());
}
// total count, is count - the existing ters
hits.setTotalCount(hits.getTotalCount() - hits.getTerms().size());
return hits;
}
use of com.tyndalehouse.step.core.data.common.TermsAndMaxCount in project step by STEPBible.
the class LuceneUtils method getBlankTermsAndMaxCount.
private static TermsAndMaxCount getBlankTermsAndMaxCount() {
TermsAndMaxCount termsAndMaxCount = new TermsAndMaxCount();
termsAndMaxCount.setTerms(new HashSet<String>());
return termsAndMaxCount;
}
Aggregations