开发者社区> 问答> 正文

如何使用JohnSnowLabs NLP拼写纠错模块NorvigSweetingModel?

我经历了JohnSnowLabs SpellChecker 。

我在那里找到了Norvig算法实现,示例部分只有以下两行:

import com.johnsnowlabs.nlp.annotator.NorvigSweetingModel
NorvigSweetingModel.pretrained()
谁能帮我在我的数据帧(df)下面应用这个预训练模型的拼写纠正“ names”列。

namesagecolor
[abc, cde]19red, abc
[eefg, efa, efb]192efg, efz efz

我试过这样做:

val schk = NorvigSweetingModel.pretrained().setInputCols("names").setOutputCol("Corrected")

val cdf = schk.transform(df)
但上面的代码给了我以下错误:

java.lang.IllegalArgumentException: requirement failed: Wrong or missing inputCols annotators in SPELL_a1f11bacb851. Received inputCols: names. Make sure such columns have following annotator types: token
at scala.Predef$.require(Predef.scala:224)
at com.johnsnowlabs.nlp.AnnotatorModel.transform(AnnotatorModel.scala:51)
... 49 elided

展开
收起
社区小助手 2018-12-12 11:04:29 2174 0
1 条回答
写回答
取消 提交回答
  • 社区小助手是spark中国社区的管理员,我会定期更新直播回顾等资料和文章干货,还整合了大家在钉群提出的有关spark的问题及回答。

    spark-nlp 设计用于其自己的特定管道,并且不同变换器的输入列必须包含特殊元数据。

    该异常已经告诉您NorvigSweetingModel应该对该输入进行标记化:

    确保此类列具有以下注释器类型:token

    如果我没有弄错的话,至少你会收集文件并在这里​​进行标记。

    import com.johnsnowlabs.nlp.DocumentAssembler
    import com.johnsnowlabs.nlp.annotator.NorvigSweetingModel
    import com.johnsnowlabs.nlp.annotators.Tokenizer
    import org.apache.spark.ml.Pipeline

    val df = Seq(Seq("abc", "cde"), Seq("eefg", "efa", "efb")).toDF("names")

    val nlpPipeline = new Pipeline().setStages(Array(
    new DocumentAssembler().setInputCol("names").setOutputCol("document"),
    new Tokenizer().setInputCols("document").setOutputCol("tokens"),
    NorvigSweetingModel.pretrained().setInputCols("tokens").setOutputCol("corrected")
    ))
    Pipeline像这样,可以在小的调整下应用到你的数据-输入数据必须是string不是array*:

    val result = df
    .transform(_.withColumn("names", concat_ws(" ", $"names")))
    .transform(df => nlpPipeline.fit(df).transform(df))

    result.show()
    names document tokens corrected
    abc cde [[document, 0, 6,... [[token, 0, 2, ab... [[token, 0, 2, ab...
    eefg efa efb [[document, 0, 11... [[token, 0, 3, ee... [[token, 0, 3, ee...

    如果你想要一个可以导出的输出你应该扩展你Pipeline的Finisher。

    import com.johnsnowlabs.nlp.Finisher

    new Finisher().setInputCols("corrected").transform(result).show
    +------------+------------------+
    | names|finished_corrected|
    +------------+------------------+
    | abc cde| [abc, cde]|
    |eefg efa efb| [eefg, efa, efb]|
    +------------+------------------+
    *根据文件 DocumentAssembler

    可以读取String列或Array [String]

    但它看起来不像1.7.3中的实际工作:

    df.transform(df => nlpPipeline.fit(df).transform(df)).show()
    org.apache.spark.sql.AnalysisException: cannot resolve 'UDF(names)' due to data type mismatch: argument 1 requires string type, however, 'names' is of array type.;;
    'Project [names#62, UDF(names#62) AS document#343]
    +- AnalysisBarrier

      +- Project [value#60 AS names#62]
         +- LocalRelation [value#60]
    2019-07-17 23:20:01
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
自然语言处理得十个发展趋势 立即下载
自然语言处理的十个发展趋势 立即下载
深度学习与自然语言处理 立即下载