【问题标题】:Fuzzy Compare between two hive columns using apache spark with scala使用带有scala的apache spark在两个hive列​​之间进行模糊比较
【发布时间】:2017-06-28 10:25:39
【问题描述】:

我正在从 2 个配置单元表中读取数据。令牌表中有需要与输入数据匹配的令牌。输入数据将具有描述列以及其他列。我需要拆分输入数据,并且需要将每个拆分的元素与令牌表中的所有元素进行比较。 目前我正在使用 me.xdrop.fuzzywuzzy.FuzzySearch 库进行模糊匹配。

下面是我的代码 sn-p-

val tokens = sqlContext.sql("select token from tokens")
val desc = sqlContext.sql("select description from desceriptiontable")
val desc_tokens = desc.flatMap(_.toString().split(" "))

现在我需要迭代 desc_tokens 并且 desc_tokens 的每个元素都应该与令牌的每个元素进行模糊匹配,并且它超过 85% 的匹配我需要用令牌中的元素替换 desc_tokens 中的元素。

例子——

我的令牌列表是

hello
this
is
token
file
sample

我的输入描述是

helo this is input desc sampl

代码应该返回

hello this is input desc sample 

因为 hellohelo 模糊匹配 > 85%,因此 helo 将被 hello 替换。样品也是如此。

【问题讨论】:

    标签: scala apache-spark fuzzy fuzzywuzzy


    【解决方案1】:

    我用这个库做一个测试:https://github.com/rockymadden/stringmetric

    其他想法(未优化):

    //I change order tokens
    val tokens = Array("this","is","sample","token","file","hello");
    val desc_tokens = Array("helo","this","is","token","file","sampl");
    
    val res = desc_tokens.map(str => {
      //Compute score beetween tokens and desc_tokens
      val elem = tokens.zipWithIndex.map{ case(tok,index) => (tok,index,JaroMetric.compare(str, tok).get)}
      //Get token has max score
      val emax = elem.maxBy{case(_,_,score) => score}
      //if emax have a score > 0.85 get It. Else keep input
      if(emax._3 > 0.85) tokens(emax._2) else str
    
    })
    res.foreach { println }
    

    我的输出: hello this is token file sample

    【讨论】:

    • 感谢@Jeremy 的回复。 zipWithIndex 将明智地遍历索引。因此,如果 hello 令牌出现在索引 2 或 3 中,则此代码将不起作用。我正在寻找的是输入描述中的每个标记都应该从标记列表中查找所有标记,并从标记列表中返回最佳匹配或第一次匹配的标记(>85%)
    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2017-10-24
    • 2021-07-05
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多