Packages

class ContextualParserApproach extends AnnotatorApproach[ContextualParserModel] with HasRecursiveFit[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)
  .setContextMatch(false)
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 -> , confidenceValue -> 0.13, hits -> regex, sentence -> 0}, []}   |
|{chunk, 49, 50, T5, {field -> Stage, normalized -> , confidenceValue -> 0.13, hits -> regex, sentence -> 0}, []}         |
|{chunk, 148, 156, cT4bcN2M1, {field -> Stage, normalized -> , confidenceValue -> 0.13, hits -> regex, sentence -> 1}, []}|
|{chunk, 189, 194, T?N3M1, {field -> Stage, normalized -> , confidenceValue -> 0.13, hits -> regex, sentence -> 2}, []}   |
|{chunk, 316, 323, pT1bN0M0, {field -> Stage, normalized -> , confidenceValue -> 0.13, hits -> regex, sentence -> 3}, []} |
+-------------------------------------------------------------------------------------------------------------------------+
See also

ContextualParserModel for the trained model

Linear Supertypes
CheckLicense, HasRecursiveFit[ContextualParserModel], 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. HasRecursiveFit
  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[SparkContext]): Unit
    Definition Classes
    CheckLicense
  11. def checkValidScope(scope: String): Unit
    Definition Classes
    CheckLicense
  12. def checkValidScopeAndEnvironment(scope: String, spark: Option[SparkContext], checkLp: Boolean): Unit
    Definition Classes
    CheckLicense
  13. final def clear(param: Param[_]): ContextualParserApproach.this.type
    Definition Classes
    Params
  14. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  15. val contextMatch: BooleanParam

    Whether to include context to annotate the hit (Default: true)

  16. final def copy(extra: ParamMap): Estimator[ContextualParserModel]
    Definition Classes
    AnnotatorApproach → Estimator → PipelineStage → Params
  17. def copyValues[T <: Params](to: T, extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  18. final def defaultCopy[T <: Params](extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  19. val description: String
    Definition Classes
    ContextualParserApproach → AnnotatorApproach
  20. 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.

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

    Input annotator types: DOCUMENT, TOKEN

    Input annotator types: DOCUMENT, TOKEN

    Definition Classes
    ContextualParserApproach → HasInputAnnotationCols
  47. final val inputCols: StringArrayParam
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  48. final def isDefined(param: Param[_]): Boolean
    Definition Classes
    Params
  49. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  50. def isLp(): Boolean
    Definition Classes
    CheckLicense
  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 optionalInputAnnotatorTypes: Array[String]
    Definition Classes
    HasInputAnnotationCols
  73. val outputAnnotatorType: AnnotatorType

    Output annotator types: CHUNK

    Output annotator types: CHUNK

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

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

  77. final def recursiveFit(dataset: Dataset[_], recursivePipeline: PipelineModel): ContextualParserModel
    Definition Classes
    HasRecursiveFit
  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 setContextMatch(value: Boolean): ContextualParserApproach.this.type

    Whether to include context to annotate the hit (Default: true)

  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. final 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. final def setOutputCol(value: String): ContextualParserApproach.this.type
    Definition Classes
    HasOutputAnnotationCol
  92. def setPrefixAndSuffixMatch(value: Boolean): ContextualParserApproach.this.type

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

  93. def setUpdateTokenizer(value: Boolean): ContextualParserApproach.this.type

    Whether to update tokenizer from pipeline when detecting multiple words on dictionary values (Default: true)

  94. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  95. def toString(): String
    Definition Classes
    Identifiable → AnyRef → Any
  96. def train(dataset: Dataset[_], recursivePipeline: Option[PipelineModel]): ContextualParserModel
    Definition Classes
    ContextualParserApproach → AnnotatorApproach
  97. final def transformSchema(schema: StructType): StructType
    Definition Classes
    AnnotatorApproach → PipelineStage
  98. def transformSchema(schema: StructType, logging: Boolean): StructType
    Attributes
    protected
    Definition Classes
    PipelineStage
    Annotations
    @DeveloperApi()
  99. val uid: String
    Definition Classes
    ContextualParserApproach → Identifiable
  100. val updateTokenizer: BooleanParam

    Whether to update tokenizer from pipeline when detecting multiple words on dictionary values (Default: true)

  101. def validate(schema: StructType): Boolean
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  102. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  103. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  104. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  105. def write: MLWriter
    Definition Classes
    DefaultParamsWritable → MLWritable

Inherited from CheckLicense

Inherited from HasRecursiveFit[ContextualParserModel]

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