Search in sources :

Example 1 with SubjectSuggestion

use of com.tyndalehouse.step.core.models.search.SubjectSuggestion in project step by STEPBible.

the class SubjectSuggestionServiceImpl method addExistingMappings.

/**
 * Adds the existing mappings back in
 * @param suggestions a list of suggestions
 * @param stemmer the stemmer itself
 * @param alreadyRetrieved the existing entries
 */
private void addExistingMappings(final Map<String, SubjectSuggestion> suggestions, final PorterStemmer stemmer, final SubjectSuggestion[] alreadyRetrieved) {
    if (alreadyRetrieved == null) {
        return;
    }
    for (SubjectSuggestion s : alreadyRetrieved) {
        stemmer.setCurrent(s.getValue());
        stemmer.stem();
        String stem = stemmer.getCurrent();
        suggestions.put(stem, s);
    }
}
Also used : SubjectSuggestion(com.tyndalehouse.step.core.models.search.SubjectSuggestion)

Example 2 with SubjectSuggestion

use of com.tyndalehouse.step.core.models.search.SubjectSuggestion 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()]);
}
Also used : PorterStemmer(org.tartarus.snowball.ext.PorterStemmer) SubjectSuggestion(com.tyndalehouse.step.core.models.search.SubjectSuggestion) TreeMap(java.util.TreeMap) TermsAndMaxCount(com.tyndalehouse.step.core.data.common.TermsAndMaxCount)

Example 3 with SubjectSuggestion

use of com.tyndalehouse.step.core.models.search.SubjectSuggestion in project step by STEPBible.

the class SubjectSuggestionServiceImpl method addSubjectTerms.

/**
 * @param suggestions the suggestions
 * @param stemmer     the stemmer
 * @param naveTerms   the nave terms
 * @param searchType  the search type
 * @return the actual number that was added, rather than marked as also available in a different search
 */
private int addSubjectTerms(final Map<String, SubjectSuggestion> suggestions, final PorterStemmer stemmer, final Collection<String> naveTerms, final SearchType searchType) {
    int added = 0;
    for (String s : naveTerms) {
        stemmer.setCurrent(s);
        stemmer.stem();
        String stem = stemmer.getCurrent();
        SubjectSuggestion suggestion = suggestions.get(stem);
        if (suggestion == null) {
            added++;
            suggestion = new SubjectSuggestion();
            suggestion.setValue(s);
            suggestion.addSearchType(searchType);
            suggestions.put(stem, suggestion);
        } else if (!suggestion.getSearchTypes().contains(searchType)) {
            suggestion.addSearchType(searchType);
            // if the suggestion's value is longer, then replace with the short value
            if (suggestion.getValue().length() > s.length()) {
                suggestion.setValue(s);
            }
        }
    }
    return added;
}
Also used : SubjectSuggestion(com.tyndalehouse.step.core.models.search.SubjectSuggestion)

Example 4 with SubjectSuggestion

use of com.tyndalehouse.step.core.models.search.SubjectSuggestion in project step by STEPBible.

the class SearchServiceImpl method enhanceSearchTokens.

/**
 * Enhances search tokens, meaning that <p /> for versions, we return the short initials and long initials in the
 * form of a 'BibleVersion' <p /> for references, we return the keywraper <p /> for strong numbers, we return the
 * lexicon suggestion <p /> for everything else, null.
 *
 * @param masterVersion the master version to use looking up references and so on.
 * @param searchTokens  a list of search tokens
 * @return with enhanced meta data if any
 */
private void enhanceSearchTokens(final String masterVersion, final List<SearchToken> searchTokens) {
    for (SearchToken st : searchTokens) {
        final String tokenType = st.getTokenType();
        if (SearchToken.VERSION.equals(tokenType)) {
            // probably need to show the short initials
            BibleVersion version = new BibleVersion();
            version.setInitials(this.versionResolver.getLongName(st.getToken()));
            version.setShortInitials(this.versionResolver.getShortName(st.getToken()));
            st.setEnhancedTokenInfo(version);
        } else if (SearchToken.REFERENCE.equals(tokenType)) {
            // could take the key but that has all parts combined
            final KeyWrapper kw = this.bibleInfoService.getKeyInfo(st.getToken(), masterVersion, masterVersion);
            String osisRef;
            if (kw.getKey() != null) {
                osisRef = kw.getKey().getOsisRef();
            } else {
                osisRef = kw.getOsisKeyId();
            }
            final BookName bookName = new BookName(kw.getName(), kw.getName(), BookName.Section.PASSAGE, false, osisRef);
            st.setEnhancedTokenInfo(bookName);
        } else if (SearchToken.STRONG_NUMBER.equals(tokenType)) {
            // hit the index and look up that strong number...
            st.setEnhancedTokenInfo(this.lexiconDefinitionService.lookup(st.getToken()));
        } else if (SearchToken.EXACT_FORM.equals(tokenType)) {
            ExactForm ef = new ExactForm();
            ef.setText(st.getToken());
            ef.setGreek(GreekUtils.isGreekText(st.getToken()));
            st.setEnhancedTokenInfo(ef);
        } else if (SearchToken.SUBJECT_SEARCH.equals(tokenType) || SearchToken.NAVE_SEARCH.equals(tokenType) || SearchToken.NAVE_SEARCH_EXTENDED.equals(tokenType)) {
            SubjectSuggestion ss = new SubjectSuggestion();
            ss.setValue(st.getToken());
            st.setEnhancedTokenInfo(ss);
        } else if (SearchToken.TEXT_SEARCH.equals(tokenType)) {
            final TextSuggestion textSuggestion = new TextSuggestion();
            textSuggestion.setText(st.getToken());
            st.setEnhancedTokenInfo(textSuggestion);
        } else if (SearchToken.MEANINGS.equals(tokenType)) {
            final LexiconSuggestion meaningSuggestion = new LexiconSuggestion();
            meaningSuggestion.setGloss(st.getToken());
            st.setEnhancedTokenInfo(meaningSuggestion);
        } else if (SearchToken.SYNTAX.equals(tokenType)) {
            SyntaxSuggestion ss = new SyntaxSuggestion();
            // take the first word, after stripping off any reference, etc.
            String syntax = st.getToken();
            syntax = IndividualSearch.MAIN_RANGE.matcher(syntax).replaceAll("").replaceAll("[()]+", "");
            if (StringUtils.isBlank(syntax)) {
                ss.setText("...");
            } else {
                int i = syntax.indexOf(' ');
                if (i != -1) {
                    ss.setText(String.format(SYNTAX_FORMAT, syntax.substring(0, i + 1)));
                } else {
                    ss.setText(String.format(SYNTAX_FORMAT, syntax));
                }
            }
            ss.setValue(st.getToken());
            st.setEnhancedTokenInfo(ss);
        } else if (SearchToken.TOPIC_BY_REF.equals(st.getTokenType())) {
            final TextSuggestion enhancedTokenInfo = new TextSuggestion();
            enhancedTokenInfo.setText(st.getToken());
            st.setEnhancedTokenInfo(enhancedTokenInfo);
        } else if (SearchToken.RELATED_VERSES.equals(st.getTokenType())) {
            final TextSuggestion enhancedTokenInfo = new TextSuggestion();
            enhancedTokenInfo.setText(st.getToken());
            st.setEnhancedTokenInfo(enhancedTokenInfo);
        }
    // nothing to do
    // for subject searches or
    // for text searches or
    // for meaning searches
    }
}
Also used : TextSuggestion(com.tyndalehouse.step.core.models.search.TextSuggestion) SyntaxSuggestion(com.tyndalehouse.step.core.models.search.SyntaxSuggestion) SubjectSuggestion(com.tyndalehouse.step.core.models.search.SubjectSuggestion)

Example 5 with SubjectSuggestion

use of com.tyndalehouse.step.core.models.search.SubjectSuggestion in project step by STEPBible.

the class SubjectSuggestionServiceImpl method getExactTerms.

@Override
public SubjectSuggestion[] getExactTerms(final SuggestionContext context, final int max, final boolean popularSort) {
    final Map<String, SubjectSuggestion> suggestions = new TreeMap<String, SubjectSuggestion>();
    final PorterStemmer stemmer = new PorterStemmer();
    // add the full term
    final String input = context.getInput();
    addSubjectTerms(suggestions, stemmer, LuceneUtils.getAllTermsPrefixedWith(true, false, this.jSwordSearchService.getIndexSearcher(JSwordPassageService.REFERENCE_BOOK), LuceneIndex.FIELD_HEADING, input, max).getTerms(), SearchType.SUBJECT_SIMPLE);
    addSubjectTerms(suggestions, stemmer, this.naves.findSetOfTerms(true, input, max, "root"), SearchType.SUBJECT_EXTENDED);
    addSubjectTerms(suggestions, stemmer, this.naves.findSetOfTerms(true, input, max, "fullTerm"), SearchType.SUBJECT_FULL);
    return suggestions.values().toArray(new SubjectSuggestion[suggestions.size()]);
}
Also used : PorterStemmer(org.tartarus.snowball.ext.PorterStemmer) SubjectSuggestion(com.tyndalehouse.step.core.models.search.SubjectSuggestion) TreeMap(java.util.TreeMap)

Aggregations

SubjectSuggestion (com.tyndalehouse.step.core.models.search.SubjectSuggestion)5 TreeMap (java.util.TreeMap)2 PorterStemmer (org.tartarus.snowball.ext.PorterStemmer)2 TermsAndMaxCount (com.tyndalehouse.step.core.data.common.TermsAndMaxCount)1 SyntaxSuggestion (com.tyndalehouse.step.core.models.search.SyntaxSuggestion)1 TextSuggestion (com.tyndalehouse.step.core.models.search.TextSuggestion)1