| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LuceneAnalyzer |
|
| 1.5;1.5 |
| 1 | /** | |
| 2 | * Distribution License: | |
| 3 | * JSword is free software; you can redistribute it and/or modify it under | |
| 4 | * the terms of the GNU Lesser General Public License, version 2.1 or later | |
| 5 | * as published by the Free Software Foundation. This program is distributed | |
| 6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
| 7 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 8 | * See the GNU Lesser General Public License for more details. | |
| 9 | * | |
| 10 | * The License is available on the internet at: | |
| 11 | * http://www.gnu.org/copyleft/lgpl.html | |
| 12 | * or by writing to: | |
| 13 | * Free Software Foundation, Inc. | |
| 14 | * 59 Temple Place - Suite 330 | |
| 15 | * Boston, MA 02111-1307, USA | |
| 16 | * | |
| 17 | * © CrossWire Bible Society, 2005 - 2016 | |
| 18 | * | |
| 19 | */ | |
| 20 | package org.crosswire.jsword.index.lucene.analysis; | |
| 21 | ||
| 22 | import java.io.Reader; | |
| 23 | ||
| 24 | import org.apache.lucene.analysis.Analyzer; | |
| 25 | import org.apache.lucene.analysis.PerFieldAnalyzerWrapper; | |
| 26 | import org.apache.lucene.analysis.SimpleAnalyzer; | |
| 27 | import org.apache.lucene.analysis.TokenStream; | |
| 28 | import org.crosswire.jsword.book.Book; | |
| 29 | import org.crosswire.jsword.index.lucene.IndexMetadata; | |
| 30 | import org.crosswire.jsword.index.lucene.InstalledIndex; | |
| 31 | import org.crosswire.jsword.index.lucene.LuceneIndex; | |
| 32 | import org.slf4j.Logger; | |
| 33 | import org.slf4j.LoggerFactory; | |
| 34 | ||
| 35 | /** | |
| 36 | * A specialized analyzer for Books that analyzes different fields differently. | |
| 37 | * This is book specific since it is possible that each book has specialized | |
| 38 | * search requirements. | |
| 39 | * | |
| 40 | * Uses AnalyzerFactory for InstalledIndexVersion > 1.1 | |
| 41 | * | |
| 42 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
| 43 | * @author DM Smith | |
| 44 | */ | |
| 45 | public class LuceneAnalyzer extends Analyzer { | |
| 46 | ||
| 47 | 0 | public LuceneAnalyzer(Book book) { |
| 48 | // The default analysis | |
| 49 | 0 | analyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer()); |
| 50 | ||
| 51 | 0 | if (InstalledIndex.instance().getInstalledIndexDefaultVersion() > IndexMetadata.INDEX_VERSION_1_1) { |
| 52 | // Content is analyzed using natural language analyzer | |
| 53 | // (stemming, stopword etc) | |
| 54 | 0 | Analyzer myNaturalLanguageAnalyzer = AnalyzerFactory.getInstance().createAnalyzer(book); |
| 55 | 0 | analyzer.addAnalyzer(LuceneIndex.FIELD_BODY, myNaturalLanguageAnalyzer); |
| 56 | //analyzer.addAnalyzer(LuceneIndex.FIELD_HEADING, myNaturalLanguageAnalyzer); //heading to use same analyzer as BODY | |
| 57 | //analyzer.addAnalyzer(LuceneIndex.FIELD_INTRO, myNaturalLanguageAnalyzer); | |
| 58 | 0 | log.debug("{}: Using languageAnalyzer: {}", book.getBookMetaData().getInitials(), myNaturalLanguageAnalyzer.getClass().getName()); |
| 59 | } | |
| 60 | ||
| 61 | // Keywords are normalized to osisIDs | |
| 62 | 0 | analyzer.addAnalyzer(LuceneIndex.FIELD_KEY, new KeyAnalyzer()); |
| 63 | ||
| 64 | // Strong's Numbers are normalized to a consistent representation | |
| 65 | 0 | analyzer.addAnalyzer(LuceneIndex.FIELD_STRONG, new StrongsNumberAnalyzer()); |
| 66 | ||
| 67 | // Strong's Numbers and Robinson's morphological codes are normalized to a consistent representation | |
| 68 | 0 | analyzer.addAnalyzer(LuceneIndex.FIELD_MORPHOLOGY, new MorphologyAnalyzer()); |
| 69 | ||
| 70 | // XRefs are normalized from ranges into a list of osisIDs | |
| 71 | 0 | analyzer.addAnalyzer(LuceneIndex.FIELD_XREF, new XRefAnalyzer()); |
| 72 | ||
| 73 | ||
| 74 | 0 | } |
| 75 | ||
| 76 | @Override | |
| 77 | public TokenStream tokenStream(String fieldName, Reader reader) { | |
| 78 | 0 | return analyzer.tokenStream(fieldName, reader); |
| 79 | } | |
| 80 | ||
| 81 | private PerFieldAnalyzerWrapper analyzer; | |
| 82 | 0 | private static final Logger log = LoggerFactory.getLogger(LuceneAnalyzer.class); |
| 83 | ||
| 84 | } |