import sys import Config ################################################################################ def isEmpty(value) : return value == "_" or value == "" ################################################################################ ################################################################################ class Transition : available = set({"RIGHT", "LEFT", "SHIFT", "REDUCE", "EOS"}) def __init__(self, name) : if name not in self.available : print("'%s' is not a valid transition type."%name, file=sys.stdout) exit(1) self.name = name def apply(self, config) : if self.name == "RIGHT" : applyRight(config) return if self.name == "LEFT" : applyLeft(config) return if self.name == "SHIFT" : applyShift(config) return if self.name == "REDUCE" : applyReduce(config) return if self.name == "EOS" : applyEOS(config) return print("ERROR : nothing to apply for '%s'"%self.name, file=sys.stderr) exit(1) def appliable(self, config) : if self.name == "RIGHT" : return len(config.stack) > 0 and isEmpty(config.getAsFeature(config.wordIndex, "HEAD")) if self.name == "LEFT" : return len(config.stack) > 0 and isEmpty(config.getAsFeature(config.stack[-1], "HEAD")) if self.name == "SHIFT" : return config.wordIndex < len(config.lines) - 1 if self.name == "REDUCE" : return len(config.stack) > 0 and not isEmpty(config.getAsFeature(config.stack[-1], "HEAD")) if self.name == "EOS" : return config.wordIndex == len(config.lines) - 1 print("ERROR : unknown name '%s'"%self.name, file=sys.stderr) exit(1) ################################################################################ ################################################################################ def applyRight(config) : config.set(config.wordIndex, "HEAD", config.stack[-1]) config.addWordIndexToStack() ################################################################################ ################################################################################ def applyLeft(config) : config.set(config.stack[-1], "HEAD", config.wordIndex) config.popStack() ################################################################################ ################################################################################ def applyShift(config) : config.addWordIndexToStack() ################################################################################ ################################################################################ def applyReduce(config) : config.popStack() ################################################################################ ################################################################################ def applyEOS(config) : rootCandidates = [index for index in config.stack if not config.isMultiword(index) and isEmpty(config.getAsFeature(index, "HEAD"))] if len(rootCandidates) == 0 : print("ERROR : no candidates for root", file=sys.stderr) config.printForDebug(sys.stderr) exit(1) rootIndex = rootCandidates[0] config.set(rootIndex, "HEAD", "-1") config.set(rootIndex, "DEPREL", "root") for index in range(len(config.lines)) : if config.isMultiword(index) or not isEmpty(config.getAsFeature(index, "HEAD")) : continue config.set(index, "HEAD", str(rootIndex)) ################################################################################