Java tutorial
package org.fastcatsearch.plugin.analysis; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.core.AnalyzerOption; import org.apache.lucene.analysis.tokenattributes.*; import org.fastcatsearch.env.Environment; import org.fastcatsearch.ir.analysis.AnalyzerPool; import org.fastcatsearch.ir.io.CharVector; import org.fastcatsearch.plugin.LicenseInvalidException; import org.fastcatsearch.plugin.Plugin; import org.fastcatsearch.plugin.PluginSetting; import org.fastcatsearch.settings.SettingFileNames; import org.fastcatsearch.util.DynamicClassLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.*; import java.util.Iterator; import java.util.List; import java.util.Scanner; /** * Created by swsong on 2015. 7. 10.. */ public class RunAnalyzer { protected static Logger logger = LoggerFactory.getLogger(RunAnalyzer.class); private AnalysisPlugin plugin; private String pluginId; public static void main(String[] args) throws IOException { if (args.length != 3) { printUsage(); System.exit(0); } File pluginDir = new File(args[0]); String pluginClassName = args[1]; String analyzerId = args[2]; RunAnalyzer runAnalyzer = new RunAnalyzer(pluginDir, pluginClassName); AnalyzerPool analyzerPool = runAnalyzer.getAnalyzerPool(analyzerId); Analyzer analyzer = null; try { analyzer = analyzerPool.getFromPool(); //? ? ? ?. Scanner sc = new Scanner(System.in); System.out.println("=================================="); System.out.println(" Fastcat analyzer"); System.out.println(" Enter 'quit' for exit program. "); System.out.println("=================================="); System.out.print("Input String: "); while (sc.hasNextLine()) { String str = sc.nextLine(); if (str.equalsIgnoreCase("quit")) { break; } try { char[] value = str.toCharArray(); TokenStream tokenStream = analyzer.tokenStream("", new CharArrayReader(value), new AnalyzerOption()); tokenStream.reset(); CharsRefTermAttribute termAttribute = null; if (tokenStream.hasAttribute(CharsRefTermAttribute.class)) { termAttribute = tokenStream.getAttribute(CharsRefTermAttribute.class); } SynonymAttribute synonymAttribute = null; if (tokenStream.hasAttribute(SynonymAttribute.class)) { synonymAttribute = tokenStream.getAttribute(SynonymAttribute.class); } AdditionalTermAttribute additionalTermAttribute = null; if (tokenStream.hasAttribute(AdditionalTermAttribute.class)) { additionalTermAttribute = tokenStream.getAttribute(AdditionalTermAttribute.class); } StopwordAttribute stopwordAttribute = null; if (tokenStream.hasAttribute(StopwordAttribute.class)) { stopwordAttribute = tokenStream.getAttribute(StopwordAttribute.class); } CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class); while (tokenStream.incrementToken()) { String word = ""; //? ?? CharsRefTermAttribute ? . if (termAttribute != null) { word = termAttribute.toString(); } else { //CharsRefTermAttribute ? ?? CharTermAttribute ? ?. word = charTermAttribute.toString(); } // ?? . if (stopwordAttribute.isStopword()) { continue; } // // ?? . // System.out.print(">> "); System.out.println(word); // . if (synonymAttribute != null) { List synonyms = synonymAttribute.getSynonyms(); if (synonyms != null) { for (Object synonymObj : synonyms) { if (synonymObj instanceof CharVector) { CharVector synonym = (CharVector) synonymObj; System.out.print("S> "); System.out.println(synonym); } else if (synonymObj instanceof List) { List synonymList = (List) synonymObj; for (Object synonym : synonymList) { System.out.print("S> "); System.out.println(synonym); } } } } } // . // ??? ? ? ?? ?, ?? . if (additionalTermAttribute != null && additionalTermAttribute.size() > 0) { Iterator<String> termIter = additionalTermAttribute.iterateAdditionalTerms(); while (termIter.hasNext()) { String token = termIter.next(); System.out.print("A> "); System.out.println(word); } } } } catch (IOException e) { e.printStackTrace(); } System.out.print("Input String: "); } } finally { if (analyzer != null) { analyzerPool.releaseToPool(analyzer); } } System.out.print("Bye!"); } private static void printUsage() { System.out.println( "Usage : java " + RunAnalyzer.class.getName() + " <pluginDir> <pluginClassName> <analyzerId>"); System.out.println("Example"); System.out.println("$ java " + RunAnalyzer.class.getName() + " plugin/analysis/Korean org.fastcatsearch.plugin.analysis.ko.KoreanAnalysisPlugin standard"); } public RunAnalyzer(File pluginDir, String pluginClassName) { Environment env = new Environment(pluginDir.getAbsolutePath()); File pluginConfigFile = new File(pluginDir, SettingFileNames.pluginConfig); try { InputStream is = new FileInputStream(pluginConfigFile); JAXBContext analysisJc = JAXBContext.newInstance(AnalysisPluginSetting.class); Unmarshaller analysisUnmarshaller = analysisJc.createUnmarshaller(); PluginSetting pluginSetting = (PluginSetting) analysisUnmarshaller.unmarshal(is); String serverId = env.getServerId(); boolean useDB = false; plugin = (AnalysisPlugin) DynamicClassLoader.loadObject(pluginClassName, Plugin.class, new Class<?>[] { File.class, PluginSetting.class, String.class }, new Object[] { pluginDir, pluginSetting, serverId }); plugin.load(useDB); pluginId = plugin.getPluginSetting().getId(); } catch (FileNotFoundException e) { logger.error("{} plugin ?? ?? ?.", pluginDir.getName()); } catch (JAXBException e) { logger.error("plugin ?? ? ?. {}", e.getMessage()); } catch (IOException e) { logger.error("IO??. {}", e.getMessage()); } catch (LicenseInvalidException e) { logger.error("?? . {}", e.getMessage()); } catch (Exception e) { e.printStackTrace(); } } public AnalyzerPool getAnalyzerPool(String analyzerId) { if (plugin.isLoaded()) { AnalyzerPool pool = plugin.getAnalyzerPool(analyzerId); if (pool != null) { return pool; } else { throw new RuntimeException("Cannot find analyzer >> " + (pluginId + "." + analyzerId)); } } return null; } }