001package org.maltparser.parser;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.core.symbol.SymbolTable;
005import org.maltparser.core.symbol.SymbolTableHandler;
006import org.maltparser.core.syntaxgraph.DependencyStructure;
007import org.maltparser.core.syntaxgraph.edge.Edge;
008import org.maltparser.core.syntaxgraph.node.DependencyNode;
009import org.maltparser.parser.guide.ClassifierGuide;
010/**
011 * @author Johan Hall
012 *
013 */
014public abstract class ParsingAlgorithm implements AlgoritmInterface {
015        protected final DependencyParserConfig manager;
016        protected final ParserRegistry registry;
017        protected ClassifierGuide classifierGuide;
018        protected final ParserState parserState;
019        protected ParserConfiguration currentParserConfiguration;
020
021        /**
022         * Creates a parsing algorithm
023         * 
024         * @param _manager a reference to the single malt configuration
025         * @param symbolTableHandler a reference to the symbol table handler
026         * @throws MaltChainedException
027         */
028        public ParsingAlgorithm(DependencyParserConfig _manager, SymbolTableHandler symbolTableHandler) throws MaltChainedException {
029                this.manager = _manager;
030                this.registry = new ParserRegistry();
031                registry.setSymbolTableHandler(symbolTableHandler);
032                registry.setDataFormatInstance(manager.getDataFormatInstance());
033                registry.setAbstractParserFeatureFactory(manager.getParserFactory());
034                parserState = new ParserState(manager, symbolTableHandler, manager.getParserFactory()); 
035        }
036        
037        public abstract void terminate() throws MaltChainedException;
038
039        public ParserRegistry getParserRegistry() {
040                return registry;
041        }
042        
043        /**
044         * Returns the classifier guide.
045         * 
046         * @return the classifier guide
047         */
048        public ClassifierGuide getGuide() {
049                return classifierGuide;
050        }
051        
052        /**
053         * Sets the classifier guide
054         * 
055         * @param guide a classifier guide
056         */
057        public void setGuide(ClassifierGuide guide) {
058                this.classifierGuide = guide;
059        }
060
061        /**
062         * Returns the current active parser configuration
063         * 
064         * @return the current active parser configuration
065         */
066        public ParserConfiguration getCurrentParserConfiguration() {
067                return currentParserConfiguration;
068        }
069        
070        /**
071         * Sets the current parser configuration
072         * 
073         * @param currentParserConfiguration a parser configuration
074         */
075        protected void setCurrentParserConfiguration(ParserConfiguration currentParserConfiguration) {
076                this.currentParserConfiguration = currentParserConfiguration;
077        }
078        
079        /**
080         * Returns the parser state
081         * 
082         * @return the parser state
083         */
084        public ParserState getParserState() {
085                return parserState;
086        }
087        
088        
089        /**
090         * Returns the single malt configuration
091         * 
092         * @return the single malt configuration
093         */
094        public DependencyParserConfig getManager() {
095                return manager;
096        }
097
098        
099        /**
100         * Copies the edges of the source dependency structure to the target dependency structure
101         * 
102         * @param source a source dependency structure
103         * @param target a target dependency structure
104         * @throws MaltChainedException
105         */
106        protected void copyEdges(DependencyStructure source, DependencyStructure target) throws MaltChainedException {
107                for (int index : source.getTokenIndices()) {
108                        DependencyNode snode = source.getDependencyNode(index);
109                        
110                        if (snode.hasHead()) {
111                                Edge s = snode.getHeadEdge();
112                                Edge t = target.addDependencyEdge(s.getSource().getIndex(), s.getTarget().getIndex());
113                                
114                                for (SymbolTable table : s.getLabelTypes()) {
115                                        t.addLabel(table, s.getLabelSymbol(table));
116                                }
117                        }
118                }
119        }
120        
121        protected void copyDynamicInput(DependencyStructure source, DependencyStructure target) throws MaltChainedException {
122                for (int index : source.getTokenIndices()) {
123                        DependencyNode snode = source.getDependencyNode(index);
124                        DependencyNode tnode = target.getDependencyNode(index);
125                        for (SymbolTable table : snode.getLabelTypes()) {
126                                if (!tnode.hasLabel(table)) {
127                                        tnode.addLabel(table,snode.getLabelSymbol(table));
128                                }
129                        }
130                }
131        }
132}