使用synonym()从wordnet中提取同义词 [英] Extracting synonymous terms from wordnet using synonym()

查看:702
本文介绍了使用synonym()从wordnet中提取同义词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我通过wordnet的同义词()函数提取帮助"的同义词,并获得以下信息:

Supposed I am pulling the synonyms of "help" by the function of synonyms() from wordnet and get the followings:

Str = synonyms("help")    
Str
[1] "c(\"aid\", \"assist\", \"assistance\", \"help\")"     
[2] "c(\"aid\", \"assistance\", \"help\")"                 
[3] "c(\"assistant\", \"helper\", \"help\", \"supporter\")"
[4] "c(\"avail\", \"help\", \"service\")"  

然后我可以使用

unique(unlist(lapply(parse(text=Str),eval)))

如下所示:

[1] "aid"        "assist"     "assistance" "help"       "assistant"  "helper"     "supporter" 
[8] "avail"      "service"

以上过程由Gabor Grothendieck建议.他/她的解决方案很好,但是我仍然无法弄清楚,如果我将查询词更改为"company","boy"或其他人,则会响应错误消息.

The above process was suggested by Gabor Grothendieck. His/Her solution is good, but I still couldn't figure out that if I change the query term into "company", "boy", or someone else, an error message will be responsed.

一个可能的原因可能是由于公司"的第六"同义词(请参见下文)是一个单独的术语,而不遵循"c(\"company \")的格式.

One possible reason maybe due to the "sixth" synonym of "company" (please see below) is a single term and does not follow the format of "c(\"company\")".

synonyms("company")

[1] "c(\"caller\", \"company\")"                                    
[2] "c(\"company\", \"companionship\", \"fellowship\", \"society\")"
[3] "c(\"company\", \"troupe\")"                                    
[4] "c(\"party\", \"company\")"                                     
[5] "c(\"ship's company\", \"company\")"                            
[6] "company"

有人可以帮助我解决这个问题吗? 非常感谢.

Could someone kindly help me to solve this problem. Many thanks.

推荐答案

您可以通过创建一个使用R的try机制捕获错误的辅助函数来解决此问题.在这种情况下,如果eval产生错误,则返回原始字符串,否则返回eval的结果:

You can solve this by creating a little helper function that uses R's try mechanism to catch errors. In this case, if the eval produces an error, then return the original string, else return the result of eval:

创建一个辅助函数:

evalOrValue <- function(expr, ...){
  z <- try(eval(expr, ...), TRUE)
  if(inherits(z, "try-error")) as.character(expr) else unlist(z)
}

unique(unlist(sapply(parse(text=Str), evalOrValue)))

产生:

[1] "caller"         "company"        "companionship" 
[4] "fellowship"     "society"        "troupe"        
[7] "party"          "ship's company"


我复制了您的数据,然后使用dput在此处复制了数据:


I reproduced your data and then used dput to reproduce it here:

Str <- c("c(\"caller\", \"company\")", "c(\"company\", \"companionship\", \"fellowship\", \"society\")", 
"c(\"company\", \"troupe\")", "c(\"party\", \"company\")", "c(\"ship's company\", \"company\")", 
"company")

这篇关于使用synonym()从wordnet中提取同义词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆