use of com.tyndalehouse.step.core.models.SearchToken in project step by STEPBible.
the class SearchPageController method populateMetaSearch.
/**
* Populates the meta data for a search
*
* @param req the request
* @param results the results
*/
private void populateMetaSearch(final HttpServletRequest req, final SearchResult results) {
try {
final List<SearchToken> searchTokens = results.getSearchTokens();
String scope = null;
StringBuilder keyInfo = new StringBuilder(128);
StringBuilder versions = new StringBuilder(32);
StringBuilder shortSearchWord = new StringBuilder(32);
StringBuilder longSearchWord = new StringBuilder(48);
boolean strongSearch = false;
for (SearchToken t : searchTokens) {
final String tokenType = t.getTokenType();
final String token = t.getToken();
final String infoSeparator = " | ";
if (SearchToken.VERSION.equals(tokenType)) {
versions.append(token);
versions.append(infoSeparator);
} else if (SearchToken.REFERENCE.equals(tokenType)) {
scope = token;
} else if (SearchToken.SUBJECT_SEARCH.equals(tokenType)) {
keyInfo.append(token);
versions.append(infoSeparator);
shortSearchWord.append(token);
} else if (SearchToken.NAVE_SEARCH.equals(tokenType)) {
keyInfo.append(token);
versions.append(infoSeparator);
shortSearchWord.append(token);
} else if (SearchToken.NAVE_SEARCH_EXTENDED.equals(tokenType)) {
keyInfo.append(token);
versions.append(infoSeparator);
shortSearchWord.append(token);
} else if (SearchToken.TEXT_SEARCH.equals(tokenType)) {
keyInfo.append(token);
versions.append(infoSeparator);
shortSearchWord.append(token);
} else if (SearchToken.STRONG_NUMBER.equals(tokenType)) {
strongSearch = true;
final LexiconSuggestion enhancedTokenInfo = (LexiconSuggestion) t.getEnhancedTokenInfo();
keyInfo.append(enhancedTokenInfo.getMatchingForm());
keyInfo.append(" - ");
keyInfo.append(enhancedTokenInfo.getGloss());
keyInfo.append(" - ");
keyInfo.append(enhancedTokenInfo.getStepTransliteration());
keyInfo.append(" - ");
keyInfo.append(enhancedTokenInfo.getStrongNumber());
keyInfo.append(infoSeparator);
shortSearchWord.append(enhancedTokenInfo.getStepTransliteration());
longSearchWord.append(enhancedTokenInfo.getStepTransliteration());
longSearchWord.append(", ");
longSearchWord.append(enhancedTokenInfo.getGloss());
longSearchWord.append(", ");
longSearchWord.append(enhancedTokenInfo.getStrongNumber());
} else if (SearchToken.MEANINGS.equals(tokenType)) {
keyInfo.append(token);
keyInfo.append(infoSeparator);
shortSearchWord.append(token);
} else if (SearchToken.TOPIC_BY_REF.equals(tokenType)) {
keyInfo.append(token);
keyInfo.append(infoSeparator);
shortSearchWord.append(token);
} else if (SearchToken.RELATED_VERSES.equals(tokenType)) {
keyInfo.append(token);
keyInfo.append(infoSeparator);
shortSearchWord.append(token);
} else if (SearchToken.EXACT_FORM.equals(tokenType)) {
keyInfo.append(token);
keyInfo.append(infoSeparator);
shortSearchWord.append(token);
} else if (SearchToken.SYNTAX.equals(tokenType)) {
keyInfo.append(token);
keyInfo.append(infoSeparator);
String tmpString = token;
if (token.substring(0, 2).equals("t=")) {
tmpString = tmpString.substring(2);
tmpString = tmpString.replaceAll(" AND ", ", ");
}
shortSearchWord.append(tmpString);
}
}
if (scope != null) {
keyInfo.append(scope);
keyInfo.append(" | ");
}
final String trimmedInfo = keyInfo.toString().trim();
final String relevantInfo = (keyInfo.length() > 2 ? trimmedInfo.substring(0, trimmedInfo.length() - 2) : trimmedInfo).replaceAll("\\|", ",");
req.setAttribute("description", ResourceBundle.getBundle("ErrorBundle").getString("search_results_for") + " " + relevantInfo);
try {
keyInfo.append(ResourceBundle.getBundle("ErrorBundle", clientSessionProvider.get().getLocale()).getString(results.getSearchType().getLanguageSearchKey()));
} catch (MissingResourceException ex) {
// swallow
LOGGER.warn("Missing resource for {}", results.getSearchType().getLanguageSearchKey(), ex);
keyInfo.append("Search");
}
String title = "";
String description = "";
if (strongSearch) {
title = shortSearchWord.toString() + " in STEP Bible with Greek and Hebrew helps";
description = "What the Bible says about " + longSearchWord.toString() + " in Bibles with Greek & Hebrew interlinear, search, study tools. Recommended by schools, Free.";
} else {
title = shortSearchWord.toString() + " in STEP Bible, study tools, 280 languages";
description = "What the Bible says about " + shortSearchWord.toString() + " in Bibles with original meaning, search, study helps, maps, topics, commentaries, meaning. Reliable, Free.";
}
// keyForTitle = "Bible verse about " + keyForTitle;
req.setAttribute("title", wrapTitle(title, results.getMasterVersion(), null));
req.setAttribute("canonicalUrl", req.getParameter("q"));
req.setAttribute("description", description);
} catch (Exception ex) {
// a page with no title is better than no pages
LOGGER.error("Unable to ascertain meta data", ex);
}
}
use of com.tyndalehouse.step.core.models.SearchToken in project step by STEPBible.
the class SearchController method suggest.
/**
* Suggests options to the user.
*
* @param input the input from the user
* @param context any specific user context, such as the selection of a book, or a particular master version
* already in the box
* @param referencesOnly true to indicate we only want references back
*/
@Timed(name = "suggest", group = "search", rateUnit = TimeUnit.SECONDS, durationUnit = TimeUnit.MILLISECONDS)
public List<AutoSuggestion> suggest(final String input, final String context, final String referencesOnly) {
boolean onlyReferences = false;
if (StringUtils.isNotBlank(referencesOnly)) {
onlyReferences = Boolean.parseBoolean(referencesOnly);
}
if (input.indexOf('=') != -1) {
return new ArrayList<AutoSuggestion>();
}
final List<AutoSuggestion> autoSuggestions = new ArrayList<AutoSuggestion>(128);
String bookContext = JSwordPassageService.REFERENCE_BOOK;
String referenceContext = null;
String limitType = null;
boolean exampleData = false;
if (StringUtils.isNotBlank(context)) {
// there are some context items... Parse them
// if there is a reference= restriction, then we will only return references, otherwise, we default
final List<SearchToken> searchTokens = parseTokens(context);
for (SearchToken st : searchTokens) {
if (SearchToken.VERSION.equals(st.getTokenType())) {
bookContext = st.getToken();
} else if (SearchToken.REFERENCE.equals(st.getTokenType())) {
referenceContext = st.getToken();
} else if (SearchToken.LIMIT.equals(st.getTokenType())) {
limitType = st.getToken();
} else if (SearchToken.EXAMPLE_DATA.equals(st.getTokenType())) {
exampleData = true;
}
}
}
if (onlyReferences || referenceContext != null) {
addReferenceSuggestions(limitType, input, autoSuggestions, bookContext, referenceContext);
} else {
addDefaultSuggestions(input, autoSuggestions, limitType, bookContext, exampleData);
}
return autoSuggestions;
}
use of com.tyndalehouse.step.core.models.SearchToken in project step by STEPBible.
the class SearchController method parseTokens.
/**
* Parses a string in the form of a=2|c=1 into a list of search tokens
*
* @param items
* @return
*/
private List<SearchToken> parseTokens(final String items) {
String[] tokens;
if (!StringUtils.isBlank(items)) {
tokens = SPLIT_TOKENS.split(items);
} else {
tokens = new String[0];
}
List<SearchToken> searchTokens = new ArrayList<SearchToken>();
for (String t : tokens) {
int indexOfPrefix = t.indexOf('=');
if (indexOfPrefix == -1) {
LOGGER.warn("Ignoring item: [{}]", t);
continue;
}
String text = t.substring(indexOfPrefix + 1);
searchTokens.add(new SearchToken(t.substring(0, indexOfPrefix), text));
}
return searchTokens;
}
Aggregations