use of com.tyndalehouse.step.core.exceptions.TranslatedException in project step by STEPBible.
the class SearchServiceImpl method adaptQueryForMeaningSearch.
/**
* Looks up all the glosses for a particular word, and then adapts to strong search and continues as before
*
* @param sq search criteria
* @return a list of matching strongs
*/
private Set<String> adaptQueryForMeaningSearch(final SearchQuery sq) {
final String query = sq.getCurrentSearch().getQuery();
final QueryParser queryParser = new QueryParser(Version.LUCENE_30, "translationsStem", this.definitions.getAnalyzer());
queryParser.setDefaultOperator(Operator.OR);
try {
// we need to also add the step gloss, but since we need the analyser for stems,
// we want to use the query parser that does the tokenization for us
// could probably do better if required
String[] terms = StringUtils.split(query);
StringBuilder finalQuery = new StringBuilder();
for (String term : terms) {
final String escapedTerm = QueryParser.escape(term);
finalQuery.append(escapedTerm);
finalQuery.append(" stepGlossStem:");
finalQuery.append(escapedTerm);
}
final Query parsed = queryParser.parse("-stopWord:true " + finalQuery.toString());
final EntityDoc[] matchingMeanings = this.definitions.search(parsed);
final Set<String> strongs = new HashSet<String>(matchingMeanings.length);
for (final EntityDoc d : matchingMeanings) {
final String strongNumber = d.get(STRONG_NUMBER_FIELD);
if (isInFilter(strongNumber, sq)) {
strongs.add(strongNumber);
}
}
final String textQuery = getQuerySyntaxForStrongs(strongs, sq);
sq.getCurrentSearch().setQuery(textQuery, true);
sq.setDefinitions(matchingMeanings);
// return the strongs that the search will match
return strongs;
} catch (final ParseException e) {
throw new TranslatedException(e, "search_invalid");
}
}
use of com.tyndalehouse.step.core.exceptions.TranslatedException in project step by STEPBible.
the class SearchServiceImpl method findByTransliteration.
/**
* Runs the transliteration rules on the input in an attempt to match an entry in the lexicon
*
* @param query the query to be found
* @param isGreek true to indicate Greek, false to indicate Hebrew
* @return the strongs that have been found/matched.
*/
private Set<String> findByTransliteration(final String query, final boolean isGreek) {
// first find by transliterations that we have
final String lowerQuery = query.toLowerCase(Locale.ENGLISH);
final String simplifiedTransliteration = OriginalWordSuggestionServiceImpl.getSimplifiedTransliterationClause(isGreek, lowerQuery, false);
final EntityDoc[] specificFormEntities = this.specificForms.searchSingleColumn("simplifiedTransliteration", simplifiedTransliteration, getFilter(isGreek));
// finally, if we haven't found anything, then abort
if (specificFormEntities.length != 0) {
final Set<String> strongs = new HashSet<String>(specificFormEntities.length);
// nothing to search for..., so abort query
for (final EntityDoc f : specificFormEntities) {
strongs.add(f.get(STRONG_NUMBER_FIELD));
}
return strongs;
}
final MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[] { "simplifiedTransliteration", "stepTransliteration", "otherTransliteration" }, this.definitions.getAnalyzer());
try {
final Query luceneQuery = queryParser.parse("-stopWord:true " + lowerQuery);
final EntityDoc[] results = this.definitions.search(luceneQuery);
if (results.length == 0) {
throw new AbortQueryException("No definitions found for input");
}
final Set<String> strongs = new HashSet<String>(results.length);
for (final EntityDoc d : results) {
strongs.add(d.get(STRONG_NUMBER_FIELD));
}
return strongs;
} catch (final ParseException e) {
throw new TranslatedException(e, "search_invalid");
}
}
use of com.tyndalehouse.step.core.exceptions.TranslatedException in project step by STEPBible.
the class JSwordPassageServiceImpl method getPlainText.
@Override
public String getPlainText(final String version, final String reference, final boolean firstVerse) {
final Book book = this.versificationService.getBookFromVersion(version);
try {
Key key = book.getKey(reference);
if (firstVerse) {
key = getFirstVerseExcludingZero(key, book);
}
final BookData data = new BookData(book, key);
return OSISUtil.getCanonicalText(data.getOsisFragment());
} catch (final BookException e) {
throw new LocalisedException(e, e.getMessage());
} catch (final NoSuchKeyException e) {
throw new TranslatedException(e, "invalid_reference_in_book", reference, version);
}
}
use of com.tyndalehouse.step.core.exceptions.TranslatedException in project step by STEPBible.
the class JSwordPassageServiceImpl method getAllReferencesAndCounts.
/**
* @param references a list of references to be parsed
* @param version the version against which the refs are parsed
* @return a String representing all the references
*/
@Override
public StringAndCount getAllReferencesAndCounts(final String references, final String version) {
int count = 0;
// TODO - can be refactored to optimize the reference query when used in subject searches...
final PassageKeyFactory keyFactory = PassageKeyFactory.instance();
final Versification av11n = this.versificationService.getVersificationForVersion(version);
final StringBuilder referenceString = new StringBuilder(1024);
try {
final Key k = keyFactory.getKey(av11n, references);
final Iterator<Key> iterator = k.iterator();
while (iterator.hasNext()) {
referenceString.append(iterator.next().getOsisID());
count++;
if (iterator.hasNext()) {
referenceString.append(' ');
}
}
return new StringAndCount(referenceString.toString(), count);
} catch (final NoSuchKeyException e) {
throw new TranslatedException(e, "invalid_reference_in_book", references, version);
}
}
use of com.tyndalehouse.step.core.exceptions.TranslatedException in project step by STEPBible.
the class JSwordPassageServiceImpl method removeDifferentLanguageIfCompare.
/**
* Checks that if comparing, we are looking at versions of the same language, or at least two of them
*
* @param displayMode the display mode
* @param books the books that have been found
*/
private Book[] removeDifferentLanguageIfCompare(final InterlinearMode displayMode, final Book[] books) {
if (books.length == 0) {
return books;
}
if (!isComparingMode(displayMode)) {
return books;
}
final String firstLanguage = books[0].getLanguage().getCode();
final List<Book> booksOfSameLanguage = new ArrayList<Book>();
// check that we have at least two books of the same language
for (final Book b : books) {
if (firstLanguage.equals(b.getLanguage().getCode())) {
booksOfSameLanguage.add(b);
}
}
if (booksOfSameLanguage.size() < 2) {
throw new TranslatedException("translations_in_different_languages");
}
return booksOfSameLanguage.toArray(new Book[0]);
}
Aggregations