com.kotlinnlp.hanclassifier.ClassesConfig.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hanclassifier Show documentation
Show all versions of hanclassifier Show documentation
HANClassifier is a very simple to use text classifier which uses the Hierarchical Attention Networks (HAN)
from the SimpleDNN library.
/* Copyright 2016-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.hanclassifier
import java.io.Serializable
/**
* The configuration of a hierarchy of classes for the classification.
*
* @property classes a map of classes indices to the configuration (optional) of their sub-levels
*/
data class ClassesConfig(val classes: Map) : Serializable {
companion object {
/**
* Private val used to serialize the class (needed by Serializable).
*/
@Suppress("unused")
private const val serialVersionUID: Long = 1L
}
/**
* The depth of the hierarchy.
*/
val depth: Int = this.classes.values.asSequence().map { 1 + (it?.depth ?: 0) }.max()!!
/**
* A classes configuration is considered empty if all its classes has not sub-classes.
*
* @return true if this classes configuration is empty, otherwise false
*/
val isEmpty: Boolean = this.classes.values.all { it == null }
/**
* A classes configuration is considered complete if it defines a complete classes set (sequential and starting
* from 0).
*
* @return true if this classes configuration is complete, otherwise false
*/
internal fun isComplete(): Boolean =
this.classes.keys.min() == 0 &&
this.classes.keys.max() == (this.classes.size - 1) &&
this.classes.values.all { it?.isComplete() ?: true }
/**
* @param other another classes configuration
*
* @return true if all this configuration classes and their sub-classes are defined in the other, otherwise false
*/
internal fun isCompatible(other: ClassesConfig): Boolean =
this.classes.all { (classIndex, subLevel) ->
val otherSubLevel: ClassesConfig? = other.classes[classIndex]
when {
classIndex !in other.classes -> false
subLevel == null -> otherSubLevel == null
else -> otherSubLevel != null && subLevel.isCompatible(otherSubLevel)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy