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]): Unit
    Definition Classes
    CheckLicense
  16. def checkValidScope(scope: String): Unit
    Definition Classes
    CheckLicense
  17. def checkValidScopeAndEnvironment(scope: String, spark: Option[SparkSession], checkLp: Boolean): Unit
    Definition Classes
    CheckLicense
  18. def checkValidScopesAndEnvironment(scopes: Seq[String], spark: Option[SparkSession], checkLp: Boolean): 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 def extractParamMap(): ParamMap
    Definition Classes
    Params
  34. final def extractParamMap(extra: ParamMap): ParamMap
    Definition Classes
    Params
  35. val features: ArrayBuffer[Feature[_, _, _]]
    Definition Classes
    HasFeatures
  36. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  37. final def fit(dataset: Dataset[_]): EntityRulerInternalModel
    Definition Classes
    AnnotatorApproach → Estimator
  38. def fit(dataset: Dataset[_], paramMaps: Seq[ParamMap]): Seq[EntityRulerInternalModel]
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  39. def fit(dataset: Dataset[_], paramMap: ParamMap): EntityRulerInternalModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  40. def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): EntityRulerInternalModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" ) @varargs()
  41. def get[T](feature: StructFeature[T]): Option[T]
    Attributes
    protected
    Definition Classes
    HasFeatures
  42. def get[K, V](feature: MapFeature[K, V]): Option[Map[K, V]]
    Attributes
    protected
    Definition Classes
    HasFeatures
  43. def get[T](feature: SetFeature[T]): Option[Set[T]]
    Attributes
    protected
    Definition Classes
    HasFeatures
  44. def get[T](feature: ArrayFeature[T]): Option[Array[T]]
    Attributes
    protected
    Definition Classes
    HasFeatures
  45. final def get[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  46. def getCaseSensitive: Boolean
    Definition Classes
    HasCaseSensitiveProperties
  47. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  48. final def getDefault[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  49. def getEnableInMemoryStorage: Boolean
    Definition Classes
    HasStorageOptions
  50. def getIncludeStorage: Boolean
    Definition Classes
    HasStorageOptions
  51. def getInputCols: Array[String]
    Definition Classes
    HasInputAnnotationCols
  52. def getLazyAnnotator: Boolean
    Definition Classes
    CanBeLazy
  53. final def getOrDefault[T](param: Param[T]): T
    Definition Classes
    Params
  54. final def getOutputCol: String
    Definition Classes
    HasOutputAnnotationCol
  55. def getParam(paramName: String): Param[Any]
    Definition Classes
    Params
  56. def getStoragePath: Option[ExternalResource]
    Definition Classes
    HasStorage
  57. def getStorageRef: String
    Definition Classes
    HasStorageRef
  58. final def hasDefault[T](param: Param[T]): Boolean
    Definition Classes
    Params
  59. def hasParam(paramName: String): Boolean
    Definition Classes
    Params
  60. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  61. val includeStorage: BooleanParam
    Definition Classes
    HasStorageOptions
  62. 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
  63. def indexStorage(fitDataset: Dataset[_], resource: Option[ExternalResource]): Unit
    Definition Classes
    EntityRulerInternalApproach → HasStorage
  64. def initializeLogIfNecessary(isInterpreter: Boolean, silent: Boolean): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  65. def initializeLogIfNecessary(isInterpreter: Boolean): Unit
    Attributes
    protected
    Definition Classes
    Logging
  66. val inputAnnotatorTypes: Array[String]

    Input annotator types: DOCUMENT, TOKEN

    Input annotator types: DOCUMENT, TOKEN

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

    Output annotator types: CHUNK

    Output annotator types: CHUNK

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

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

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

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

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

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

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

  135. def validate(schema: StructType): Boolean
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  136. def validateStorageRef(dataset: Dataset[_], inputCols: Array[String], annotatorType: String): Unit
    Definition Classes
    HasStorageRef
  137. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  138. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  139. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  140. 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