Packages

class RegexMatcherInternal extends AnnotatorApproach[RegexMatcherInternalModel] with MergeCommonParams with CheckLicense

Uses rules to match a set of regular expressions and associate them with a provided entity.

A rule consists of a regex pattern and an entity, delimited by a character of choice. An example could be \d{4}\/\d\d\/\d\d,date which will match strings like "1970/01/01" to the entity "date".

Rules must be provided by either setRules (followed by setDelimiter) or an external file.

To use an external file, a dictionary of predefined regular expressions must be provided with setExternalRules. The dictionary can be set in either in the form of a delimited text file or directly as an ExternalResource.

Example

In this example, the rules.txt has the form of

the\s\w+, followed by 'the'
ceremonies, ceremony

where each regex is separated by the entity by ","

import ResourceHelper.spark.implicits._
import com.johnsnowlabs.nlp.base.DocumentAssembler
import com.johnsnowlabs.nlp.annotator.SentenceDetector
import com.johnsnowlabs.nlp.annotators.regex.RegexMatcherInternal
import org.apache.spark.ml.Pipeline

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

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

val regexMatcher = new RegexMatcherInternal()
  .setExternalRules("src/test/resources/regex-matcher/rules.txt",  ",")
  .setInputCols(Array("sentence"))
  .setOutputCol("regex")
  .setStrategy("MATCH_ALL")

val pipeline = new Pipeline().setStages(Array(documentAssembler, sentence, regexMatcher))

val data = Seq(
  "My first sentence with the first rule. This is my second sentence with ceremonies rule."
).toDF("text")
val results = pipeline.fit(data).transform(data)

results.selectExpr("explode(regex) as result").show(false)
+--------------------------------------------------------------------------------------------+
|result                                                                                      |
+--------------------------------------------------------------------------------------------+
|[chunk, 23, 31, the first, [entity -> followed by 'the', sentence -> 0, chunk -> 0], []]|
|[chunk, 71, 80, ceremonies, [entity -> ceremony, sentence -> 1, chunk -> 0], []]        |
+--------------------------------------------------------------------------------------------+
Linear Supertypes
CheckLicense, MergeCommonParams, AnnotatorApproach[RegexMatcherInternalModel], CanBeLazy, DefaultParamsWritable, MLWritable, HasOutputAnnotatorType, HasOutputAnnotationCol, HasInputAnnotationCols, Estimator[RegexMatcherInternalModel], PipelineStage, Logging, Params, Serializable, Serializable, Identifiable, AnyRef, Any
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. RegexMatcherInternal
  2. CheckLicense
  3. MergeCommonParams
  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 RegexMatcherInternal()
  2. new RegexMatcherInternal(uid: String)

    uid

    internal element required 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. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  5. def _fit(dataset: Dataset[_], recursiveStages: Option[PipelineModel]): RegexMatcherInternalModel
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def beforeTraining(spark: SparkSession): Unit
    Definition Classes
    AnnotatorApproach
  8. final def checkSchema(schema: StructType, inputAnnotatorType: String): Boolean
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  9. def checkValidEnvironment(spark: Option[SparkSession], scopes: Seq[String]): Unit
    Definition Classes
    CheckLicense
  10. def checkValidScope(scope: String): Unit
    Definition Classes
    CheckLicense
  11. def checkValidScopeAndEnvironment(scope: String, spark: Option[SparkSession], checkLp: Boolean): Unit
    Definition Classes
    CheckLicense
  12. def checkValidScopesAndEnvironment(scopes: Seq[String], spark: Option[SparkSession], checkLp: Boolean): Unit
    Definition Classes
    CheckLicense
  13. final def clear(param: Param[_]): RegexMatcherInternal.this.type
    Definition Classes
    Params
  14. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  15. final def copy(extra: ParamMap): Estimator[RegexMatcherInternalModel]
    Definition Classes
    AnnotatorApproach → Estimator → PipelineStage → Params
  16. def copyValues[T <: Params](to: T, extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  17. final def defaultCopy[T <: Params](extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  18. val delimiter: Param[String]

    Delimiter for rules provided with setRules

  19. val description: String

    Matches described regex rules that come in tuples in a text file

    Matches described regex rules that come in tuples in a text file

    Definition Classes
    RegexMatcherInternal → AnnotatorApproach
  20. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  21. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  22. def explainParam(param: Param[_]): String
    Definition Classes
    Params
  23. def explainParams(): String
    Definition Classes
    Params
  24. val externalRules: ExternalResourceParam

    External resource to rules, needs 'delimiter' in options

  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[_]): RegexMatcherInternalModel
    Definition Classes
    AnnotatorApproach → Estimator
  29. def fit(dataset: Dataset[_], paramMaps: Seq[ParamMap]): Seq[RegexMatcherInternalModel]
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  30. def fit(dataset: Dataset[_], paramMap: ParamMap): RegexMatcherInternalModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  31. def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): RegexMatcherInternalModel
    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 getInputCols: Array[String]
    Definition Classes
    HasInputAnnotationCols
  36. def getLazyAnnotator: Boolean
    Definition Classes
    CanBeLazy
  37. def getMergeOverlapping: Boolean
    Definition Classes
    MergeCommonParams
  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. def getStrategy: String

    Strategy for which to match the expressions (Default: "MATCH_ALL")

  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[AnnotatorType]

    Input annotator type: DOCUMENT

    Input annotator type: DOCUMENT

    Definition Classes
    RegexMatcherInternal → 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 lazyAnnotator: BooleanParam
    Definition Classes
    CanBeLazy
  54. def log: Logger
    Attributes
    protected
    Definition Classes
    Logging
  55. def logDebug(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  56. def logDebug(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  57. def logError(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  58. def logError(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  59. def logInfo(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  60. def logInfo(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  61. def logName: String
    Attributes
    protected
    Definition Classes
    Logging
  62. def logTrace(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  63. def logTrace(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  64. def logWarning(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  65. def logWarning(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  66. val mergeOverlapping: BooleanParam

    whether to merge overlapping matched chunks.

    whether to merge overlapping matched chunks. Defaults to true

    Definition Classes
    MergeCommonParams
  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: RegexMatcherInternalModel, spark: SparkSession): Unit
    Definition Classes
    AnnotatorApproach
  72. val optionalInputAnnotatorTypes: Array[String]
    Definition Classes
    HasInputAnnotationCols
  73. val outputAnnotatorType: AnnotatorType

    Input annotator type: CHUNK

    Input annotator type: CHUNK

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

    Rules with regex pattern and entities for matching

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

    Sets the regex rules to match the entity with.

    Sets the regex rules to match the entity with.

    Note that only either externalRules or rules can be set at once.

    value

    Array of rules and entities as tuples

  84. def setExternalRules(path: String, delimiter: String, readAs: Format = ReadAs.TEXT, options: Map[String, String] = Map("format" -> "text")): RegexMatcherInternal.this.type

    External dictionary to be used by the lemmatizer, which needs delimiter set for parsing the resource.

    External dictionary to be used by the lemmatizer, which needs delimiter set for parsing the resource.

    Note that only either externalRules or rules can be set at once.

  85. def setExternalRules(value: ExternalResource): RegexMatcherInternal.this.type

    External dictionary already in the form of ExternalResource, for which the Map member options has "delimiter" defined.

    External dictionary already in the form of ExternalResource, for which the Map member options has "delimiter" defined.

    Note that only either externalRules or rules can be set at once.

    Example

    val regexMatcher = new RegexMatcherInternal()
      .setExternalRules(ExternalResource(
        "src/test/resources/regex-matcher/rules.txt",
        ReadAs.TEXT,
        Map("delimiter" -> ",")
      ))
      .setInputCols("sentence")
      .setOutputCol("regex")
      .setStrategy(strategy)
  86. final def setInputCols(value: String*): RegexMatcherInternal.this.type
    Definition Classes
    HasInputAnnotationCols
  87. def setInputCols(value: Array[String]): RegexMatcherInternal.this.type
    Definition Classes
    HasInputAnnotationCols
  88. def setLazyAnnotator(value: Boolean): RegexMatcherInternal.this.type
    Definition Classes
    CanBeLazy
  89. def setMergeOverlapping(v: Boolean): RegexMatcherInternal.this.type

    whether to merge overlapping matched chunks.

    whether to merge overlapping matched chunks.

    Definition Classes
    MergeCommonParams
  90. final def setOutputCol(value: String): RegexMatcherInternal.this.type
    Definition Classes
    HasOutputAnnotationCol
  91. def setRules(value: Array[String]): RegexMatcherInternal.this.type

    Sets the regex rules to match the entity with.

    Sets the regex rules to match the entity with.

    The rules must consist of a regex pattern and an entity for that pattern. The regex pattern and the entity must be delimited by a character that will also have to set with setDelimiter.

    Only one of either parameter rules or externalRules must be set.

    Example

    val regexMatcher = new RegexMatcherInternal()
      .setRules(Array("\d{4}\/\d\d\/\d\d,date", "\d{2}\/\d\d\/\d\d,date_short")
      .setDelimiter(",")
      .setInputCols("sentence")
      .setOutputCol("regex")
      .setStrategy("MATCH_ALL")
    value

    Array of rules

  92. def setStrategy(value: String): RegexMatcherInternal.this.type

    Strategy for which to match the expressions (Default: "MATCH_ALL")

  93. val strategy: Param[String]

    Strategy for which to match the expressions (Default: "MATCH_ALL").

    Strategy for which to match the expressions (Default: "MATCH_ALL"). Possible values are:

    • MATCH_ALL brings one-to-many results
    • MATCH_FIRST catches only first match
    • MATCH_COMPLETE returns only if match is entire target.
  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]): RegexMatcherInternalModel
    Definition Classes
    RegexMatcherInternal → 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
    RegexMatcherInternal → Identifiable
  100. def validate(schema: StructType): Boolean
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  101. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  102. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  103. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  104. def write: MLWriter
    Definition Classes
    DefaultParamsWritable → MLWritable

Inherited from CheckLicense

Inherited from MergeCommonParams

Inherited from AnnotatorApproach[RegexMatcherInternalModel]

Inherited from CanBeLazy

Inherited from DefaultParamsWritable

Inherited from MLWritable

Inherited from HasOutputAnnotatorType

Inherited from HasOutputAnnotationCol

Inherited from HasInputAnnotationCols

Inherited from Estimator[RegexMatcherInternalModel]

Inherited from PipelineStage

Inherited from Logging

Inherited from Params

Inherited from Serializable

Inherited from Serializable

Inherited from Identifiable

Inherited from AnyRef

Inherited from Any

Annotator types

Required input and expected output annotator types

Parameters

A list of (hyper-)parameter keys this annotator can take. Users can set and get the parameter values through setters and getters, respectively.

Parameter setters

Parameter getters

Members