sparknlp.base.Finisher

class sparknlp.base.Finisher[source]

Bases: sparknlp.internal.AnnotatorTransformer

Converts annotation results into a format that easier to use.

It is useful to extract the results from Spark NLP Pipelines. The Finisher outputs annotation(s) values into String.

For more extended examples on document pre-processing see the Spark NLP Workshop.

Input Annotation types

Output Annotation type

ANY

NONE

Parameters
inputCols

Input annotations

outputCols

Output finished annotation cols

valueSplitSymbol

Character separating values, by default #

annotationSplitSymbol

Character separating annotations, by default @

cleanAnnotations

Whether to remove annotation columns, by default True

includeMetadata

Whether to include annotation metadata, by default False

outputAsArray

Finisher generates an Array with the results instead of string, by default True

parseEmbeddingsVectors

Whether to include embeddings vectors in the process, by default False

Examples

>>> import sparknlp
>>> from sparknlp.base import *
>>> from sparknlp.annotator import *
>>> from sparknlp.pretrained import PretrainedPipeline
>>> data = spark.createDataFrame([[1, "New York and New Jersey aren't that far apart actually."]]).toDF("id", "text")

Define pretrained pipeline that extracts Named Entities amongst other things and apply the Finisher on it.

>>> pipeline = PretrainedPipeline("explain_document_dl")
>>> finisher = Finisher().setInputCols("entities").setOutputCols("output")
>>> explainResult = pipeline.transform(data)

Show results.

>>> explainResult.selectExpr("explode(entities)").show(truncate=False)
+------------------------------------------------------------------------------------------------------------------------------------------------------+
|entities                                                                                                                                              |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
|[[chunk, 0, 7, New York, [entity -> LOC, sentence -> 0, chunk -> 0], []], [chunk, 13, 22, New Jersey, [entity -> LOC, sentence -> 0, chunk -> 1], []]]|
+------------------------------------------------------------------------------------------------------------------------------------------------------+
>>> result = finisher.transform(explainResult)
>>> result.select("output").show(truncate=False)
+----------------------+
|output                |
+----------------------+
|[New York, New Jersey]|
+----------------------+

Methods

__init__()

clear(param)

Clears a param from the param map if it has been explicitly set.

copy([extra])

Creates a copy of this instance with the same uid and some extra params.

explainParam(param)

Explains a single param and returns its name, doc, and optional default value and user-supplied value in a string.

explainParams()

Returns the documentation of all params with their optionally default values and user-supplied values.

extractParamMap([extra])

Extracts the embedded default param values and user-supplied values, and then merges them with extra values from input into a flat param map, where the latter value is used if there exist conflicts, i.e., with ordering: default param values < user-supplied values < extra.

getOrDefault(param)

Gets the value of a param in the user-supplied param map or its default value.

getParam(paramName)

Gets a param by its name.

getParamValue(paramName)

Gets the value of a parameter.

hasDefault(param)

Checks whether a param has a default value.

hasParam(paramName)

Tests whether this instance contains a param with a given (string) name.

isDefined(param)

Checks whether a param is explicitly set by user or has a default value.

isSet(param)

Checks whether a param is explicitly set by user.

load(path)

Reads an ML instance from the input path, a shortcut of read().load(path).

read()

Returns an MLReader instance for this class.

save(path)

Save this ML instance to the given path, a shortcut of 'write().save(path)'.

set(param, value)

Sets a parameter in the embedded param map.

setAnnotationSplitSymbol(value)

Sets character separating annotations, by default @.

setCleanAnnotations(value)

Sets whether to remove annotation columns, by default True.

setIncludeMetadata(value)

Sets whether to include annotation metadata.

setInputCols(*value)

Sets column names of input annotations.

setOutputAsArray(value)

Sets whether to generate an array with the results instead of a string.

setOutputCols(*value)

Sets column names of finished output annotations.

setParamValue(paramName)

Sets the value of a parameter.

setParams()

setParseEmbeddingsVectors(value)

Sets whether to include embeddings vectors in the process.

setValueSplitSymbol(value)

Sets character separating values, by default #.

transform(dataset[, params])

Transforms the input dataset with optional parameters.

write()

Returns an MLWriter instance for this ML instance.

Attributes

annotationSplitSymbol

cleanAnnotations

getter_attrs

includeMetadata

inputCols

name

outputAsArray

outputCols

params

Returns all params ordered by name.

parseEmbeddingsVectors

valueSplitSymbol

clear(param)

Clears a param from the param map if it has been explicitly set.

copy(extra=None)

Creates a copy of this instance with the same uid and some extra params. This implementation first calls Params.copy and then make a copy of the companion Java pipeline component with extra params. So both the Python wrapper and the Java pipeline component get copied.

Parameters

extra – Extra parameters to copy to the new instance

Returns

Copy of this instance

explainParam(param)

Explains a single param and returns its name, doc, and optional default value and user-supplied value in a string.

explainParams()

Returns the documentation of all params with their optionally default values and user-supplied values.

extractParamMap(extra=None)

Extracts the embedded default param values and user-supplied values, and then merges them with extra values from input into a flat param map, where the latter value is used if there exist conflicts, i.e., with ordering: default param values < user-supplied values < extra.

Parameters

extra – extra param values

Returns

merged param map

getOrDefault(param)

Gets the value of a param in the user-supplied param map or its default value. Raises an error if neither is set.

getParam(paramName)

Gets a param by its name.

getParamValue(paramName)

Gets the value of a parameter.

Parameters
paramNamestr

Name of the parameter

hasDefault(param)

Checks whether a param has a default value.

hasParam(paramName)

Tests whether this instance contains a param with a given (string) name.

isDefined(param)

Checks whether a param is explicitly set by user or has a default value.

isSet(param)

Checks whether a param is explicitly set by user.

classmethod load(path)

Reads an ML instance from the input path, a shortcut of read().load(path).

property params

Returns all params ordered by name. The default implementation uses dir() to get all attributes of type Param.

classmethod read()

Returns an MLReader instance for this class.

save(path)

Save this ML instance to the given path, a shortcut of ‘write().save(path)’.

set(param, value)

Sets a parameter in the embedded param map.

setAnnotationSplitSymbol(value)[source]

Sets character separating annotations, by default @.

Parameters
valuestr

setCleanAnnotations(value)[source]

Sets whether to remove annotation columns, by default True.

Parameters
valuebool

Whether to remove annotation columns

setIncludeMetadata(value)[source]

Sets whether to include annotation metadata.

Parameters
valuebool

Whether to include annotation metadata

setInputCols(*value)[source]

Sets column names of input annotations.

Parameters
*valuestr

Input columns for the annotator

setOutputAsArray(value)[source]

Sets whether to generate an array with the results instead of a string.

Parameters
valuebool

Whether to generate an array with the results instead of a string

setOutputCols(*value)[source]

Sets column names of finished output annotations.

Parameters
*valueList[str]

List of output columns

setParamValue(paramName)

Sets the value of a parameter.

Parameters
paramNamestr

Name of the parameter

setParseEmbeddingsVectors(value)[source]

Sets whether to include embeddings vectors in the process.

Parameters
valuebool

Whether to include embeddings vectors in the process

setValueSplitSymbol(value)[source]

Sets character separating values, by default #.

Parameters
valuestr

Character to separate annotations

transform(dataset, params=None)

Transforms the input dataset with optional parameters.

Parameters
  • dataset – input dataset, which is an instance of pyspark.sql.DataFrame

  • params – an optional param map that overrides embedded params.

Returns

transformed dataset

New in version 1.3.0.

uid

A unique id for the object.

write()

Returns an MLWriter instance for this ML instance.