-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARKNLP-1059] Adding aggressiveMatching parameter to DocumentSimila…
…rityRanker
- Loading branch information
Showing
7 changed files
with
274 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/scala/com/johnsnowlabs/nlp/annotators/similarity/DocumentSimilarityUtil.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.johnsnowlabs.nlp.annotators.similarity | ||
|
||
import org.apache.spark.ml.linalg.Vectors | ||
import org.apache.spark.sql.expressions.UserDefinedFunction | ||
|
||
import scala.util.hashing.MurmurHash3 | ||
|
||
object DocumentSimilarityUtil { | ||
|
||
import org.apache.spark.sql.functions._ | ||
|
||
val mh3Func: String => Int = (s: String) => MurmurHash3.stringHash(s, MurmurHash3.stringSeed) | ||
val mh3UDF: UserDefinedFunction = udf { mh3Func } | ||
|
||
val averageAggregation: UserDefinedFunction = udf((embeddings: Seq[Seq[Double]]) => { | ||
val summed = embeddings.transpose.map(_.sum) | ||
val averaged = summed.map(_ / embeddings.length) | ||
Vectors.dense(averaged.toArray) | ||
}) | ||
|
||
val firstEmbeddingAggregation: UserDefinedFunction = udf((embeddings: Seq[Seq[Double]]) => { | ||
Vectors.dense(embeddings.head.toArray) | ||
}) | ||
|
||
val maxAggregation: UserDefinedFunction = udf((embeddings: Seq[Seq[Double]]) => { | ||
Vectors.dense(embeddings.transpose.map(_.max).toArray) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package com.johnsnowlabs.util.spark | ||
|
||
import org.apache.spark.sql.expressions.UserDefinedFunction | ||
import org.apache.spark.sql.functions.udf | ||
import org.apache.spark.sql.Dataset | ||
|
||
object SparkUtil { | ||
|
||
// Helper UDF function to flatten arrays for Spark < 2.4.0 | ||
def flattenArrays: UserDefinedFunction = udf { (arrayColumn: Seq[Seq[String]]) => | ||
arrayColumn.flatten.distinct | ||
def retrieveColumnName(dataset: Dataset[_], annotatorType: String): String = { | ||
val structFields = dataset.schema.fields | ||
.filter(field => field.metadata.contains("annotatorType")) | ||
.filter(field => field.metadata.getString("annotatorType") == annotatorType) | ||
val columnNames = structFields.map(structField => structField.name) | ||
|
||
columnNames.head | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.johnsnowlabs.util | ||
|
||
import com.johnsnowlabs.nlp.AnnotatorType.{DOCUMENT, TOKEN} | ||
import com.johnsnowlabs.nlp.annotator.SentenceDetector | ||
import com.johnsnowlabs.nlp.annotators.{SparkSessionTest, Tokenizer} | ||
import com.johnsnowlabs.tags.FastTest | ||
import com.johnsnowlabs.util.spark.SparkUtil | ||
import org.apache.spark.ml.Pipeline | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
|
||
class SparkUtilTest extends AnyFlatSpec with SparkSessionTest { | ||
|
||
"SparkUtil" should "retrieve column name for Token annotator type " taggedAs FastTest in { | ||
val expectedColumn = "token" | ||
val testDataset = tokenizerPipeline.fit(emptyDataSet).transform(emptyDataSet) | ||
|
||
val actualColumn = SparkUtil.retrieveColumnName(testDataset, TOKEN) | ||
|
||
assert(expectedColumn == actualColumn) | ||
} | ||
|
||
it should "retrieve custom column name for Token annotator type " taggedAs FastTest in { | ||
val customColumnName = "my_custom_token_col" | ||
val tokenizer = new Tokenizer() | ||
.setInputCols("document") | ||
.setOutputCol(customColumnName) | ||
|
||
val pipeline = new Pipeline().setStages(Array(documentAssembler, tokenizer)) | ||
val testDataset = pipeline.fit(emptyDataSet).transform(emptyDataSet) | ||
|
||
val actualColumn = SparkUtil.retrieveColumnName(testDataset, TOKEN) | ||
|
||
assert(customColumnName == actualColumn) | ||
} | ||
|
||
} |