-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordCounts.scala
48 lines (24 loc) · 920 Bytes
/
wordCounts.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
// Databricks notebook source
import com.johnsnowlabs.nlp.base._
import com.johnsnowlabs.nlp.annotator._
// COMMAND ----------
import com.johnsnowlabs.nlp.pretrained.PretrainedPipeline
// COMMAND ----------
val pipeline = PretrainedPipeline("recognize_entities_dl", lang="en")
// COMMAND ----------
val dracula = sc.textFile("/FileStore/tables/dracula.txt")
// COMMAND ----------
val result = pipeline.annotate(dracula.collect())
// COMMAND ----------
val data = dracula.toDF("text")
// COMMAND ----------
val resultDF = pipeline.annotate(data, "text")
// COMMAND ----------
import org.apache.spark.sql.functions._
val sortedNamedE = resultDF.groupBy(col("entities.result")(0) as "entity").count().sort(desc("count"))
// COMMAND ----------
sortedNamedE.show()
// COMMAND ----------
val resultMap = sortedNamedE.rdd.map(row => (row(0) -> row.getLong(1)))
// COMMAND ----------
resultMap.collect()