All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kotlinnlp.syntaxdecoder.transitionsystem.models.arcspine.ArcSpineTransitionsGenerator.kt Maven / Gradle / Ivy

Go to download

SyntaxDecoder is a generalized transition-based parsing framework designed to simplify the development of statistical transition-based dependency parsers.

There is a newer version: 0.2.3
Show newest version
/* Copyright 2017-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.syntaxdecoder.transitionsystem.models.arcspine

import com.kotlinnlp.syntaxdecoder.transitionsystem.TransitionsGenerator
import com.kotlinnlp.syntaxdecoder.transitionsystem.models.arcspine.transitions.ArcLeft
import com.kotlinnlp.syntaxdecoder.transitionsystem.models.arcspine.transitions.ArcRight
import com.kotlinnlp.syntaxdecoder.transitionsystem.models.arcspine.transitions.Root
import com.kotlinnlp.syntaxdecoder.transitionsystem.models.arcspine.transitions.Shift
import com.kotlinnlp.syntaxdecoder.utils.secondToLast

/**
 * The TransitionsGenerator for the ArcHybrid Transition System.
 */
class ArcSpineTransitionsGenerator : TransitionsGenerator() {

  /**
   * @param state the state from which to extract valid transitions.
   *
   * @return a list of valid transitions for the given [state].
   */
  override fun generate(state: ArcSpineState): List {

    val transitions = ArrayList()

    transitions.add(Root(state, id = transitions.getNextId()))
    transitions.add(Shift(state, id = transitions.getNextId()))
    transitions.addArcs(state)

    return transitions
  }

  /**
   * Add multiple ArcLeft and ArcRight transitions.
   */
  private fun ArrayList.addArcs(state: ArcSpineState){

    if (state.stack.size > 1) {

      state.stack.last().leftSpine.indices.forEach { k ->
        this.add(ArcLeft(state, governorSpineIndex =  k, id = this.getNextId()))
      }

      state.stack.secondToLast().rightSpine.indices.forEach { k ->
        this.add(ArcRight(state, governorSpineIndex =  k, id = this.getNextId()))
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy