1

I'm using com.johnsnowlabs.nlp-2.2.2 with spark-2.4.4 to process some articles. In those articles, there are some very long words I'm not interested in and which slows down the POS tagging a lot. I would to like to exclude them after the tokenization and before the POSTagging.

I tried to write the smaller code to reproduce my issues

import sc.implicits._
val documenter = new DocumentAssembler().setInputCol("text").setOutputCol("document").setIdCol("id")
val tokenizer = new Tokenizer().setInputCols(Array("document")).setOutputCol("token")
val normalizer = new Normalizer().setInputCols("token").setOutputCol("normalized").setLowercase(true)

val df = Seq("This is a very useless/ugly sentence").toDF("text")

val document = documenter.transform(df.withColumn("id", monotonically_increasing_id()))
val token = tokenizer.fit(document).transform(document)

val token_filtered = token
  .drop("token")
  .join(token
    .select(col("id"), col("token"))
    .withColumn("tmp", explode(col("token")))
    .groupBy("id")
    .agg(collect_list(col("tmp")).as("token")),
    Seq("id"))
token_filtered.select($"token").show(false)
val normal = normalizer.fit(token_filtered).transform(token_filtered)

I have this error when I transform token_filtered

+--------------------+---+--------------------+--------------------+--------------------+
|                text| id|            document|            sentence|               token|
+--------------------+---+--------------------+--------------------+--------------------+
|This is a very us...|  0|[[document, 0, 35...|[[document, 0, 35...|[[token, 0, 3, Th...|
+--------------------+---+--------------------+--------------------+--------------------+


Exception in thread "main" java.lang.IllegalArgumentException:
requirement failed: Wrong or missing inputCols annotators in NORMALIZER_4bde2f08742a.
Received inputCols: token.
Make sure such annotators exist in your pipeline, with the right output
names and that they have following annotator types: token

It works fine if I directly fit and transform token in normalizer It seems that during the explode/groupBy/collect_list, some information is lost, but the schema and data looks the same.

Any idea ?

ticapix
  • 1,534
  • 1
  • 11
  • 15

2 Answers2

1

To update the answer given by @ticapix which is correct, however, in the new release there are two features added to SentenceDetector and Tokenizer which are minLenght and maxLength:

You can simply filter out the tokens you don't wish to give through the pipeline:

val tokenizer = new Tokenizer()
      .setInputCols("document")
      .setOutputCol("token")
      .setMinLength(4)
      .setMaxLength(10)

References:

Maziyar
  • 1,913
  • 2
  • 18
  • 37
0

Answer is: not doable. (https://github.com/JohnSnowLabs/spark-nlp/issues/653)

Annotator are destroyed during the groupBy operation.

The solutions are to either:

  • implement a custom Transformer
  • use an UDF
  • pre-process the data before feeding it to the pipeline
ticapix
  • 1,534
  • 1
  • 11
  • 15