Packages

class Tokenizer extends AnnotatorApproach[TokenizerModel]

Tokenizes raw text in document type columns into TokenizedSentence .

This class represents a non fitted tokenizer. Fitting it will cause the internal RuleFactory to construct the rules for tokenizing from the input configuration.

Identifies tokens with tokenization open standards. A few rules will help customizing it if defaults do not fit user needs.

For extended examples of usage see the Examples and Tokenizer test class

Example

import spark.implicits._
import com.johnsnowlabs.nlp.DocumentAssembler
import com.johnsnowlabs.nlp.annotators.Tokenizer
import org.apache.spark.ml.Pipeline

val data = Seq("I'd like to say we didn't expect that. Jane's boyfriend.").toDF("text")
val documentAssembler = new DocumentAssembler().setInputCol("text").setOutputCol("document")
val tokenizer = new Tokenizer().setInputCols("document").setOutputCol("token").fit(data)

val pipeline = new Pipeline().setStages(Array(documentAssembler, tokenizer)).fit(data)
val result = pipeline.transform(data)

result.selectExpr("token.result").show(false)
+-----------------------------------------------------------------------+
|output                                                                 |
+-----------------------------------------------------------------------+
|[I'd, like, to, say, we, didn't, expect, that, ., Jane's, boyfriend, .]|
+-----------------------------------------------------------------------+
Linear Supertypes
AnnotatorApproach[TokenizerModel], CanBeLazy, DefaultParamsWritable, MLWritable, HasOutputAnnotatorType, HasOutputAnnotationCol, HasInputAnnotationCols, Estimator[TokenizerModel], PipelineStage, Logging, Params, Serializable, Serializable, Identifiable, AnyRef, Any
Known Subclasses
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. Tokenizer
  2. AnnotatorApproach
  3. CanBeLazy
  4. DefaultParamsWritable
  5. MLWritable
  6. HasOutputAnnotatorType
  7. HasOutputAnnotationCol
  8. HasInputAnnotationCols
  9. Estimator
  10. PipelineStage
  11. Logging
  12. Params
  13. Serializable
  14. Serializable
  15. Identifiable
  16. AnyRef
  17. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Tokenizer()
  2. new Tokenizer(uid: String)

    uid

    required uid 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]): TokenizerModel
    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  6. def addContextChars(v: String): Tokenizer.this.type

    Add one character string to rip off from tokens, such as parenthesis or question marks.

    Add one character string to rip off from tokens, such as parenthesis or question marks. Ignored if using prefix, infix or suffix patterns.

  7. def addException(value: String): Tokenizer.this.type

    Add a single exception

  8. def addInfixPattern(value: String): Tokenizer.this.type

    Add an extension pattern regex with groups to the top of thsetExceptionse rules (will target first, from more specific to the more general).

  9. def addSplitChars(v: String): Tokenizer.this.type

    One character string to split tokens inside, such as hyphens.

    One character string to split tokens inside, such as hyphens. Ignored if using infix, prefix or suffix patterns.

  10. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  11. def beforeTraining(spark: SparkSession): Unit
    Definition Classes
    AnnotatorApproach
  12. def buildRuleFactory: RuleFactory

    Build rule factory which combines all defined parameters to build regex that is applied to tokens

  13. val caseSensitiveExceptions: BooleanParam

    Whether to care for case sensitiveness in exceptions (Default: true)

  14. final def checkSchema(schema: StructType, inputAnnotatorType: String): Boolean
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  15. final def clear(param: Param[_]): Tokenizer.this.type
    Definition Classes
    Params
  16. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  17. val contextChars: StringArrayParam

    Character list used to separate from token boundaries (Default: Array(".", ",", ";", ":", "!", "?", "*", "-", "(", ")", "\"", "'"))

  18. final def copy(extra: ParamMap): Estimator[TokenizerModel]
    Definition Classes
    AnnotatorApproach → Estimator → PipelineStage → Params
  19. def copyValues[T <: Params](to: T, extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  20. final def defaultCopy[T <: Params](extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  21. val description: String

    Annotator that identifies points of analysis in a useful manner

    Annotator that identifies points of analysis in a useful manner

    Definition Classes
    TokenizerAnnotatorApproach
  22. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  23. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  24. val exceptions: StringArrayParam

    Words that won't be affected by tokenization rules

  25. val exceptionsPath: ExternalResourceParam

    Path to file containing list of exceptions

  26. def explainParam(param: Param[_]): String
    Definition Classes
    Params
  27. def explainParams(): String
    Definition Classes
    Params
  28. final def extractParamMap(): ParamMap
    Definition Classes
    Params
  29. final def extractParamMap(extra: ParamMap): ParamMap
    Definition Classes
    Params
  30. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  31. final def fit(dataset: Dataset[_]): TokenizerModel
    Definition Classes
    AnnotatorApproach → Estimator
  32. def fit(dataset: Dataset[_], paramMaps: Seq[ParamMap]): Seq[TokenizerModel]
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  33. def fit(dataset: Dataset[_], paramMap: ParamMap): TokenizerModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" )
  34. def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): TokenizerModel
    Definition Classes
    Estimator
    Annotations
    @Since( "2.0.0" ) @varargs()
  35. final def get[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  36. def getCaseSensitiveExceptions(value: Boolean): Boolean

    Whether to follow case sensitiveness for matching exceptions in text

  37. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  38. def getContextChars: Array[String]

    List of 1 character string to rip off from tokens, such as parenthesis or question marks.

    List of 1 character string to rip off from tokens, such as parenthesis or question marks. Ignored if using prefix, infix or suffix patterns.

  39. final def getDefault[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  40. def getExceptions: Array[String]

  41. def getInfixPatterns: Array[String]

    Add an extension pattern regex with groups to the top of the rules (will target first, from more specific to the more general).

  42. def getInputCols: Array[String]

    returns

    input annotations columns currently used

    Definition Classes
    HasInputAnnotationCols
  43. def getLazyAnnotator: Boolean
    Definition Classes
    CanBeLazy
  44. def getMaxLength(value: Int): Int

    Get the maximum allowed length for each token

  45. def getMinLength(value: Int): Int

    Get the minimum allowed length for each token

  46. final def getOrDefault[T](param: Param[T]): T
    Definition Classes
    Params
  47. final def getOutputCol: String

    Gets annotation column name going to generate

    Gets annotation column name going to generate

    Definition Classes
    HasOutputAnnotationCol
  48. def getParam(paramName: String): Param[Any]
    Definition Classes
    Params
  49. def getPrefixPattern: String

    Regex to identify subtokens that come in the beginning of the token.

    Regex to identify subtokens that come in the beginning of the token. Regex has to start with \\A and must contain groups (). Each group will become a separate token within the prefix. Defaults to non-letter characters. e.g. quotes or parenthesis

  50. def getSplitChars: Array[String]

    List of 1 character string to split tokens inside, such as hyphens.

    List of 1 character string to split tokens inside, such as hyphens. Ignored if using infix, prefix or suffix patterns.

  51. def getSplitPattern: String

    List of 1 character string to split tokens inside, such as hyphens.

    List of 1 character string to split tokens inside, such as hyphens. Ignored if using infix, prefix or suffix patterns.

  52. def getSuffixPattern: String

    Regex to identify subtokens that are in the end of the token.

    Regex to identify subtokens that are in the end of the token. Regex has to end with \\z and must contain groups (). Each group will become a separate token within the prefix. Defaults to non-letter characters. e.g. quotes or parenthesis

  53. def getTargetPattern: String

    Basic regex rule to identify a candidate for tokenization.

    Basic regex rule to identify a candidate for tokenization. Defaults to \\S+ which means anything not a space

  54. final def hasDefault[T](param: Param[T]): Boolean
    Definition Classes
    Params
  55. def hasParam(paramName: String): Boolean
    Definition Classes
    Params
  56. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  57. val infixPatterns: StringArrayParam

    Regex patterns that match tokens within a single target.

    Regex patterns that match tokens within a single target. groups identify different sub-tokens. multiple defaults

    Infix patterns must use regex group. Notice each group will result in separate token

    Example:

    import org.apache.spark.ml.Pipeline
    import com.johnsnowlabs.nlp.annotators.Tokenizer
    import com.johnsnowlabs.nlp.DocumentAssembler
    
    val textDf = sqlContext.sparkContext.parallelize(Array("l'une d'un l'un, des l'extrême des l'extreme")).toDF("text")
    val documentAssembler = new DocumentAssembler().setInputCol("text").setOutputCol("sentences")
    val tokenizer = new Tokenizer().setInputCols("sentences").setOutputCol("tokens").setInfixPatterns(Array("([\\p{L}\\w]+'{1})([\\p{L}\\w]+)"))
    new Pipeline().setStages(Array(documentAssembler, tokenizer)).fit(textDf).transform(textDf).select("tokens.result").show(false)

    This will yield: l', une, d', un, l', un, , , des, l', extrême, des, l', extreme

  58. def initializeLogIfNecessary(isInterpreter: Boolean, silent: Boolean): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  59. def initializeLogIfNecessary(isInterpreter: Boolean): Unit
    Attributes
    protected
    Definition Classes
    Logging
  60. val inputAnnotatorTypes: Array[AnnotatorType]

    Input annotator type : DOCUMENT

    Input annotator type : DOCUMENT

    Definition Classes
    TokenizerHasInputAnnotationCols
  61. final val inputCols: StringArrayParam

    columns that contain annotations necessary to run this annotator AnnotatorType is used both as input and output columns if not specified

    columns that contain annotations necessary to run this annotator AnnotatorType is used both as input and output columns if not specified

    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  62. final def isDefined(param: Param[_]): Boolean
    Definition Classes
    Params
  63. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  64. final def isSet(param: Param[_]): Boolean
    Definition Classes
    Params
  65. def isTraceEnabled(): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  66. val lazyAnnotator: BooleanParam
    Definition Classes
    CanBeLazy
  67. def log: Logger
    Attributes
    protected
    Definition Classes
    Logging
  68. def logDebug(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  69. def logDebug(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  70. def logError(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  71. def logError(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  72. def logInfo(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  73. def logInfo(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  74. def logName: String
    Attributes
    protected
    Definition Classes
    Logging
  75. def logTrace(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  76. def logTrace(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  77. def logWarning(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  78. def logWarning(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  79. val maxLength: IntParam

    Set the maximum allowed length for each token

  80. val minLength: IntParam

    Set the minimum allowed length for each token

  81. def msgHelper(schema: StructType): String
    Attributes
    protected
    Definition Classes
    HasInputAnnotationCols
  82. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  83. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  84. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  85. def onTrained(model: TokenizerModel, spark: SparkSession): Unit
    Definition Classes
    AnnotatorApproach
  86. val optionalInputAnnotatorTypes: Array[String]
    Definition Classes
    HasInputAnnotationCols
  87. val outputAnnotatorType: AnnotatorType

    Output annotator type : TOKEN

    Output annotator type : TOKEN

    Definition Classes
    TokenizerHasOutputAnnotatorType
  88. final val outputCol: Param[String]
    Attributes
    protected
    Definition Classes
    HasOutputAnnotationCol
  89. lazy val params: Array[Param[_]]
    Definition Classes
    Params
  90. val prefixPattern: Param[String]

    Regex with groups and begins with \\A to match target prefix.

    Regex with groups and begins with \\A to match target prefix. Overrides contextCharacters Param

  91. def save(path: String): Unit
    Definition Classes
    MLWritable
    Annotations
    @Since( "1.6.0" ) @throws( ... )
  92. final def set(paramPair: ParamPair[_]): Tokenizer.this.type
    Attributes
    protected
    Definition Classes
    Params
  93. final def set(param: String, value: Any): Tokenizer.this.type
    Attributes
    protected
    Definition Classes
    Params
  94. final def set[T](param: Param[T], value: T): Tokenizer.this.type
    Definition Classes
    Params
  95. def setCaseSensitiveExceptions(value: Boolean): Tokenizer.this.type

    Whether to follow case sensitiveness for matching exceptions in text

  96. def setContextChars(v: Array[String]): Tokenizer.this.type

    List of 1 character string to rip off from tokens, such as parenthesis or question marks.

    List of 1 character string to rip off from tokens, such as parenthesis or question marks. Ignored if using prefix, infix or suffix patterns.

  97. final def setDefault(paramPairs: ParamPair[_]*): Tokenizer.this.type
    Attributes
    protected
    Definition Classes
    Params
  98. final def setDefault[T](param: Param[T], value: T): Tokenizer.this.type
    Attributes
    protected[org.apache.spark.ml]
    Definition Classes
    Params
  99. def setExceptions(value: Array[String]): Tokenizer.this.type

    List of tokens to not alter at all.

    List of tokens to not alter at all. Allows composite tokens like two worded tokens that the user may not want to split.

  100. def setExceptionsPath(path: String, readAs: Format = ReadAs.TEXT, options: Map[String, String] = Map("format" -> "text")): Tokenizer.this.type

    Path to txt file with list of token exceptions

  101. def setInfixPatterns(value: Array[String]): Tokenizer.this.type

    Set a list of Regex patterns that match tokens within a single target.

    Set a list of Regex patterns that match tokens within a single target. Groups identify different sub-tokens. multiple defaults

  102. final def setInputCols(value: String*): Tokenizer.this.type
    Definition Classes
    HasInputAnnotationCols
  103. def setInputCols(value: Array[String]): Tokenizer.this.type

    Overrides required annotators column if different than default

    Overrides required annotators column if different than default

    Definition Classes
    HasInputAnnotationCols
  104. def setLazyAnnotator(value: Boolean): Tokenizer.this.type
    Definition Classes
    CanBeLazy
  105. def setMaxLength(value: Int): Tokenizer.this.type

    Get the maximum allowed length for each token

  106. def setMinLength(value: Int): Tokenizer.this.type

    Set the minimum allowed length for each token

  107. final def setOutputCol(value: String): Tokenizer.this.type

    Overrides annotation column name when transforming

    Overrides annotation column name when transforming

    Definition Classes
    HasOutputAnnotationCol
  108. def setPrefixPattern(value: String): Tokenizer.this.type

    Regex to identify subtokens that come in the beginning of the token.

    Regex to identify subtokens that come in the beginning of the token. Regex has to start with \\A and must contain groups (). Each group will become a separate token within the prefix. Defaults to non-letter characters. e.g. quotes or parenthesis

  109. def setSplitChars(v: Array[String]): Tokenizer.this.type

    List of 1 character string to split tokens inside, such as hyphens.

    List of 1 character string to split tokens inside, such as hyphens. Ignored if using infix, prefix or suffix patterns.

  110. def setSplitPattern(value: String): Tokenizer.this.type

    Regex pattern to separate from the inside of tokens.

    Regex pattern to separate from the inside of tokens. Takes priority over splitChars.

  111. def setSuffixPattern(value: String): Tokenizer.this.type

    Regex to identify subtokens that are in the end of the token.

    Regex to identify subtokens that are in the end of the token. Regex has to end with \\z and must contain groups (). Each group will become a separate token within the prefix. Defaults to non-letter characters. e.g. quotes or parenthesis

  112. def setTargetPattern(value: String): Tokenizer.this.type

    Set a basic regex rule to identify token candidates in text.

  113. val splitChars: StringArrayParam

    Character list used to separate from the inside of tokens

  114. val splitPattern: Param[String]

    Pattern to separate from the inside of tokens.

    Pattern to separate from the inside of tokens. takes priority over splitChars.

    This pattern will be applied to the tokens which where extracted with the target pattern previously

    Example:

    import org.apache.spark.ml.Pipeline
    
    import com.johnsnowlabs.nlp.annotators.Tokenizer
    
    import com.johnsnowlabs.nlp.DocumentAssembler
    
    val textDf = sqlContext.sparkContext.parallelize(Array("Tokens in this-text will#be#split on hashtags-and#dashes")).toDF("text")
    
    val documentAssembler = new DocumentAssembler().setInputCol("text").setOutputCol("sentences")
    
    val tokenizer = new Tokenizer().setInputCols("sentences").setOutputCol("tokens").setSplitPattern("-|#")
    
    new Pipeline().setStages(Array(documentAssembler, tokenizer)).fit(textDf).transform(textDf).select("tokens.result").show(false)

    This will yield: Tokens, in, this, text, will, be, split, on, hashtags, and, dashes

  115. val suffixPattern: Param[String]

    Regex with groups and ends with \\z to match target suffix.

    Regex with groups and ends with \\z to match target suffix. Overrides contextCharacters Param

  116. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  117. val targetPattern: Param[String]

    Pattern to grab from text as token candidates.

    Pattern to grab from text as token candidates. (Default: "\\S+")

    Defaults to: "\\S+" which means anything not a space will be matched and considered as a token candidate, This will cause text to be split on on white spaces to yield token candidates.

    This rule will be added to the BREAK_PATTERN varaible, which is used to yield token candidates.

    import org.apache.spark.ml.Pipeline
    import com.johnsnowlabs.nlp.annotators.Tokenizer
    import com.johnsnowlabs.nlp.DocumentAssembler
    
    val textDf = sqlContext.sparkContext.parallelize(Array("I only consider lowercase characters and NOT UPPERCASED and only the numbers 0,1, to 7 as tokens but not 8 or 9")).toDF("text")
    val documentAssembler = new DocumentAssembler().setInputCol("text").setOutputCol("sentences")
    val tokenizer = new Tokenizer().setInputCols("sentences").setOutputCol("tokens").setTargetPattern("a-z-0-7")
    new Pipeline().setStages(Array(documentAssembler, tokenizer)).fit(textDf).transform(textDf).select("tokens.result").show(false)

    This will yield: only, consider, lowercase, characters, and, and, only, the, numbers, 0, 1, to, 7, as, tokens, but, not, or

  118. def toString(): String
    Definition Classes
    Identifiable → AnyRef → Any
  119. def train(dataset: Dataset[_], recursivePipeline: Option[PipelineModel]): TokenizerModel

    Clears out rules and constructs a new rule for every combination of rules provided .

    Clears out rules and constructs a new rule for every combination of rules provided . The strategy is to catch one token per regex group. User may add its own groups if needs targets to be tokenized separately from the rest

    Definition Classes
    TokenizerAnnotatorApproach
  120. final def transformSchema(schema: StructType): StructType

    requirement for pipeline transformation validation.

    requirement for pipeline transformation validation. It is called on fit()

    Definition Classes
    AnnotatorApproach → PipelineStage
  121. def transformSchema(schema: StructType, logging: Boolean): StructType
    Attributes
    protected
    Definition Classes
    PipelineStage
    Annotations
    @DeveloperApi()
  122. val uid: String
    Definition Classes
    Tokenizer → Identifiable
  123. def validate(schema: StructType): Boolean

    takes a Dataset and checks to see if all the required annotation types are present.

    takes a Dataset and checks to see if all the required annotation types are present.

    schema

    to be validated

    returns

    True if all the required types are present, else false

    Attributes
    protected
    Definition Classes
    AnnotatorApproach
  124. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  125. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  126. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  127. def write: MLWriter
    Definition Classes
    DefaultParamsWritable → MLWritable

Inherited from CanBeLazy

Inherited from DefaultParamsWritable

Inherited from MLWritable

Inherited from HasOutputAnnotatorType

Inherited from HasOutputAnnotationCol

Inherited from HasInputAnnotationCols

Inherited from Estimator[TokenizerModel]

Inherited from PipelineStage

Inherited from Logging

Inherited from Params

Inherited from Serializable

Inherited from Serializable

Inherited from Identifiable

Inherited from AnyRef

Inherited from Any

param

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

Annotator types

Required input and expected output annotator types

Members

Parameter setters

Parameter getters