根据给定的相似度百分比将批量文本分组

Grouping bulk text as into group based on the given similarity percentage

我浏览了 GitHub NLP 中可用的以下 NLP gem,但无法找到正确的解决方案。

是否有任何 gem 或库可用于根据给定的相似百分比对文本进行分组。以上所有 gem 都有助于找到两个字符串之间的相似性,但对大量数据进行分组需要花费大量时间。

您可以只使用 Ruby 加上列出的 gem 之一。

我选择fuzzy-string-match是因为我喜欢这个名字

gem 的使用方法如下:

require 'fuzzystringmatch'

# Create the matcher
jarow = FuzzyStringMatch::JaroWinkler.create( :native )

# Get the distance
jarow.getDistance(  "jones",      "johnson" )
# => 0.8323809523809523

# Round it
jarow.getDistance(  "jones",      "johnson" ).round(2)
# => 0.83

由于您获得的是浮点数,因此您可以使用 round 方法定义您要寻找的精度。

现在,要对相似的结果进行分组,您可以使用 Enumerable 模块中的 group_by 方法。

您将一个块传递给它,group_by 将遍历该集合。对于每次迭代,您 return 您要为其分组的值(在本例中为距离),它将 return 一个以距离作为键和匹配在一起的字符串数组的散列作为值。

require 'fuzzystringmatch'

jarow = FuzzyStringMatch::JaroWinkler.create( :native )

target = "jones"
precision = 2
candidates = [ "Jessica Jones", "Jones", "Johnson", "thompson", "john", "thompsen" ]

distances = candidates.group_by { |candidate|
  jarow.getDistance( target, candidate ).round(precision)
}

distances
# => {0.52=>["Jessica Jones"],
#     0.87=>["Jones"],
#     0.68=>["Johnson"],
#     0.55=>["thompson", "thompsen"],
#     0.83=>["john"]}

希望对您有所帮助