Packages

class ContextualParserApproach extends AnnotatorApproach[ContextualParserModel] with HasRecursiveFit[ContextualParserModel] with Licensed

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
Licensed, 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. Licensed
  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. final def clear(param: Param[_]): ContextualParserApproach.this.type
    Definition Classes
    Params
  11. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  12. val contextMatch: BooleanParam

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

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

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

    Input annotator types: DOCUMENT, TOKEN

    Input annotator types: DOCUMENT, TOKEN

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

    Path to json file with regex rules

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

    Output annotator types: CHUNK

    Output annotator types: CHUNK

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

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

  72. final def recursiveFit(dataset: Dataset[_], recursivePipeline: PipelineModel): ContextualParserModel
    Definition Classes
    HasRecursiveFit
  73. def save(path: String): Unit
    Definition Classes
    MLWritable
    Annotations
    @Since( "1.6.0" ) @throws( ... )
  74. final def set(paramPair: ParamPair[_]): ContextualParserApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  75. final def set(param: String, value: Any): ContextualParserApproach.this.type
    Attributes
    protected
    Definition Classes
    Params
  76. final def set[T](param: Param[T], value: T): ContextualParserApproach.this.type
    Definition Classes
    Params
  77. def setCaseSensitive(value: Boolean): ContextualParserApproach.this.type

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

  78. def setContextMatch(value: Boolean): ContextualParserApproach.this.type

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

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

  82. final def setInputCols(value: String*): ContextualParserApproach.this.type
    Definition Classes
    HasInputAnnotationCols
  83. final def setInputCols(value: Array[String]): ContextualParserApproach.this.type
    Definition Classes
    HasInputAnnotationCols
  84. def setJsonPath(value: String): ContextualParserApproach.this.type

    Path to json file with regex rules

  85. def setLazyAnnotator(value: Boolean): ContextualParserApproach.this.type
    Definition Classes
    CanBeLazy
  86. final def setOutputCol(value: String): ContextualParserApproach.this.type
    Definition Classes
    HasOutputAnnotationCol
  87. def setPrefixAndSuffixMatch(value: Boolean): ContextualParserApproach.this.type

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

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

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

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

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

  96. def validate(schema: StructType): Boolean
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  97. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  98. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  99. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  100. def write: MLWriter
    Definition Classes
    DefaultParamsWritable → MLWritable

Inherited from Licensed

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