001 package org.maltparser; 002 003 import java.util.SortedMap; 004 import java.util.TreeMap; 005 006 import org.maltparser.core.exception.MaltChainedException; 007 import org.maltparser.core.flow.FlowChartInstance; 008 import org.maltparser.core.flow.FlowChartManager; 009 import org.maltparser.core.flow.item.ChartItem; 010 import org.maltparser.core.helper.SystemLogger; 011 import org.maltparser.core.helper.Util; 012 import org.maltparser.core.options.OptionManager; 013 import org.maltparser.core.plugin.PluginLoader; 014 015 016 public class Engine { 017 private final long startTime; 018 private final FlowChartManager flowChartManager; 019 private final SortedMap<Integer,FlowChartInstance> flowChartInstances; 020 021 public Engine() throws MaltChainedException { 022 startTime = System.currentTimeMillis(); 023 flowChartManager = new FlowChartManager(); 024 flowChartManager.getFlowChartSystem().load(getClass().getResource("/appdata/flow/flowchartsystem.xml")); 025 flowChartManager.getFlowChartSystem().load(PluginLoader.instance()); 026 flowChartManager.load(getClass().getResource("/appdata/flow/flowcharts.xml")); 027 flowChartManager.load(PluginLoader.instance()); 028 flowChartInstances = new TreeMap<Integer,FlowChartInstance>(); 029 } 030 031 public FlowChartInstance initialize(int optionContainerIndex) throws MaltChainedException { 032 String flowChartName = null; 033 if (OptionManager.instance().getOptionValueNoDefault(optionContainerIndex, "config", "flowchart") != null) { 034 flowChartName = OptionManager.instance().getOptionValue(optionContainerIndex, "config", "flowchart").toString(); 035 } 036 if (flowChartName == null) { 037 if (OptionManager.instance().getOptionValueNoDefault(optionContainerIndex, "singlemalt", "mode") != null) { 038 // This fix maps --singlemalt-mode option to --config-flowchart option because of historical reasons (version 1.0-1.1) 039 flowChartName = OptionManager.instance().getOptionValue(optionContainerIndex, "singlemalt", "mode").toString(); 040 OptionManager.instance().overloadOptionValue(optionContainerIndex, "config", "flowchart", flowChartName); 041 } else { 042 flowChartName = OptionManager.instance().getOptionValue(optionContainerIndex, "config", "flowchart").toString(); 043 } 044 } 045 FlowChartInstance flowChartInstance = flowChartManager.initialize(optionContainerIndex, flowChartName); 046 flowChartInstances.put(optionContainerIndex, flowChartInstance); 047 return flowChartInstance; 048 } 049 050 public void process(int optionContainerIndex) throws MaltChainedException { 051 FlowChartInstance flowChartInstance = flowChartInstances.get(optionContainerIndex); 052 if (flowChartInstance.hasPreProcessChartItems()) { 053 flowChartInstance.preprocess(); 054 } 055 if (flowChartInstance.hasProcessChartItems()) { 056 int signal = ChartItem.CONTINUE; 057 int tic = 0; 058 int sentenceCounter = 0; 059 int nIteration = 1; 060 flowChartInstance.setEngineRegistry("iterations", nIteration); 061 while (signal != ChartItem.TERMINATE) { 062 signal = flowChartInstance.process(); 063 if (signal == ChartItem.CONTINUE) { 064 sentenceCounter++; 065 } else if (signal == ChartItem.NEWITERATION) { 066 SystemLogger.logger().info("\n=== END ITERATION "+nIteration+" ===\n"); 067 nIteration++; 068 flowChartInstance.setEngineRegistry("iterations", nIteration); 069 } 070 // System.out.println(sentenceCounter); 071 if (sentenceCounter < 101 && sentenceCounter == 1 || sentenceCounter == 10 || sentenceCounter == 100) { 072 Util.startTicer(SystemLogger.logger(), startTime, 10, sentenceCounter); 073 } 074 if (sentenceCounter%100 == 0) { 075 tic = Util.simpleTicer(SystemLogger.logger(), startTime, 10, tic, sentenceCounter); 076 } 077 } 078 Util.endTicer(SystemLogger.logger(), startTime, 10, tic, sentenceCounter); 079 } 080 if (flowChartInstance.hasPostProcessChartItems()) { 081 flowChartInstance.postprocess(); 082 } 083 } 084 085 public void terminate(int optionContainerIndex) throws MaltChainedException { 086 flowChartInstances.get(optionContainerIndex).terminate(); 087 } 088 }