Ruby/Rails-在渲染时从单词/短语生成超链接



我已经搜索了很多答案,但我一直在寻找关于如何将纯文本链接变成可点击的超链接或从文本中删除超链接的文章。这两者都不是。

我希望能够在运行时解析出单词/短语&基于一些后端逻辑/数据从它们创建超链接。例如,用户配置文件可能有一个"关于我"部分,如下所示:

I went to xyz university and like basketball & football.

我想有一些功能,可以创建超链接,其中:

  • "xyz university"指向school_path的文本链接("xyz大学")
  • "basketball"文本链接到sport_path("basket")和
  • "football"文本链接到sport_path("足球")

用户可以随时根据不同的运动、音乐等更改她的个人资料,我希望能够对此做出解释。如果我指定链接的单词/短语列表中不存在该单词或短语,则不会发生任何事情。

有没有一个我应该在谷歌上搜索的术语,一些隐藏的Ruby类可以做到这一点,或者有一个我找不到的宝石?

我感谢你能提供的任何帮助!

Kyle

我认为首先你要谈论机器学习的主题,特别是关键字匹配。

http://www.quora.com/What-are-good-tools-to-extract-key-words-and-or-topics-tags-from-a-random-paragraph-of-text

我不确定最好的方法,但我的出发点可能是进行某种类型的postgres关键字搜索,该搜索具有大量索引,并包含一个为您提供路线的属性。你必须建立某种类型的关键字,值查找,你可能必须自己开始建立字典,因为我不确定你会如何获得基于单词的主观信息。

8一种方法。。。其他人可能会提出一些改进。。。

创建一个名为Substitutions的模型和表,其中包含以下字段:短语、超链接、短语长度(保存前计算短语长度)。。。

# look for substitions in descending phrase length order
# so that "Columbus University" is substituted instead of "Columbus" (the city)
# replace matched phrase with a temporary eyecatcher storing substitution id
# we do this so that other matches of smaller strings will disregard 
# already matched text
Substitution.order("phrase_length DESC").each do |subst| 
  paragraph.sub!( subst.phrase, "{subbing#{subst.id.to_s.rjust(8, '0')}}" )
end
# now replace eyecatchers with hyperlink string
pointer = paragraph.index '{subbing' 
while pointer 
  subst = Substitution.find(paragraph[pointer+9, 8].to_i)
  paragraph.sub!( "{subbing#{subst.id.to_s.rjust(8, '0')}}", subst.hyperlink )
  pointer = paragraph.index '{subbing' # continue while eyecatchers still present
end

最新更新