/
DocumentAssembler.scala
117 lines (89 loc) · 3.68 KB
/
DocumentAssembler.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.johnsnowlabs.nlp
import org.apache.spark.ml.Transformer
import org.apache.spark.ml.param.{Param, ParamMap}
import org.apache.spark.ml.util.{DefaultParamsReadable, DefaultParamsWritable, Identifiable}
import org.apache.spark.sql.expressions.UserDefinedFunction
import org.apache.spark.sql.functions.udf
import org.apache.spark.sql.types._
import org.apache.spark.sql.{DataFrame, Dataset, Row}
/**
* Created by saif on 06/07/17.
*/
class DocumentAssembler(override val uid: String)
extends Transformer
with DefaultParamsWritable
with HasAnnotatorType
with HasOutputAnnotationCol {
import com.johnsnowlabs.nlp.AnnotatorType._
private type DocumentationContent = Row
val inputCol: Param[String] = new Param[String](this, "inputCol", "input text column for processing")
val idCol: Param[String] = new Param[String](this, "idCol", "id column for row reference")
val metadataCol: Param[String] = new Param[String](this, "metadataCol", "metadata for document column")
setDefault(outputCol, DOCUMENT)
override val annotatorType: AnnotatorType = DOCUMENT
def setInputCol(value: String): this.type = set(inputCol, value)
def getInputCol: String = $(inputCol)
def setIdCol(value: String): this.type = set(idCol, value)
def getIdCol: String = $(idCol)
def setMetadataCol(value: String): this.type = set(metadataCol, value)
def getMetadataCol: String = $(metadataCol)
def this() = this(Identifiable.randomUID("document"))
override def copy(extra: ParamMap): Transformer = defaultCopy(extra)
private def assemble(text: String, metadata: Map[String, String]): Seq[Annotation] = {
Seq(Annotation(annotatorType, 0, text.length - 1, text, metadata))
}
private def dfAssemble: UserDefinedFunction = udf {
(text: String, id: String, metadata: Map[String, String]) =>
assemble(text, metadata ++ Map("id" -> id))
}
private def dfAssembleOnlyId: UserDefinedFunction = udf {
(text: String, id: String) =>
assemble(text, Map("id" -> id))
}
private def dfAssembleNoId: UserDefinedFunction = udf {
(text: String, metadata: Map[String, String]) =>
assemble(text, metadata)
}
private def dfAssembleNoExtras: UserDefinedFunction = udf {
(text: String) =>
assemble(text, Map.empty[String, String])
}
/** requirement for pipeline transformation validation. It is called on fit() */
override final def transformSchema(schema: StructType): StructType = {
val metadataBuilder: MetadataBuilder = new MetadataBuilder()
metadataBuilder.putString("annotatorType", annotatorType)
val outputFields = schema.fields :+
StructField(getOutputCol, ArrayType(Annotation.dataType), nullable = false, metadataBuilder.build)
StructType(outputFields)
}
override def transform(dataset: Dataset[_]): DataFrame = {
val metadataBuilder: MetadataBuilder = new MetadataBuilder()
metadataBuilder.putString("annotatorType", annotatorType)
val documentAnnotations =
if (get(idCol).isDefined && get(metadataCol).isDefined)
dfAssemble(
dataset.col(getInputCol),
dataset.col(getIdCol),
dataset.col(getMetadataCol)
)
else if (get(idCol).isDefined)
dfAssembleOnlyId(
dataset.col(getInputCol),
dataset.col(getIdCol)
)
else if (get(metadataCol).isDefined)
dfAssembleNoId(
dataset.col(getInputCol),
dataset.col(getMetadataCol)
)
else
dfAssembleNoExtras(
dataset.col(getInputCol)
)
dataset.withColumn(
getOutputCol,
documentAnnotations.as(getOutputCol, metadataBuilder.build)
)
}
}
object DocumentAssembler extends DefaultParamsReadable[DocumentAssembler]