Packages

c

com.johnsnowlabs.nlp.annotators.er

EntityRulerInternalApproach

class EntityRulerInternalApproach extends AnnotatorApproach[EntityRulerInternalModel] with HasStorage with CheckLicense

Fits an Annotator to match exact strings or regex patterns provided in a file against a Document and assigns them an named entity. The definitions can contain any number of named entities.

There are multiple ways and formats to set the extraction resource. It is possible to set it either as a "JSON", "JSONL" or "CSV" file. A path to the file needs to be provided to setPatternsResource. The file format needs to be set as the "format" field in the option parameter map and depending on the file type, additional parameters might need to be set.

If the file is in a JSON format, then the rule definitions need to be given in a list with the fields "id", "label" and "patterns":

 [
  {
    "id": "person-regex",
    "label": "PERSON",
    "patterns": ["\\w+\\s\\w+", "\\w+-\\w+"]
  },
  {
    "id": "locations-words",
    "label": "LOCATION",
    "patterns": ["Winterfell"]
  }
]

The same fields also apply to a file in the JSONL format:

{"id": "names-with-j", "label": "PERSON", "patterns": ["Jon", "John", "John Snow"]}
{"id": "names-with-s", "label": "PERSON", "patterns": ["Stark", "Snow"]}
{"id": "names-with-e", "label": "PERSON", "patterns": ["Eddard", "Eddard Stark"]}

In order to use a CSV file, an additional parameter "delimiter" needs to be set. In this case, the delimiter might be set by using .setPatternsResource("patterns.csv", ReadAs.TEXT, Map("format"->"csv", "delimiter" -> "\\|"))

PERSON|Jon
PERSON|John
PERSON|John Snow
LOCATION|Winterfell

Example

In this example, the entities file as the form of

PERSON|Jon
PERSON|John
PERSON|John Snow
LOCATION|Winterfell

where each line represents an entity and the associated string delimited by "|".

import spark.implicits._
import com.johnsnowlabs.nlp.base.DocumentAssembler
import com.johnsnowlabs.nlp.annotators.Tokenizer
import com.johnsnowlabs.nlp.annotators.er.EntityRulerInternalApproach
import com.johnsnowlabs.nlp.util.io.ReadAs

import org.apache.spark.ml.Pipeline

val documentAssembler = new DocumentAssembler()
  .setInputCol("text")
  .setOutputCol("document")

val tokenizer = new Tokenizer()
  .setInputCols("document")
  .setOutputCol("token")

val entityRuler = new EntityRulerInternalApproach()
  .setInputCols("document", "token")
  .setOutputCol("entities")
  .setPatternsResource(
    path = "src/test/resources/entity-ruler/patterns.csv",
    readAs = ReadAs.TEXT,
    options = Map("format" -> "csv", "delimiter" -> "\\|")
  )

val pipeline = new Pipeline().setStages(Array(
  documentAssembler,
  tokenizer,
  entityRuler
))

val data = Seq("Jon Snow wants to be lord of Winterfell.").toDF("text")
val result = pipeline.fit(data).transform(data)

result.selectExpr("explode(entities)").show(false)
+--------------------------------------------------------------------+
|col                                                                 |
+--------------------------------------------------------------------+
|[chunk, 0, 2, Jon, [entity -> PERSON, sentence -> 0], []]           |
|[chunk, 29, 38, Winterfell, [entity -> LOCATION, sentence -> 0], []]|
+--------------------------------------------------------------------+
Linear Supertypes
CheckLicense, HasStorage, HasCaseSensitiveProperties, HasStorageOptions, HasStorageRef, ParamsAndFeaturesWritable, HasFeatures, AnnotatorApproach[EntityRulerInternalModel], CanBeLazy, DefaultParamsWritable, MLWritable, HasOutputAnnotatorType, HasOutputAnnotationCol, HasInputAnnotationCols, Estimator[EntityRulerInternalModel], PipelineStage, Logging, Params, Serializable, Serializable, Identifiable, AnyRef, Any
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. EntityRulerInternalApproach
  2. CheckLicense
  3. HasStorage
  4. HasCaseSensitiveProperties
  5. HasStorageOptions
  6. HasStorageRef
  7. ParamsAndFeaturesWritable
  8. HasFeatures
  9. AnnotatorApproach
  10. CanBeLazy
  11. DefaultParamsWritable
  12. MLWritable
  13. HasOutputAnnotatorType
  14. HasOutputAnnotationCol
  15. HasInputAnnotationCols
  16. Estimator
  17. PipelineStage
  18. Logging
  19. Params
  20. Serializable
  21. Serializable
  22. Identifiable
  23. AnyRef
  24. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new EntityRulerInternalApproach()
  2. new EntityRulerInternalApproach(uid: String)

    uid

    required uid for storing annotator to disk

Type Members

  1. type AnnotatorType = String
    Definition Classes
    HasOutputAnnotatorType

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def $[T](param: Param[T]): T
    Attributes
    protected
    Definition Classes
    Params
  4. def $$[T](feature: StructFeature[T]): T
    Attributes
    protected
    Definition Classes
    HasFeatures
  5. def $$[K, V](feature: MapFeature[K, V]): Map[K, V]
    Attributes
    protected
    Definition Classes
    HasFeatures
  6. def $$[T](feature: SetFeature[T]): Set[T]
    Attributes
    protected
    Definition Classes
    HasFeatures
  7. def $$[T](feature: ArrayFeature[T]): Array[T]
    Attributes
    protected
    Definition Classes
    HasFeatures
  8. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  9. def _fit(dataset: Dataset[_], recursiveStages: Option[PipelineModel]): EntityRulerInternalModel
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  10. val alphabet: ExternalResourceParam
  11. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  12. def beforeTraining(spark: SparkSession): Unit
    Definition Classes
    EntityRulerInternalApproach → AnnotatorApproach
  13. val caseSensitive: BooleanParam
    Definition Classes
    HasCaseSensitiveProperties
  14. final def checkSchema(schema: StructType, inputAnnotatorType: String): Boolean
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  15. def checkValidEnvironment(spark: Option[SparkSession], scopes: Seq[String], metadata: Option[Map[String, Value]]): Unit
    Definition Classes
    CheckLicense
  16. def checkValidScope(scope: String): Unit
    Definition Classes
    CheckLicense
  17. def checkValidScopeAndEnvironment(scope: String, spark: Option[SparkSession], checkLp: Boolean, metadata: Option[Map[String, Value]]): Unit
    Definition Classes
    CheckLicense
  18. def checkValidScopesAndEnvironment(scopes: Seq[String], spark: Option[SparkSession], checkLp: Boolean, metadata: Option[Map[String, Value]]): Unit
    Definition Classes
    CheckLicense
  19. final def clear(param: Param[_]): EntityRulerInternalApproach.this.type
    Definition Classes
    Params
  20. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  21. final def copy(extra: ParamMap): Estimator[EntityRulerInternalModel]
    Definition Classes
    AnnotatorApproach → Estimator → PipelineStage → Params
  22. def copyValues[T <: Params](to: T, extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  23. def createDatabaseConnection(database: Name): RocksDBConnection
    Definition Classes
    HasStorageRef
  24. def createWriter(database: Name, connection: RocksDBConnection): StorageWriter[_]
    Attributes
    protected
    Definition Classes
    EntityRulerInternalApproach → HasStorage
  25. val databases: Array[Name]
    Attributes
    protected
    Definition Classes
    EntityRulerInternalApproach → HasStorage
  26. final def defaultCopy[T <: Params](extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  27. val description: String
    Definition Classes
    EntityRulerInternalApproach → AnnotatorApproach
  28. val enableInMemoryStorage: BooleanParam
    Definition Classes
    HasStorageOptions
  29. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  30. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  31. def explainParam(param: Param[_]): String
    Definition Classes
    Params
  32. def explainParams(): String
    Definition Classes
    Params
  33. final val extraInputCols: StringArrayParam
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  34. final def extractParamMap(): ParamMap
    Definition Classes
    Params
  35. final def extractParamMap(extra: ParamMap): ParamMap
    Definition Classes
    Params
  36. val features: ArrayBuffer[Feature[_, _, _]]
    Definition Classes
    HasFeatures
  37. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  38. final def fit(dataset: Dataset[_]): EntityRulerInternalModel
    Definition Classes
    AnnotatorApproach → Estimator
  39. def fit(dataset: Dataset[_], paramMaps: Seq[ParamMap]): Seq[EntityRulerInternalModel]
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  40. def fit(dataset: Dataset[_], paramMap: ParamMap): EntityRulerInternalModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  41. def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): EntityRulerInternalModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" ) @varargs()
  42. def get[T](feature: StructFeature[T]): Option[T]
    Attributes
    protected
    Definition Classes
    HasFeatures
  43. def get[K, V](feature: MapFeature[K, V]): Option[Map[K, V]]
    Attributes
    protected
    Definition Classes
    HasFeatures
  44. def get[T](feature: SetFeature[T]): Option[Set[T]]
    Attributes
    protected
    Definition Classes
    HasFeatures
  45. def get[T](feature: ArrayFeature[T]): Option[Array[T]]
    Attributes
    protected
    Definition Classes
    HasFeatures
  46. final def get[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  47. def getCaseSensitive: Boolean
    Definition Classes
    HasCaseSensitiveProperties
  48. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  49. final def getDefault[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  50. def getEnableInMemoryStorage: Boolean
    Definition Classes
    HasStorageOptions
  51. def getIncludeStorage: Boolean
    Definition Classes
    HasStorageOptions
  52. def getInputCols: Array[String]
    Definition Classes
    HasInputAnnotationCols
  53. def getLazyAnnotator: Boolean
    Definition Classes
    CanBeLazy
  54. final def getOrDefault[T](param: Param[T]): T
    Definition Classes
    Params
  55. final def getOutputCol: String
    Definition Classes
    HasOutputAnnotationCol
  56. def getParam(paramName: String): Param[Any]
    Definition Classes
    Params
  57. def getStoragePath: Option[ExternalResource]
    Definition Classes
    HasStorage
  58. def getStorageRef: String
    Definition Classes
    HasStorageRef
  59. final def hasDefault[T](param: Param[T]): Boolean
    Definition Classes
    Params
  60. def hasParam(paramName: String): Boolean
    Definition Classes
    Params
  61. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  62. val includeStorage: BooleanParam
    Definition Classes
    HasStorageOptions
  63. def index(fitDataset: Dataset[_], storageSourcePath: Option[String], readAs: Option[util.io.ReadAs.Value], writers: Map[Name, StorageWriter[_]], readOptions: Option[Map[String, String]]): Unit
    Attributes
    protected
    Definition Classes
    EntityRulerInternalApproach → HasStorage
  64. def indexStorage(fitDataset: Dataset[_], resource: Option[ExternalResource]): Unit
    Definition Classes
    EntityRulerInternalApproach → HasStorage
  65. def initializeLogIfNecessary(isInterpreter: Boolean, silent: Boolean): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  66. def initializeLogIfNecessary(isInterpreter: Boolean): Unit
    Attributes
    protected
    Definition Classes
    Logging
  67. val inputAnnotatorTypes: Array[String]

    Input annotator types: DOCUMENT, TOKEN

    Input annotator types: DOCUMENT, TOKEN

    Definition Classes
    EntityRulerInternalApproach → HasInputAnnotationCols
  68. final val inputCols: StringArrayParam
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  69. final def isDefined(param: Param[_]): Boolean
    Definition Classes
    Params
  70. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  71. final def isSet(param: Param[_]): Boolean
    Definition Classes
    Params
  72. def isTraceEnabled(): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  73. val lazyAnnotator: BooleanParam
    Definition Classes
    CanBeLazy
  74. def log: Logger
    Attributes
    protected
    Definition Classes
    Logging
  75. def logDebug(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  76. def logDebug(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  77. def logError(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  78. def logError(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  79. def logInfo(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  80. def logInfo(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  81. def logName: String
    Attributes
    protected
    Definition Classes
    Logging
  82. def logTrace(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  83. def logTrace(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  84. def logWarning(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  85. def logWarning(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  86. val missingRefMsg: String
    Attributes
    protected
    Definition Classes
    HasStorage
  87. def msgHelper(schema: StructType): String
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  88. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  89. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  90. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  91. def onTrained(model: EntityRulerInternalModel, spark: SparkSession): Unit
    Definition Classes
    AnnotatorApproach
  92. def onWrite(path: String, spark: SparkSession): Unit
    Attributes
    protected
    Definition Classes
    ParamsAndFeaturesWritable
  93. val optionalInputAnnotatorTypes: Array[String]
    Definition Classes
    EntityRulerInternalApproach → HasInputAnnotationCols
  94. val outputAnnotatorType: AnnotatorType

    Output annotator types: CHUNK

    Output annotator types: CHUNK

    Definition Classes
    EntityRulerInternalApproach → HasOutputAnnotatorType
  95. final val outputCol: Param[String]
    Attributes
    protected
    Definition Classes
    HasOutputAnnotationCol
  96. lazy val params: Array[Param[_]]
    Definition Classes
    Params
  97. val patternsResource: ExternalResourceParam

    Resource in JSON or CSV format to map entities to patterns (Default: null).

  98. def save(path: String): Unit
    Definition Classes
    MLWritable
    Annotations
    @Since( "1.6.0" ) @throws( ... )
  99. val sentenceMatch: BooleanParam
  100. def set[T](feature: StructFeature[T], value: T): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    HasFeatures
  101. def set[K, V](feature: MapFeature[K, V], value: Map[K, V]): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    HasFeatures
  102. def set[T](feature: SetFeature[T], value: Set[T]): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    HasFeatures
  103. def set[T](feature: ArrayFeature[T], value: Array[T]): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    HasFeatures
  104. final def set(paramPair: ParamPair[_]): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  105. final def set(param: String, value: Any): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  106. final def set[T](param: Param[T], value: T): EntityRulerInternalApproach.this.type
    Definition Classes
    Params
  107. def setAlphabetResource(path: String): EntityRulerInternalApproach.this.type

  108. def setCaseSensitive(value: Boolean): EntityRulerInternalApproach.this.type
    Definition Classes
    HasCaseSensitiveProperties
  109. def setDefault[T](feature: StructFeature[T], value: () ⇒ T): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    HasFeatures
  110. def setDefault[K, V](feature: MapFeature[K, V], value: () ⇒ Map[K, V]): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    HasFeatures
  111. def setDefault[T](feature: SetFeature[T], value: () ⇒ Set[T]): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    HasFeatures
  112. def setDefault[T](feature: ArrayFeature[T], value: () ⇒ Array[T]): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    HasFeatures
  113. final def setDefault(paramPairs: ParamPair[_]*): EntityRulerInternalApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  114. final def setDefault[T](param: Param[T], value: T): EntityRulerInternalApproach.this.type
    Attributes
    protected[org.apache.spark.ml]
    Definition Classes
    Params
  115. def setEnableInMemoryStorage(value: Boolean): EntityRulerInternalApproach.this.type
    Definition Classes
    HasStorageOptions
  116. def setExtraInputCols(value: Array[String]): EntityRulerInternalApproach.this.type
    Definition Classes
    HasInputAnnotationCols
  117. def setIncludeStorage(value: Boolean): EntityRulerInternalApproach.this.type
    Definition Classes
    HasStorageOptions
  118. final def setInputCols(value: String*): EntityRulerInternalApproach.this.type
    Definition Classes
    HasInputAnnotationCols
  119. def setInputCols(value: Array[String]): EntityRulerInternalApproach.this.type
    Definition Classes
    HasInputAnnotationCols
  120. def setLazyAnnotator(value: Boolean): EntityRulerInternalApproach.this.type
    Definition Classes
    CanBeLazy
  121. final def setOutputCol(value: String): EntityRulerInternalApproach.this.type
    Definition Classes
    HasOutputAnnotationCol
  122. def setPatternsResource(path: String, readAs: Format, options: Map[String, String] = Map("format" -> "JSON")): EntityRulerInternalApproach.this.type

  123. def setSentenceMatch(value: Boolean): EntityRulerInternalApproach.this.type
  124. def setStoragePath(path: String, readAs: util.io.ReadAs.Value): EntityRulerInternalApproach.this.type
    Definition Classes
    HasStorage
  125. def setStoragePath(path: String, readAs: String): EntityRulerInternalApproach.this.type
    Definition Classes
    HasStorage
  126. def setStorageRef(value: String): EntityRulerInternalApproach.this.type
    Definition Classes
    HasStorageRef
  127. def setUseStorage(value: Boolean): EntityRulerInternalApproach.this.type

  128. val storagePath: ExternalResourceParam
    Definition Classes
    HasStorage
  129. val storageRef: Param[String]
    Definition Classes
    HasStorageRef
  130. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  131. def toString(): String
    Definition Classes
    Identifiable → AnyRef → Any
  132. def train(dataset: Dataset[_], recursivePipeline: Option[PipelineModel]): EntityRulerInternalModel
    Definition Classes
    EntityRulerInternalApproach → AnnotatorApproach
  133. final def transformSchema(schema: StructType): StructType
    Definition Classes
    AnnotatorApproach → PipelineStage
  134. def transformSchema(schema: StructType, logging: Boolean): StructType
    Attributes
    protected
    Definition Classes
    PipelineStage
    Annotations
    @DeveloperApi()
  135. val uid: String
    Definition Classes
    EntityRulerInternalApproach → Identifiable
  136. val useStorage: BooleanParam

    Whether to use RocksDB storage to serialize patterns (Default: true).

  137. def validate(schema: StructType): Boolean
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  138. def validateStorageRef(dataset: Dataset[_], inputCols: Array[String], annotatorType: String): Unit
    Definition Classes
    HasStorageRef
  139. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  140. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  141. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  142. def write: MLWriter
    Definition Classes
    ParamsAndFeaturesWritable → DefaultParamsWritable → MLWritable

Inherited from CheckLicense

Inherited from HasStorage

Inherited from HasCaseSensitiveProperties

Inherited from HasStorageOptions

Inherited from HasStorageRef

Inherited from ParamsAndFeaturesWritable

Inherited from HasFeatures

Inherited from AnnotatorApproach[EntityRulerInternalModel]

Inherited from CanBeLazy

Inherited from DefaultParamsWritable

Inherited from MLWritable

Inherited from HasOutputAnnotatorType

Inherited from HasOutputAnnotationCol

Inherited from HasInputAnnotationCols

Inherited from Estimator[EntityRulerInternalModel]

Inherited from PipelineStage

Inherited from Logging

Inherited from Params

Inherited from Serializable

Inherited from Serializable

Inherited from Identifiable

Inherited from AnyRef

Inherited from Any

Parameters

A list of (hyper-)parameter keys this annotator can take. Users can set and get the parameter values through setters and getters, respectively.

Annotator types

Required input and expected output annotator types

Members

Parameter setters