class ContextualParserApproach extends AnnotatorApproach[ContextualParserModel] with HandleExceptionParams with CheckLicense

Creates a model, that extracts entity from a document based on user defined rules. Rule matching is based on a RegexMatcher defined in a JSON file. It is set through the parameter setJsonPath() In this JSON file, regex is defined that you want to match along with the information that will output on metadata field. Additionally, a dictionary can be provided with setDictionary to map extracted entities to a unified representation. The first column of the dictionary file should be the representation with following columns the possible matches.

Example

An example JSON file regex_token.json can look like this:

{
"entity": "Stage",
"ruleScope": "sentence",
"regex": "[cpyrau]?[T][0-9X?][a-z^cpyrau]*",
"matchScope": "token"
}

Which means to extract the stage code on a sentence level. An example pipeline could then be defined like this

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

val sentenceDetector = new SentenceDetector()
  .setInputCols("document")
  .setOutputCol("sentence")

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

Define the parser (json file needs to be provided)

val data = Seq("A patient has liver metastases pT1bN0M0 and the T5 primary site may be colon or... ").toDF("text")
val contextualParser = new ContextualParserApproach()
  .setInputCols(Array("sentence", "token"))
  .setOutputCol("entity")
  .setJsonPath("/path/to/regex_token.json")
  .setCaseSensitive(true)
val pipeline = new Pipeline().setStages(Array(
    documentAssembler,
    sentenceDetector,
    tokenizer,
    contextualParser
  ))

val result = pipeline.fit(data).transform(data)

Show Results

result.selectExpr("explode(entity)").show(5, truncate=false)
+-------------------------------------------------------------------------------------------------------------------------+
|col                                                                                                                      |
+-------------------------------------------------------------------------------------------------------------------------+
|{chunk, 32, 39, pT1bN0M0, {field -> Stage, normalized -> , confidence -> 1.00, sentence -> 0}, []}                 |
|{chunk, 49, 50, T5, {field -> Stage, normalized -> , confidence -> 1.00, sentence -> 0}, []}                       |
|{chunk, 148, 156, cT4bcN2M1, {field -> Stage, normalized -> , confidence -> 1.00, sentence -> 1}, []}              |
|{chunk, 189, 194, T?N3M1, {field -> Stage, normalized -> , confidence -> 1.00, sentence -> 2}, []}                 |
|{chunk, 316, 323, pT1bN0M0, {field -> Stage, normalized -> , confidence -> 1.00, sentence -> 3}, []}               |
+-------------------------------------------------------------------------------------------------------------------------+
See also

ContextualParserModel for the trained model

Linear Supertypes
CheckLicense, HandleExceptionParams, AnnotatorApproach[ContextualParserModel], CanBeLazy, DefaultParamsWritable, MLWritable, HasOutputAnnotatorType, HasOutputAnnotationCol, HasInputAnnotationCols, Estimator[ContextualParserModel], PipelineStage, Logging, Params, Serializable, Serializable, Identifiable, AnyRef, Any
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. ContextualParserApproach
  2. CheckLicense
  3. HandleExceptionParams
  4. AnnotatorApproach
  5. CanBeLazy
  6. DefaultParamsWritable
  7. MLWritable
  8. HasOutputAnnotatorType
  9. HasOutputAnnotationCol
  10. HasInputAnnotationCols
  11. Estimator
  12. PipelineStage
  13. Logging
  14. Params
  15. Serializable
  16. Serializable
  17. Identifiable
  18. AnyRef
  19. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

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

    uid

    a unique identifier for the instantiated AnnotatorModel

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. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  5. def _fit(dataset: Dataset[_], recursiveStages: Option[PipelineModel]): ContextualParserModel
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def beforeTraining(spark: SparkSession): Unit
    Definition Classes
    AnnotatorApproach
  8. val caseSensitive: BooleanParam

    Whether to use case sensitive when matching values (Default: false)

  9. final def checkSchema(schema: StructType, inputAnnotatorType: String): Boolean
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  10. def checkValidEnvironment(spark: Option[SparkSession], scopes: Seq[String]): Unit
    Definition Classes
    CheckLicense
  11. def checkValidScope(scope: String): Unit
    Definition Classes
    CheckLicense
  12. def checkValidScopeAndEnvironment(scope: String, spark: Option[SparkSession], checkLp: Boolean): Unit
    Definition Classes
    CheckLicense
  13. def checkValidScopesAndEnvironment(scopes: Seq[String], spark: Option[SparkSession], checkLp: Boolean): Unit
    Definition Classes
    CheckLicense
  14. final def clear(param: Param[_]): ContextualParserApproach.this.type
    Definition Classes
    Params
  15. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  16. val completeContextMatch: BooleanParam

    Whether to do an exact match of prefix and suffix.

  17. final def copy(extra: ParamMap): Estimator[ContextualParserModel]
    Definition Classes
    AnnotatorApproach → Estimator → PipelineStage → Params
  18. def copyValues[T <: Params](to: T, extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  19. final def defaultCopy[T <: Params](extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  20. val description: String
    Definition Classes
    ContextualParserApproach → AnnotatorApproach
  21. val dictionary: ExternalResourceParam

    Path to dictionary file in tsv or csv format, where the first column should be the representation with following columns the possible matches.

  22. val doExceptionHandling: BooleanParam

    If true, exceptions are handled.

    If true, exceptions are handled. If exception causing data is passed to the model, a error annotation is emitted which has the exception message. Processing continues with the next one. This comes with a performance penalty.

    Definition Classes
    HandleExceptionParams
  23. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  24. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  25. def explainParam(param: Param[_]): String
    Definition Classes
    Params
  26. def explainParams(): String
    Definition Classes
    Params
  27. final def extractParamMap(): ParamMap
    Definition Classes
    Params
  28. final def extractParamMap(extra: ParamMap): ParamMap
    Definition Classes
    Params
  29. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  30. final def fit(dataset: Dataset[_]): ContextualParserModel
    Definition Classes
    AnnotatorApproach → Estimator
  31. def fit(dataset: Dataset[_], paramMaps: Seq[ParamMap]): Seq[ContextualParserModel]
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  32. def fit(dataset: Dataset[_], paramMap: ParamMap): ContextualParserModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  33. def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): ContextualParserModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" ) @varargs()
  34. final def get[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  35. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  36. final def getDefault[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  37. def getEntityDefinition: Option[EntityDefinition]
  38. def getInputCols: Array[String]
    Definition Classes
    HasInputAnnotationCols
  39. def getLazyAnnotator: Boolean
    Definition Classes
    CanBeLazy
  40. final def getOrDefault[T](param: Param[T]): T
    Definition Classes
    Params
  41. final def getOutputCol: String
    Definition Classes
    HasOutputAnnotationCol
  42. def getParam(paramName: String): Param[Any]
    Definition Classes
    Params
  43. final def hasDefault[T](param: Param[T]): Boolean
    Definition Classes
    Params
  44. def hasParam(paramName: String): Boolean
    Definition Classes
    Params
  45. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  46. def initializeLogIfNecessary(isInterpreter: Boolean, silent: Boolean): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  47. def initializeLogIfNecessary(isInterpreter: Boolean): Unit
    Attributes
    protected
    Definition Classes
    Logging
  48. val inputAnnotatorTypes: Array[String]

    Input annotator types: DOCUMENT, TOKEN

    Input annotator types: DOCUMENT, TOKEN

    Definition Classes
    ContextualParserApproach → HasInputAnnotationCols
  49. final val inputCols: StringArrayParam
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  50. final def isDefined(param: Param[_]): Boolean
    Definition Classes
    Params
  51. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  52. final def isSet(param: Param[_]): Boolean
    Definition Classes
    Params
  53. def isTraceEnabled(): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  54. val jsonPath: Param[String]

    Path to json file with regex rules

  55. val lazyAnnotator: BooleanParam
    Definition Classes
    CanBeLazy
  56. def log: Logger
    Attributes
    protected
    Definition Classes
    Logging
  57. def logDebug(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  58. def logDebug(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  59. def logError(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  60. def logError(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  61. def logInfo(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  62. def logInfo(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  63. def logName: String
    Attributes
    protected
    Definition Classes
    Logging
  64. def logTrace(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  65. def logTrace(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  66. def logWarning(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  67. def logWarning(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  68. def msgHelper(schema: StructType): String
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  69. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  70. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  71. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  72. def onTrained(model: ContextualParserModel, spark: SparkSession): Unit
    Definition Classes
    AnnotatorApproach
  73. val optionalContextRules: BooleanParam

    When set to true, it will output regex match regardless of context matches.

  74. val optionalInputAnnotatorTypes: Array[String]
    Definition Classes
    HasInputAnnotationCols
  75. val outputAnnotatorType: AnnotatorType

    Output annotator types: CHUNK

    Output annotator types: CHUNK

    Definition Classes
    ContextualParserApproach → HasOutputAnnotatorType
  76. final val outputCol: Param[String]
    Attributes
    protected
    Definition Classes
    HasOutputAnnotationCol
  77. lazy val params: Array[Param[_]]
    Definition Classes
    Params
  78. val prefixAndSuffixMatch: BooleanParam

    Whether to match both prefix and suffix to annotate the hit (Default: false)

  79. def save(path: String): Unit
    Definition Classes
    MLWritable
    Annotations
    @Since( "1.6.0" ) @throws( ... )
  80. final def set(paramPair: ParamPair[_]): ContextualParserApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  81. final def set(param: String, value: Any): ContextualParserApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  82. final def set[T](param: Param[T], value: T): ContextualParserApproach.this.type
    Definition Classes
    Params
  83. def setCaseSensitive(value: Boolean): ContextualParserApproach.this.type

    Whether to use case sensitive when matching values (Default: false)

  84. def setCompleteContextMatch(value: Boolean): ContextualParserApproach.this.type

    Whether to do an exact match of prefix and suffix.

  85. final def setDefault(paramPairs: ParamPair[_]*): ContextualParserApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  86. final def setDefault[T](param: Param[T], value: T): ContextualParserApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  87. def setDictionary(path: String, readAs: Format = ReadAs.TEXT, options: Map[String, String] = Map("delimiter" -> "\t")): ContextualParserApproach.this.type

    Path to dictionary file in tsv or csv format.

    Path to dictionary file in tsv or csv format. The first column should be the representation, following columns the possible matches.

  88. def setDoExceptionHandling(value: Boolean): ContextualParserApproach.this.type

    If true, exceptions are handled.

    If true, exceptions are handled. If exception causing data is passed to the model, a error annotation is emitted which has the exception message. Processing continues with the next one. This comes with a performance penalty.

    Definition Classes
    HandleExceptionParams
  89. final def setInputCols(value: String*): ContextualParserApproach.this.type
    Definition Classes
    HasInputAnnotationCols
  90. def setInputCols(value: Array[String]): ContextualParserApproach.this.type
    Definition Classes
    HasInputAnnotationCols
  91. def setJsonPath(value: String): ContextualParserApproach.this.type

    Path to json file with regex rules

  92. def setLazyAnnotator(value: Boolean): ContextualParserApproach.this.type
    Definition Classes
    CanBeLazy
  93. def setOptionalContextRules(value: Boolean): ContextualParserApproach.this.type

    When set to true, it will output regex match regardless of context matches.

  94. final def setOutputCol(value: String): ContextualParserApproach.this.type
    Definition Classes
    HasOutputAnnotationCol
  95. def setPrefixAndSuffixMatch(value: Boolean): ContextualParserApproach.this.type

    Whether to match both prefix and suffix to annotate the hit (Default: false)

  96. def setShortestContextMatch(value: Boolean): ContextualParserApproach.this.type

    When set to true, it will stop finding for matches when prefix/suffix data is found in the text.

  97. val shortestContextMatch: BooleanParam

    When set to true, it will stop finding for matches when prefix/suffix data is found in the text.

  98. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  99. def toString(): String
    Definition Classes
    Identifiable → AnyRef → Any
  100. def train(dataset: Dataset[_], recursivePipeline: Option[PipelineModel]): ContextualParserModel
    Definition Classes
    ContextualParserApproach → AnnotatorApproach
  101. final def transformSchema(schema: StructType): StructType
    Definition Classes
    AnnotatorApproach → PipelineStage
  102. def transformSchema(schema: StructType, logging: Boolean): StructType
    Attributes
    protected
    Definition Classes
    PipelineStage
    Annotations
    @DeveloperApi()
  103. val uid: String
    Definition Classes
    ContextualParserApproach → Identifiable
  104. def validate(schema: StructType): Boolean
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  105. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  106. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  107. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  108. def write: MLWriter
    Definition Classes
    DefaultParamsWritable → MLWritable

Inherited from CheckLicense

Inherited from HandleExceptionParams

Inherited from AnnotatorApproach[ContextualParserModel]

Inherited from CanBeLazy

Inherited from DefaultParamsWritable

Inherited from MLWritable

Inherited from HasOutputAnnotatorType

Inherited from HasOutputAnnotationCol

Inherited from HasInputAnnotationCols

Inherited from Estimator[ContextualParserModel]

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

Annotator types

Required input and expected output annotator types

Members

Parameter setters