001 package org.maltparser.parser;
002
003 import org.maltparser.core.exception.MaltChainedException;
004 import org.maltparser.core.syntaxgraph.LabelSet;
005 import org.maltparser.parser.guide.OracleGuide;
006 import org.maltparser.parser.history.GuideUserHistory;
007 import org.maltparser.parser.history.action.GuideUserAction;
008 import org.maltparser.parser.history.container.ActionContainer;
009 /**
010 * @author Johan Hall
011 *
012 */
013 public abstract class Oracle implements OracleGuide {
014 private final DependencyParserConfig manager;
015 private final GuideUserHistory history;
016 private String name;
017 protected final ActionContainer[] actionContainers;
018 protected ActionContainer transActionContainer;
019 protected final ActionContainer[] arcLabelActionContainers;
020
021 public Oracle(DependencyParserConfig manager, GuideUserHistory history) throws MaltChainedException {
022 this.manager = manager;
023 this.history = history;
024 this.actionContainers = history.getActionContainerArray();
025
026 if (actionContainers.length < 1) {
027 throw new ParsingException("Problem when initialize the history (sequence of actions). There are no action containers. ");
028 }
029 int nLabels = 0;
030 for (int i = 0; i < actionContainers.length; i++) {
031 if (actionContainers[i].getTableContainerName().startsWith("A.")) {
032 nLabels++;
033 }
034 }
035 int j = 0;
036 this.arcLabelActionContainers = new ActionContainer[nLabels];
037 for (int i = 0; i < actionContainers.length; i++) {
038 if (actionContainers[i].getTableContainerName().equals("T.TRANS")) {
039 transActionContainer = actionContainers[i];
040 } else if (actionContainers[i].getTableContainerName().startsWith("A.")) {
041 arcLabelActionContainers[j++] = actionContainers[i];
042 }
043 }
044 }
045
046 public GuideUserHistory getHistory() {
047 return history;
048 }
049
050 public DependencyParserConfig getConfiguration() {
051 return manager;
052 }
053
054 public String getGuideName() {
055 return name;
056 }
057
058 public void setGuideName(String guideName) {
059 this.name = guideName;
060 }
061
062 protected GuideUserAction updateActionContainers(int transition, LabelSet arcLabels) throws MaltChainedException {
063 transActionContainer.setAction(transition);
064
065 if (arcLabels == null) {
066 for (int i = 0; i < arcLabelActionContainers.length; i++) {
067 arcLabelActionContainers[i].setAction(-1);
068 }
069 } else {
070 for (int i = 0; i < arcLabelActionContainers.length; i++) {
071 arcLabelActionContainers[i].setAction(arcLabels.get(arcLabelActionContainers[i].getTable()).shortValue());
072 }
073 }
074 GuideUserAction oracleAction = history.getEmptyGuideUserAction();
075 oracleAction.addAction(actionContainers);
076 return oracleAction;
077 }
078 }