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

com.kotlinnlp.linguisticconditions.operators.AnyDescendant.kt Maven / Gradle / Ivy

There is a newer version: 0.2.3
Show newest version
/* Copyright 2018-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.linguisticconditions.operators

import com.kotlinnlp.linguisticconditions.Condition
import com.kotlinnlp.dependencytree.DependencyTree
import com.kotlinnlp.linguisticdescription.sentence.token.MorphoSynToken

/**
 * The 'any-descendant' operator.
 * It verifies that there is at least one descendant of a given token that verifies the [condition].
 *
 * @param condition the condition to which this operator is applied
 */
internal class AnyDescendant(condition: Condition) : Operator.Single(condition) {

  companion object {

    /**
     * The annotation of the condition.
     */
    const val ANNOTATION: String = "any-descendant"
  }

  /**
   * @param token a token or null if called on the virtual root
   * @param tokens the list of all the tokens that compose the sentence
   * @param dependencyTree the dependency tree of the token sentence
   *
   * @return a boolean indicating if this condition is verified for the given [token]
   */
  override fun isVerified(token: MorphoSynToken.Single?,
                          tokens: List,
                          dependencyTree: DependencyTree): Boolean {

    val descendantsIds: List = if (token != null)
      dependencyTree.getAllDescendants(token.id)
    else
      dependencyTree.elements // the token is the virtual root and its descendants are all the elements

    return descendantsIds.any {
      this.condition.isVerified(
        token = tokens[dependencyTree.getPosition(it)],
        tokens = tokens,
        dependencyTree = dependencyTree)
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy