class ContextualParserApproach extends AnnotatorApproach[ContextualParserModel] 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, 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. AnnotatorApproach
  4. CanBeLazy
  5. DefaultParamsWritable
  6. MLWritable
  7. HasOutputAnnotatorType
  8. HasOutputAnnotationCol
  9. HasInputAnnotationCols
  10. Estimator
  11. PipelineStage
  12. Logging
  13. Params
  14. Serializable
  15. Serializable
  16. Identifiable
  17. AnyRef
  18. 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. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  23. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  24. def explainParam(param: Param[_]): String
    Definition Classes
    Params
  25. def explainParams(): String
    Definition Classes
    Params
  26. final def extractParamMap(): ParamMap
    Definition Classes
    Params
  27. final def extractParamMap(extra: ParamMap): ParamMap
    Definition Classes
    Params
  28. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  29. final def fit(dataset: Dataset[_]): ContextualParserModel
    Definition Classes
    AnnotatorApproach → Estimator
  30. def fit(dataset: Dataset[_], paramMaps: Seq[ParamMap]): Seq[ContextualParserModel]
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  31. def fit(dataset: Dataset[_], paramMap: ParamMap): ContextualParserModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  32. def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): ContextualParserModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" ) @varargs()
  33. final def get[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  34. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  35. final def getDefault[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  36. def getEntityDefinition: Option[EntityDefinition]
  37. def getInputCols: Array[String]
    Definition Classes
    HasInputAnnotationCols
  38. def getLazyAnnotator: Boolean
    Definition Classes
    CanBeLazy
  39. final def getOrDefault[T](param: Param[T]): T
    Definition Classes
    Params
  40. final def getOutputCol: String
    Definition Classes
    HasOutputAnnotationCol
  41. def getParam(paramName: String): Param[Any]
    Definition Classes
    Params
  42. final def hasDefault[T](param: Param[T]): Boolean
    Definition Classes
    Params
  43. def hasParam(paramName: String): Boolean
    Definition Classes
    Params
  44. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  45. def initializeLogIfNecessary(isInterpreter: Boolean, silent: Boolean): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  46. def initializeLogIfNecessary(isInterpreter: Boolean): Unit
    Attributes
    protected
    Definition Classes
    Logging
  47. val inputAnnotatorTypes: Array[String]

    Input annotator types: DOCUMENT, TOKEN

    Input annotator types: DOCUMENT, TOKEN

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

    Path to json file with regex rules

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

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

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

    Output annotator types: CHUNK

    Output annotator types: CHUNK

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

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

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

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

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

    Whether to do an exact match of prefix and suffix.

  84. final def setDefault(paramPairs: ParamPair[_]*): ContextualParserApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  85. final def setDefault[T](param: Param[T], value: T): ContextualParserApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  86. 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.

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

    Path to json file with regex rules

  90. def setLazyAnnotator(value: Boolean): ContextualParserApproach.this.type
    Definition Classes
    CanBeLazy
  91. def setOptionalContextRules(value: Boolean): ContextualParserApproach.this.type

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

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

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

  94. 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.

  95. val shortestContextMatch: BooleanParam

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

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

Inherited from CheckLicense

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