Packages

class EmbeddingsFinisher extends Transformer with DefaultParamsWritable

Extracts embeddings from Annotations into a more easily usable form.

This is useful for example: WordEmbeddings, BertEmbeddings, SentenceEmbeddings and ChunkEmbeddings.

By using EmbeddingsFinisher you can easily transform your embeddings into array of floats or vectors which are compatible with Spark ML functions such as LDA, K-mean, Random Forest classifier or any other functions that require featureCol.

For more extended examples see the Examples.

Example

import spark.implicits._
import org.apache.spark.ml.Pipeline
import com.johnsnowlabs.nlp.{DocumentAssembler, EmbeddingsFinisher}
import com.johnsnowlabs.nlp.annotator.{Normalizer, StopWordsCleaner, Tokenizer, WordEmbeddingsModel}

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

val tokenizer = new Tokenizer()
  .setInputCols("document")
  .setOutputCol("token")

val normalizer = new Normalizer()
  .setInputCols("token")
  .setOutputCol("normalized")

val stopwordsCleaner = new StopWordsCleaner()
  .setInputCols("normalized")
  .setOutputCol("cleanTokens")
  .setCaseSensitive(false)

val gloveEmbeddings = WordEmbeddingsModel.pretrained()
  .setInputCols("document", "cleanTokens")
  .setOutputCol("embeddings")
  .setCaseSensitive(false)

val embeddingsFinisher = new EmbeddingsFinisher()
  .setInputCols("embeddings")
  .setOutputCols("finished_sentence_embeddings")
  .setOutputAsVector(true)
  .setCleanAnnotations(false)

val data = Seq("Spark NLP is an open-source text processing library.")
  .toDF("text")
val pipeline = new Pipeline().setStages(Array(
  documentAssembler,
  tokenizer,
  normalizer,
  stopwordsCleaner,
  gloveEmbeddings,
  embeddingsFinisher
)).fit(data)

val result = pipeline.transform(data)
val resultWithSize = result.selectExpr("explode(finished_sentence_embeddings)")
  .map { row =>
    val vector = row.getAs[org.apache.spark.ml.linalg.DenseVector](0)
    (vector.size, vector)
  }.toDF("size", "vector")

resultWithSize.show(5, 80)
+----+--------------------------------------------------------------------------------+
|size|                                                                          vector|
+----+--------------------------------------------------------------------------------+
| 100|[0.1619900017976761,0.045552998781204224,-0.03229299932718277,-0.685609996318...|
| 100|[-0.42416998744010925,1.1378999948501587,-0.5717899799346924,-0.5078899860382...|
| 100|[0.08621499687433243,-0.15772999823093414,-0.06067200005054474,0.395359992980...|
| 100|[-0.4970499873161316,0.7164199948310852,0.40119001269340515,-0.05761000141501...|
| 100|[-0.08170200139284134,0.7159299850463867,-0.20677000284194946,0.0295659992843...|
+----+--------------------------------------------------------------------------------+
See also

Finisher for finishing Strings

Linear Supertypes
DefaultParamsWritable, MLWritable, Transformer, PipelineStage, Logging, Params, Serializable, Serializable, Identifiable, AnyRef, Any
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. EmbeddingsFinisher
  2. DefaultParamsWritable
  3. MLWritable
  4. Transformer
  5. PipelineStage
  6. Logging
  7. Params
  8. Serializable
  9. Serializable
  10. Identifiable
  11. AnyRef
  12. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

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

    uid

    required uid for storing annotator to disk

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. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  6. val cleanAnnotations: BooleanParam

    Whether to remove all the existing annotation columns (Default: true)

  7. final def clear(param: Param[_]): EmbeddingsFinisher.this.type
    Definition Classes
    Params
  8. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  9. def copy(extra: ParamMap): Transformer
    Definition Classes
    EmbeddingsFinisher → Transformer → PipelineStage → Params
  10. def copyValues[T <: Params](to: T, extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  11. final def defaultCopy[T <: Params](extra: ParamMap): T
    Attributes
    protected
    Definition Classes
    Params
  12. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  14. def explainParam(param: Param[_]): String
    Definition Classes
    Params
  15. def explainParams(): String
    Definition Classes
    Params
  16. final def extractParamMap(): ParamMap
    Definition Classes
    Params
  17. final def extractParamMap(extra: ParamMap): ParamMap
    Definition Classes
    Params
  18. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  19. final def get[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  20. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  21. def getCleanAnnotations: Boolean

    Whether to remove all the existing annotation columns (Default: true)

  22. final def getDefault[T](param: Param[T]): Option[T]
    Definition Classes
    Params
  23. def getInputCols: Array[String]

    Name of EmbeddingsFinisher output cols

  24. final def getOrDefault[T](param: Param[T]): T
    Definition Classes
    Params
  25. def getOutputAsVector: Boolean

    If enabled it will output the embeddings as Vectors instead of arrays (Default: false)

  26. def getOutputCols: Array[String]

    Name of input annotation cols containing embeddings

  27. def getParam(paramName: String): Param[Any]
    Definition Classes
    Params
  28. final def hasDefault[T](param: Param[T]): Boolean
    Definition Classes
    Params
  29. def hasParam(paramName: String): Boolean
    Definition Classes
    Params
  30. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  31. def initializeLogIfNecessary(isInterpreter: Boolean, silent: Boolean): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  32. def initializeLogIfNecessary(isInterpreter: Boolean): Unit
    Attributes
    protected
    Definition Classes
    Logging
  33. val inputCols: StringArrayParam

    Name of input annotation cols containing embeddings

  34. final def isDefined(param: Param[_]): Boolean
    Definition Classes
    Params
  35. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  36. final def isSet(param: Param[_]): Boolean
    Definition Classes
    Params
  37. def isTraceEnabled(): Boolean
    Attributes
    protected
    Definition Classes
    Logging
  38. def log: Logger
    Attributes
    protected
    Definition Classes
    Logging
  39. def logDebug(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  40. def logDebug(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  41. def logError(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  42. def logError(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  43. def logInfo(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  44. def logInfo(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  45. def logName: String
    Attributes
    protected
    Definition Classes
    Logging
  46. def logTrace(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  47. def logTrace(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  48. def logWarning(msg: ⇒ String, throwable: Throwable): Unit
    Attributes
    protected
    Definition Classes
    Logging
  49. def logWarning(msg: ⇒ String): Unit
    Attributes
    protected
    Definition Classes
    Logging
  50. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  51. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  52. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  53. val outputAsVector: BooleanParam

    If enabled it will output the embeddings as Vectors instead of arrays (Default: false)

  54. val outputCols: StringArrayParam

    Name of EmbeddingsFinisher output cols

  55. lazy val params: Array[Param[_]]
    Definition Classes
    Params
  56. def save(path: String): Unit
    Definition Classes
    MLWritable
    Annotations
    @Since( "1.6.0" ) @throws( ... )
  57. final def set(paramPair: ParamPair[_]): EmbeddingsFinisher.this.type
    Attributes
    protected
    Definition Classes
    Params
  58. final def set(param: String, value: Any): EmbeddingsFinisher.this.type
    Attributes
    protected
    Definition Classes
    Params
  59. final def set[T](param: Param[T], value: T): EmbeddingsFinisher.this.type
    Definition Classes
    Params
  60. def setCleanAnnotations(value: Boolean): EmbeddingsFinisher.this.type

    Whether to remove all the existing annotation columns (Default: true)

  61. final def setDefault(paramPairs: ParamPair[_]*): EmbeddingsFinisher.this.type
    Attributes
    protected
    Definition Classes
    Params
  62. final def setDefault[T](param: Param[T], value: T): EmbeddingsFinisher.this.type
    Attributes
    protected[org.apache.spark.ml]
    Definition Classes
    Params
  63. def setInputCols(value: String*): EmbeddingsFinisher.this.type

    Name of input annotation cols containing embeddings

  64. def setInputCols(value: Array[String]): EmbeddingsFinisher.this.type

    Name of input annotation cols containing embeddings

  65. def setOutputAsVector(value: Boolean): EmbeddingsFinisher.this.type

    If enabled it will output the embeddings as Vectors instead of arrays (Default: false)

  66. def setOutputCols(value: String*): EmbeddingsFinisher.this.type

    Name of EmbeddingsFinisher output cols

  67. def setOutputCols(value: Array[String]): EmbeddingsFinisher.this.type

    Name of EmbeddingsFinisher output cols

  68. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  69. def toString(): String
    Definition Classes
    Identifiable → AnyRef → Any
  70. def transform(dataset: Dataset[_]): Dataset[Row]
    Definition Classes
    EmbeddingsFinisher → Transformer
  71. def transform(dataset: Dataset[_], paramMap: ParamMap): DataFrame
    Definition Classes
    Transformer
    Annotations
    @Since( "2.0.0" )
  72. def transform(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*): DataFrame
    Definition Classes
    Transformer
    Annotations
    @Since( "2.0.0" ) @varargs()
  73. def transformSchema(schema: StructType): StructType
    Definition Classes
    EmbeddingsFinisher → PipelineStage
  74. def transformSchema(schema: StructType, logging: Boolean): StructType
    Attributes
    protected
    Definition Classes
    PipelineStage
    Annotations
    @DeveloperApi()
  75. val uid: String
    Definition Classes
    EmbeddingsFinisher → Identifiable
  76. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  77. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  78. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  79. def write: MLWriter
    Definition Classes
    DefaultParamsWritable → MLWritable

Inherited from DefaultParamsWritable

Inherited from MLWritable

Inherited from Transformer

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

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

Members

Parameter setters

Parameter getters