0

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

下面是我的代码片段-

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 替换。样品也是如此。

4

1 回答 1

0

我用这个库做了一个测试: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

于 2017-06-28T14:17:26.213 回答