com.johnsnowlabs.nlp.annotators.regex
RegexMatcherInternal
Companion object RegexMatcherInternal
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], []] | +--------------------------------------------------------------------------------------------+
- Grouped
- Alphabetic
- By Inheritance
- RegexMatcherInternal
- CheckLicense
- MergeCommonParams
- AnnotatorApproach
- CanBeLazy
- DefaultParamsWritable
- MLWritable
- HasOutputAnnotatorType
- HasOutputAnnotationCol
- HasInputAnnotationCols
- Estimator
- PipelineStage
- Logging
- Params
- Serializable
- Serializable
- Identifiable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
Type Members
-
type
AnnotatorType = String
- Definition Classes
- HasOutputAnnotatorType
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
$[T](param: Param[T]): T
- Attributes
- protected
- Definition Classes
- Params
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
_fit(dataset: Dataset[_], recursiveStages: Option[PipelineModel]): RegexMatcherInternalModel
- Attributes
- protected
- Definition Classes
- AnnotatorApproach
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
beforeTraining(spark: SparkSession): Unit
- Definition Classes
- AnnotatorApproach
-
final
def
checkSchema(schema: StructType, inputAnnotatorType: String): Boolean
- Attributes
- protected
- Definition Classes
- HasInputAnnotationCols
-
def
checkValidEnvironment(spark: Option[SparkSession], scopes: Seq[String]): Unit
- Definition Classes
- CheckLicense
-
def
checkValidScope(scope: String): Unit
- Definition Classes
- CheckLicense
-
def
checkValidScopeAndEnvironment(scope: String, spark: Option[SparkSession], checkLp: Boolean): Unit
- Definition Classes
- CheckLicense
-
def
checkValidScopesAndEnvironment(scopes: Seq[String], spark: Option[SparkSession], checkLp: Boolean): Unit
- Definition Classes
- CheckLicense
-
final
def
clear(param: Param[_]): RegexMatcherInternal.this.type
- Definition Classes
- Params
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
copy(extra: ParamMap): Estimator[RegexMatcherInternalModel]
- Definition Classes
- AnnotatorApproach → Estimator → PipelineStage → Params
-
def
copyValues[T <: Params](to: T, extra: ParamMap): T
- Attributes
- protected
- Definition Classes
- Params
-
final
def
defaultCopy[T <: Params](extra: ParamMap): T
- Attributes
- protected
- Definition Classes
- Params
-
val
delimiter: Param[String]
Delimiter for rules provided with setRules
-
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
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
explainParam(param: Param[_]): String
- Definition Classes
- Params
-
def
explainParams(): String
- Definition Classes
- Params
-
val
externalRules: ExternalResourceParam
External resource to rules, needs 'delimiter' in options
-
final
def
extractParamMap(): ParamMap
- Definition Classes
- Params
-
final
def
extractParamMap(extra: ParamMap): ParamMap
- Definition Classes
- Params
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
fit(dataset: Dataset[_]): RegexMatcherInternalModel
- Definition Classes
- AnnotatorApproach → Estimator
-
def
fit(dataset: Dataset[_], paramMaps: Seq[ParamMap]): Seq[RegexMatcherInternalModel]
- Definition Classes
- Estimator
- Annotations
- @Since( "2.0.0" )
-
def
fit(dataset: Dataset[_], paramMap: ParamMap): RegexMatcherInternalModel
- Definition Classes
- Estimator
- Annotations
- @Since( "2.0.0" )
-
def
fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): RegexMatcherInternalModel
- Definition Classes
- Estimator
- Annotations
- @Since( "2.0.0" ) @varargs()
-
final
def
get[T](param: Param[T]): Option[T]
- Definition Classes
- Params
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
getDefault[T](param: Param[T]): Option[T]
- Definition Classes
- Params
-
def
getInputCols: Array[String]
- Definition Classes
- HasInputAnnotationCols
-
def
getLazyAnnotator: Boolean
- Definition Classes
- CanBeLazy
-
def
getMergeOverlapping: Boolean
- Definition Classes
- MergeCommonParams
-
final
def
getOrDefault[T](param: Param[T]): T
- Definition Classes
- Params
-
final
def
getOutputCol: String
- Definition Classes
- HasOutputAnnotationCol
-
def
getParam(paramName: String): Param[Any]
- Definition Classes
- Params
-
def
getStrategy: String
Strategy for which to match the expressions (Default:
"MATCH_ALL"
) -
final
def
hasDefault[T](param: Param[T]): Boolean
- Definition Classes
- Params
-
def
hasParam(paramName: String): Boolean
- Definition Classes
- Params
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
initializeLogIfNecessary(isInterpreter: Boolean, silent: Boolean): Boolean
- Attributes
- protected
- Definition Classes
- Logging
-
def
initializeLogIfNecessary(isInterpreter: Boolean): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
val
inputAnnotatorTypes: Array[AnnotatorType]
Input annotator type: DOCUMENT
Input annotator type: DOCUMENT
- Definition Classes
- RegexMatcherInternal → HasInputAnnotationCols
-
final
val
inputCols: StringArrayParam
- Attributes
- protected
- Definition Classes
- HasInputAnnotationCols
-
final
def
isDefined(param: Param[_]): Boolean
- Definition Classes
- Params
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
isSet(param: Param[_]): Boolean
- Definition Classes
- Params
-
def
isTraceEnabled(): Boolean
- Attributes
- protected
- Definition Classes
- Logging
-
val
lazyAnnotator: BooleanParam
- Definition Classes
- CanBeLazy
-
def
log: Logger
- Attributes
- protected
- Definition Classes
- Logging
-
def
logDebug(msg: ⇒ String, throwable: Throwable): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logDebug(msg: ⇒ String): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logError(msg: ⇒ String, throwable: Throwable): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logError(msg: ⇒ String): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logInfo(msg: ⇒ String, throwable: Throwable): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logInfo(msg: ⇒ String): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logName: String
- Attributes
- protected
- Definition Classes
- Logging
-
def
logTrace(msg: ⇒ String, throwable: Throwable): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logTrace(msg: ⇒ String): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logWarning(msg: ⇒ String, throwable: Throwable): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
def
logWarning(msg: ⇒ String): Unit
- Attributes
- protected
- Definition Classes
- Logging
-
val
mergeOverlapping: BooleanParam
whether to merge overlapping matched chunks.
whether to merge overlapping matched chunks. Defaults to true
- Definition Classes
- MergeCommonParams
-
def
msgHelper(schema: StructType): String
- Attributes
- protected
- Definition Classes
- HasInputAnnotationCols
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
onTrained(model: RegexMatcherInternalModel, spark: SparkSession): Unit
- Definition Classes
- AnnotatorApproach
-
val
optionalInputAnnotatorTypes: Array[String]
- Definition Classes
- HasInputAnnotationCols
-
val
outputAnnotatorType: AnnotatorType
Input annotator type: CHUNK
Input annotator type: CHUNK
- Definition Classes
- RegexMatcherInternal → HasOutputAnnotatorType
-
final
val
outputCol: Param[String]
- Attributes
- protected
- Definition Classes
- HasOutputAnnotationCol
-
lazy val
params: Array[Param[_]]
- Definition Classes
- Params
-
val
rules: StringArrayParam
Rules with regex pattern and entities for matching
-
def
save(path: String): Unit
- Definition Classes
- MLWritable
- Annotations
- @Since( "1.6.0" ) @throws( ... )
-
final
def
set(paramPair: ParamPair[_]): RegexMatcherInternal.this.type
- Attributes
- protected
- Definition Classes
- Params
-
final
def
set(param: String, value: Any): RegexMatcherInternal.this.type
- Attributes
- protected
- Definition Classes
- Params
-
final
def
set[T](param: Param[T], value: T): RegexMatcherInternal.this.type
- Definition Classes
- Params
-
final
def
setDefault(paramPairs: ParamPair[_]*): RegexMatcherInternal.this.type
- Attributes
- protected
- Definition Classes
- Params
-
final
def
setDefault[T](param: Param[T], value: T): RegexMatcherInternal.this.type
- Attributes
- protected[org.apache.spark.ml]
- Definition Classes
- Params
-
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
-
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.
-
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)
-
final
def
setInputCols(value: String*): RegexMatcherInternal.this.type
- Definition Classes
- HasInputAnnotationCols
-
def
setInputCols(value: Array[String]): RegexMatcherInternal.this.type
- Definition Classes
- HasInputAnnotationCols
-
def
setLazyAnnotator(value: Boolean): RegexMatcherInternal.this.type
- Definition Classes
- CanBeLazy
-
def
setMergeOverlapping(v: Boolean): RegexMatcherInternal.this.type
whether to merge overlapping matched chunks.
whether to merge overlapping matched chunks.
- Definition Classes
- MergeCommonParams
-
final
def
setOutputCol(value: String): RegexMatcherInternal.this.type
- Definition Classes
- HasOutputAnnotationCol
-
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
orexternalRules
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
-
def
setStrategy(value: String): RegexMatcherInternal.this.type
Strategy for which to match the expressions (Default:
"MATCH_ALL"
) -
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.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- Identifiable → AnyRef → Any
-
def
train(dataset: Dataset[_], recursivePipeline: Option[PipelineModel]): RegexMatcherInternalModel
- Definition Classes
- RegexMatcherInternal → AnnotatorApproach
-
final
def
transformSchema(schema: StructType): StructType
- Definition Classes
- AnnotatorApproach → PipelineStage
-
def
transformSchema(schema: StructType, logging: Boolean): StructType
- Attributes
- protected
- Definition Classes
- PipelineStage
- Annotations
- @DeveloperApi()
-
val
uid: String
- Definition Classes
- RegexMatcherInternal → Identifiable
-
def
validate(schema: StructType): Boolean
- Attributes
- protected
- Definition Classes
- AnnotatorApproach
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
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.