001 package org.maltparser.parser.algorithm.covington; 002 003 import org.maltparser.core.exception.MaltChainedException; 004 import org.maltparser.core.syntaxgraph.DependencyStructure; 005 import org.maltparser.core.syntaxgraph.edge.Edge; 006 import org.maltparser.core.syntaxgraph.node.DependencyNode; 007 import org.maltparser.parser.ParserConfiguration; 008 import org.maltparser.parser.TransitionSystem; 009 import org.maltparser.parser.history.GuideUserHistory; 010 import org.maltparser.parser.history.History; 011 import org.maltparser.parser.history.action.ComplexDecisionAction; 012 import org.maltparser.parser.history.action.GuideUserAction; 013 import org.maltparser.parser.transition.TransitionTable; 014 /** 015 * @author Johan Hall 016 * 017 */ 018 public class Projective extends TransitionSystem { 019 protected static final int SHIFT = 1; 020 protected static final int NOARC = 2; 021 protected static final int RIGHTARC = 3; 022 protected static final int LEFTARC = 4; 023 024 025 public Projective() throws MaltChainedException { 026 super(); 027 } 028 029 public void apply(GuideUserAction currentAction, ParserConfiguration config) throws MaltChainedException { 030 CovingtonConfig covingtonConfig = (CovingtonConfig)config; 031 currentAction.getAction(actionContainers); 032 033 Edge e = null; 034 switch (transActionContainer.getActionCode()) { 035 case LEFTARC: 036 e = covingtonConfig.getDependencyGraph().addDependencyEdge(covingtonConfig.getRightTarget().getIndex(), covingtonConfig.getLeftTarget().getIndex()); 037 addEdgeLabels(e); 038 // config.setArcParent(covingtonConfig.getRightTarget()); 039 // config.setArcChild(covingtonConfig.getLeftTarget()); 040 break; 041 case RIGHTARC: 042 e = covingtonConfig.getDependencyGraph().addDependencyEdge(covingtonConfig.getLeftTarget().getIndex(), covingtonConfig.getRightTarget().getIndex()); 043 addEdgeLabels(e); 044 // config.setArcParent(covingtonConfig.getLeftTarget()); 045 // config.setArcChild(covingtonConfig.getRightTarget()); 046 break; 047 default: 048 // config.setArcParent(null); 049 // config.setArcChild(null); 050 break; 051 } 052 update(covingtonConfig, transActionContainer.getActionCode()); 053 } 054 055 private void update(CovingtonConfig covingtonConfig, int trans) throws MaltChainedException { 056 if (trans == SHIFT || trans == RIGHTARC) { 057 covingtonConfig.setRight(covingtonConfig.getRight() + 1); 058 covingtonConfig.setLeft(covingtonConfig.getRight() - 1); 059 } else { 060 int leftstop = covingtonConfig.getLeftstop(); 061 int left = covingtonConfig.getLeft(); 062 if (trans == NOARC) { 063 DependencyStructure dg = covingtonConfig.getDependencyStructure(); 064 DependencyNode leftNode = covingtonConfig.getInput().get(covingtonConfig.getLeft()); 065 if (dg.getTokenNode(leftNode.getIndex()) != null && dg.getTokenNode(leftNode.getIndex()).hasHead()) { 066 left = dg.getTokenNode(leftNode.getIndex()).getHead().getIndex(); 067 } else { 068 left = leftstop - 1; 069 } 070 } else { 071 DependencyNode rightNode = covingtonConfig.getRightTarget(); 072 left--; 073 DependencyNode leftNode = null; 074 while (left >= leftstop) { 075 leftNode = covingtonConfig.getInput().get(left); 076 if (rightNode.findComponent().getIndex() != leftNode.findComponent().getIndex()) { 077 break; 078 } 079 left--; 080 } 081 } 082 083 if (left < leftstop) { 084 covingtonConfig.setRight(covingtonConfig.getRight() + 1); 085 covingtonConfig.setLeft(covingtonConfig.getRight() - 1); 086 } else { 087 covingtonConfig.setLeft(left); 088 } 089 } 090 } 091 092 public GuideUserAction getDeterministicAction(GuideUserHistory history, ParserConfiguration config) throws MaltChainedException { 093 return null; 094 } 095 096 protected void addAvailableTransitionToTable(TransitionTable ttable) throws MaltChainedException { 097 ttable.addTransition(SHIFT, "SH", false, null); 098 ttable.addTransition(NOARC, "NA", false, null); 099 ttable.addTransition(RIGHTARC, "RA", true, null); 100 ttable.addTransition(LEFTARC, "LA", true, null); 101 } 102 103 protected void initWithDefaultTransitions(GuideUserHistory history) throws MaltChainedException { 104 GuideUserAction currentAction = new ComplexDecisionAction((History)history); 105 106 transActionContainer.setAction(SHIFT); 107 transActionContainer.setAction(NOARC); 108 for (int i = 0; i < arcLabelActionContainers.length; i++) { 109 arcLabelActionContainers[i].setAction(-1); 110 } 111 currentAction.addAction(actionContainers); 112 } 113 114 public String getName() { 115 return "covnonproj"; 116 } 117 118 public boolean permissible(GuideUserAction currentAction, ParserConfiguration config) throws MaltChainedException { 119 CovingtonConfig covingtonConfig = (CovingtonConfig)config; 120 DependencyNode leftTarget = covingtonConfig.getLeftTarget(); 121 DependencyNode rightTarget = covingtonConfig.getRightTarget(); 122 DependencyStructure dg = covingtonConfig.getDependencyGraph(); 123 currentAction.getAction(actionContainers); 124 int trans = transActionContainer.getActionCode(); 125 126 if (trans == SHIFT && covingtonConfig.isAllowShift() == false) { 127 return false; 128 } 129 if ((trans == LEFTARC || trans == RIGHTARC) && !isActionContainersLabeled()) { 130 return false; 131 } 132 if (trans == LEFTARC && leftTarget.isRoot()) { 133 return false; 134 } 135 if (trans == LEFTARC && dg.hasLabeledDependency(leftTarget.getIndex())) { 136 return false; 137 } 138 if (trans == RIGHTARC && dg.hasLabeledDependency(rightTarget.getIndex())) { 139 return false; 140 } 141 return true; 142 } 143 144 public GuideUserAction defaultAction(GuideUserHistory history, ParserConfiguration configuration) throws MaltChainedException { 145 return updateActionContainers(history, Projective.NOARC, null); 146 } 147 } 148