Search in sources :

Example 11 with TranslatedException

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");
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) MultiFieldQueryParser(org.apache.lucene.queryParser.MultiFieldQueryParser) Query(org.apache.lucene.search.Query) SearchQuery(com.tyndalehouse.step.core.service.impl.SearchQuery) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) ParseException(org.apache.lucene.queryParser.ParseException) HashSet(java.util.HashSet)

Example 12 with TranslatedException

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");
    }
}
Also used : MultiFieldQueryParser(org.apache.lucene.queryParser.MultiFieldQueryParser) Query(org.apache.lucene.search.Query) SearchQuery(com.tyndalehouse.step.core.service.impl.SearchQuery) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) ParseException(org.apache.lucene.queryParser.ParseException) HashSet(java.util.HashSet) AbortQueryException(com.tyndalehouse.step.core.service.impl.AbortQueryException)

Example 13 with TranslatedException

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);
    }
}
Also used : LocalisedException(com.tyndalehouse.step.core.exceptions.LocalisedException) NoSuchKeyException(org.crosswire.jsword.passage.NoSuchKeyException) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) BibleBook(org.crosswire.jsword.versification.BibleBook) Book(org.crosswire.jsword.book.Book) BookException(org.crosswire.jsword.book.BookException) BookData(org.crosswire.jsword.book.BookData) Key(org.crosswire.jsword.passage.Key)

Example 14 with TranslatedException

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);
    }
}
Also used : NoSuchKeyException(org.crosswire.jsword.passage.NoSuchKeyException) StringAndCount(com.tyndalehouse.step.core.models.StringAndCount) PassageKeyFactory(org.crosswire.jsword.passage.PassageKeyFactory) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) Versification(org.crosswire.jsword.versification.Versification) Key(org.crosswire.jsword.passage.Key)

Example 15 with TranslatedException

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]);
}
Also used : TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) BibleBook(org.crosswire.jsword.versification.BibleBook) Book(org.crosswire.jsword.book.Book) ArrayList(java.util.ArrayList) XMLUtil.writeToString(org.crosswire.common.xml.XMLUtil.writeToString)

Aggregations

TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)15 Book (org.crosswire.jsword.book.Book)8 Key (org.crosswire.jsword.passage.Key)6 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)6 BibleBook (org.crosswire.jsword.versification.BibleBook)5 EntityDoc (com.tyndalehouse.step.core.data.EntityDoc)4 SearchQuery (com.tyndalehouse.step.core.service.impl.SearchQuery)4 ParseException (org.apache.lucene.queryParser.ParseException)4 Query (org.apache.lucene.search.Query)4 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)3 StringAndCount (com.tyndalehouse.step.core.models.StringAndCount)3 HashSet (java.util.HashSet)3 MultiFieldQueryParser (org.apache.lucene.queryParser.MultiFieldQueryParser)3 Versification (org.crosswire.jsword.versification.Versification)3 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)2 QueryParser (org.apache.lucene.queryParser.QueryParser)2 BookException (org.crosswire.jsword.book.BookException)2 Passage (org.crosswire.jsword.passage.Passage)2 DirectoryListingInstaller (com.tyndalehouse.step.core.data.DirectoryListingInstaller)1 ValidationException (com.tyndalehouse.step.core.exceptions.ValidationException)1