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

com.kotlinnlp.syntaxdecoder.transitionsystem.ActionsGenerator.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.

The 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

import com.google.common.collect.HashMultimap
import com.kotlinnlp.dependencytree.Deprel
import com.kotlinnlp.dependencytree.POSTag
import com.kotlinnlp.syntaxdecoder.transitionsystem.state.State
import com.kotlinnlp.syntaxdecoder.syntax.SyntacticDependency

/**
 * The ActionsGenerator.
 */
sealed class ActionsGenerator, TransitionType: Transition> {

  /**
   * Generate the possible actions allowed in a given state.
   *
   * @param transitions the transitions from which to generate the actions
   *
   * @return a list of Actions
   */
  fun generateFrom(transitions: List):
    List.Action> {

    val result = arrayListOf.Action>()

    transitions.forEach { result.addAll(it.generateActions(startId = result.size)) }

    return result
  }

  /**
   * @param startId the first available id (incremental) to assign to the generated actions
   *
   * @return a list of Actions
   */
  protected abstract fun Transition.generateActions(startId: Int):
    List.Action>

  /**
   * The Unlabeled Actions Generator generates an action for each transition, resulting in a 1:1
   * transition-action relation.
   */
  class Unlabeled, TransitionType: Transition>
    : ActionsGenerator() {

    /**
     * @param startId the first available id (incremental) to assign to the generated actions
     *
     * @return a list of Actions
     */
    override fun Transition.generateActions(startId: Int):
      List.Action>
      = listOf(this.actionFactory(id = startId))
  }

  /**
   * The Labeled Actions Generator can generate multiple actions for each transition, resulting in a 1:N
   * transition-actions relation, where N > 1 in case of transitions that create a syntactic dependency
   * and N is the number of 'deprels' associated to the transition direction (left, right, root).
   */
  class Labeled, TransitionType: Transition>(
    private val deprels: Map>
  ) : ActionsGenerator(){

    /**
     * @return a list of Actions
     */
    override fun Transition.generateActions(startId: Int):
      List.Action> {

      val actions = mutableListOf.Action>()
      var actionId = startId

      if (this is SyntacticDependency && this.type.direction in [email protected]) {

        [email protected](this.type.direction).forEach { deprel ->
          actions.add(this.actionFactory(id = actionId++, deprel = deprel))
        }

      } else {
        actions.add(this.actionFactory(id = actionId++))
      }

      return actions
    }
  }

  /**
   * The Labeled Actions Generator can generate multiple actions for each transition, resulting in a 1:N
   * transition-actions relation, where N > 1 in case of transitions that create a syntactic dependency
   * and N is the number of [deprelPosTagCombinations].
   */
  class MorphoSyntacticLabeled, TransitionType: Transition>(
    private val deprels: Map>,
    private val deprelPosTagCombinations: HashMultimap
  ) : ActionsGenerator(){

    /**
     * @return a list of Actions
     */
    override fun Transition.generateActions(startId: Int):
      List.Action> {

      val actions = mutableListOf.Action>()
      var actionId = startId

      if (this is SyntacticDependency && this.type.direction in [email protected]) {

        [email protected](this.type.direction).forEach { deprel ->
          [email protected](deprel).forEach { posTag ->

            actions.add(this.actionFactory(id = actionId++, deprel = deprel, posTag = posTag))
          }
        }

      } else {
        actions.add(this.actionFactory(id = actionId++))
      }

      return actions
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy