List of usage examples for org.apache.lucene.analysis.synonym SynonymFilterFactory SynonymFilterFactory
public SynonymFilterFactory(Map<String, String> args)
From source file:cn.howardliu.lucene.extension.ManagedSynonymFilterFactory.java
License:Apache License
/** * Called once, during core initialization, to initialize any analysis components * that depend on the data managed by this resource. It is important that the * analysis component is only initialized once during core initialization so that * text analysis is consistent, especially in a distributed environment, as we * don't want one server applying a different set of stop words than other servers. *///from www .j a v a2 s. c o m @SuppressWarnings("unchecked") @Override public void onManagedResourceInitialized(NamedList<?> initArgs, final ManagedResource res) throws SolrException { NamedList<Object> args = (NamedList<Object>) initArgs; args.add("synonyms", getResourceId()); args.add("expand", "false"); args.add("format", "solr"); Map<String, String> filtArgs = new HashMap<>(); for (Map.Entry<String, ?> entry : args) { filtArgs.put(entry.getKey(), entry.getValue().toString()); } // create the actual filter factory that pulls the synonym mappings // from synonymMappings using a custom parser implementation delegate = new SynonymFilterFactory(filtArgs) { @Override protected SynonymMap loadSynonyms(ResourceLoader loader, String cname, boolean dedup, Analyzer analyzer) throws IOException, ParseException { CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder() .onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT); ManagedSynonymParser parser = new ManagedSynonymParser((SynonymManager) res, dedup, analyzer); // ??? InputStreamReader in = null; if (StringUtils.isNotBlank(synonymFile)) { in = new InputStreamReader(loader.openResource(synonymFile), decoder); } parser.parse(in); return parser.build(); } }; try { delegate.inform(res.getResourceLoader()); } catch (IOException e) { throw new SolrException(ErrorCode.SERVER_ERROR, e); } }
From source file:org.apache.solr.search.PreAnalyzedQParserPlugin.java
License:Apache License
public void init(NamedList args) { // TODO Auto-generated method stub try {//from w w w . j a v a 2s. co m // Initialize lemmatizer lemmatizer = LemmatizerFactory.createLemmatizer(); // Initialize OpenNLP model and tokenizer modelsPath = args.get("modelsPath").toString(); tokenModelIn = new FileInputStream( PreAnalyzedQParserPlugin.modelsPath + lang + File.separator + lang + "-token.bin"); tokenModel = new TokenizerModel(tokenModelIn); tokenizer = new TokenizerME(tokenModel); tokenizerSimple = SimpleTokenizer.INSTANCE; tokenizerWS = WhitespaceTokenizer.INSTANCE; // Get Synonyms file path synonymsPath = args.get("synonymsPath").toString(); // Initialize Synonyms Filter factory Map<String, String> argsSyn = new HashMap<String, String>(); argsSyn.put("synonyms", synonymsPath); argsSyn.put("luceneMatchVersion", Version.LUCENE_46.toString()); synFactory = new SynonymFilterFactory(argsSyn); synFactory.inform(new FilesystemResourceLoader()); // Get Mapping Char file path mappingPath = args.get("mappingsPath").toString(); lemmaLogPath = args.get("lemmaLogPath").toString(); // Initialize Mapping Char Filter factory Map<String, String> argsCharFactory = new HashMap<String, String>(); argsCharFactory.put("mapping", mappingPath); argsCharFactory.put("luceneMatchVersion", Version.LUCENE_46.toString()); mapCharFactory = new MappingCharFilterFactory(argsCharFactory); mapCharFactory.inform(new FilesystemResourceLoader()); } catch (LemmatizerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:uk.gov.nationalarchives.discovery.taxonomy.common.config.LuceneIAViewConfiguration.java
License:Mozilla Public License
public @Bean SynonymFilterFactory synonymFilterFactory() { Map<String, String> synonymFilterArgs = new HashMap<String, String>(); synonymFilterArgs.put("synonyms", "synonyms.txt"); synonymFilterArgs.put("expand", "true"); synonymFilterArgs.put("ignoreCase", "true"); synonymFilterArgs.put("luceneMatchVersion", version); SynonymFilterFactory synonymFilterFactory = new SynonymFilterFactory(synonymFilterArgs); try {// w w w . j a v a 2s . c om ResourceLoader loader = new ClasspathResourceLoader(getClass()); synonymFilterFactory.inform(loader); } catch (IOException e) { logger.error(".synonymFilterFactory: an error occured while creating the Filter factory: {}", e.getMessage()); } return synonymFilterFactory; }