我正在寻找一种方法来解析文本,将其转换为文本中包含的Web应用程序实体的链接。为清楚起见,我们以此文本为例:
Stoner rock and stoner metal are two related sub-genres of hard rock and heavy metal respectively, both combining elements of psychedelic rock, blues-rock, traditional heavy metal and doom metal. The genre emerged during the early 1990s and was pioneered foremost by the Californian bands Kyuss and Sleep.
我想链接文字中出现的所有类型,乐队和专辑(并在我的网络应用中作为实体存在),如下所示:
<a href="/genre/1">Stoner rock</a> and stoner metal are two related sub-genres of hard rock and <a href="/genre/2">heavy metal</a> respectively, both combining elements of psychedelic rock, blues-rock, traditional <a href="/genre/2">heavy metal</a> and <a href="/genre/4">doom metal</a>. The genre emerged during the early 1990s and was pioneered foremost by the Californian bands <a href="/band/1">Kyuss</a> and <a href="/band/2">Sleep</a>.
我认为有不同的选择,例如:
我对任何替代方案都不满意,我认为应该是一种更聪明的方法来处理这个问题。你能建议一个更好的方法吗?我正在使用Ruby On Rails 3。
答案 0 :(得分:0)
伪代码:
Keywords.find_each do |keyword|
text.gsub!(keyword, link_to keyword, some_path_for(keyword))
end
根据您拥有的关键字数量,此任务可能会占用大量CPU资源。考虑首先将文本存储到DB,并让每个测试都有后台工作程序(如Resque或Delayed :: Job)。
显示原始(未链接)文本,直到它被处理完毕。我建议对未链接和链接的文本使用两个不同的表字段。