首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ruby中从散列中过滤重复的子字符串

在Ruby中从散列中过滤重复的子字符串
EN

Stack Overflow用户
提问于 2017-03-08 03:22:22
回答 2查看 34关注 0票数 0

我正在编写一个Rails应用程序,用于从新闻页面获取RSS提要,对标题应用词性标记,从标题中获取名词短语以及每个短语出现的次数。我需要过滤掉属于其他名词短语的名词短语,并使用以下代码来完成此操作:

代码语言:javascript
复制
filtered_noun_phrases = sorted_noun_phrases.select{|a|
  sorted_noun_phrases.keys.any?{|b| b != a and a.index(b) } }.to_h

所以这就是:

代码语言:javascript
复制
{"troops retake main government office"=>2,
 "retake main government office"=>2, "main government office"=>2}

应该变得公正:

代码语言:javascript
复制
{"troops retake main government office"=>2}

但是,名词短语的有序散列如下:

代码语言:javascript
复制
{"troops retake main government office"=>2, "chinese students fighting racism"=>2,
 "retake main government office"=>2, "mosul retake government base"=>2,
 "toddler killer shot dead"=>2, "students fighting racism"=>2,
 "retake government base"=>2, "main government office"=>2,
 "white house tourists"=>2, "horn at french zoo"=>2, "government office"=>2,
 "cia hacking tools"=>2, "killer shot dead"=>2, "government base"=>2,
 "boko haram teen"=>2, "horn chainsawed"=>2, "fighting racism"=>2,
 "silver surfers"=>2, "house tourists"=>2, "natural causes"=>2,
 "george michael"=>2, "instagram fame"=>2, "hacking tools"=>2,
 "iraqi forces"=>2, "mosul battle"=>2, "own wedding"=>2, "french zoo"=>2,
 "haram teen"=>2, "hacked tvs"=>2, "shot dead"=>2}

取而代之的是部分过滤:

代码语言:javascript
复制
{"troops retake main government office"=>2, "chinese students fighting racism"=>2,
 "retake main government office"=>2, "mosul retake government base"=>2,
 "toddler killer shot dead"=>2, "students fighting racism"=>2,
 "retake government base"=>2, "main government office"=>2,
 "white house tourists"=>2, "horn at french zoo"=>2,
 "cia hacking tools"=>2, "killer shot dead"=>2,
 "boko haram teen"=>2}

那么,如何从实际有效的散列中过滤出重复的子字符串呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-08 03:39:52

您当前所做的是选择作为短语的子字符串的任何短语存在的所有短语。

对于“军队夺回主要政府办公室”,这是正确的,因为我们发现“夺回主要政府办公室”。

然而,对于“收回主要政府办公室”,我们仍然可以找到“主要政府办公室”,因此没有将其过滤掉。

举个例子:

代码语言:javascript
复制
 filtered_noun_phrases = sorted_noun_phrases.reject{|a| sorted_noun_phrases.keys.any?{|b| b != a and b.index(a) } }.to_h

您可以拒绝存在包含该短语的任何字符串的所有短语。

票数 0
EN

Stack Overflow用户

发布于 2017-03-08 03:36:50

代码语言:javascript
复制
filtered_noun_phrases = sorted_noun_phrases.reject{|a| sorted_noun_phrases.keys.any?{|b| b != a and b.index(a) } }.to_h

trueunlessfalse

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42656592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档