繁体   English   中英

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

[英]Ruby / Rails - Generate hyperlinks from words/phrases at render time?

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

我希望能够在运行时解析单词/短语,并根据一些后端逻辑/数据从它们创建超链接。 例如,用户个人资料可能包含“关于我”部分,如下所示:

I went to xyz university and like basketball & football.

我想要一些可以创建超链接的功能:

  • “xyz university”文本链接到school_path(“xyz university”)
  • “basketball”文本链接到sport_path(“篮球”)和
  • “football”的文字链接到sport_path(“football”)

用户可以随时通过不同的体育,音乐等改变他/她的个人资料,我希望能够解释这一点。 如果单词/短语列表中不存在单词或短语我指定链接,则没有任何反应。

是否有一个术语我应该谷歌搜索,一些隐藏的Ruby类,这样做,或在那里我找不到的宝石?

我感谢您提供的任何帮助!

凯尔

我想首先谈谈机器学习的主题,特别是关键词匹配。

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的Model和表,其中包含以下字段:phrase,hyperlink,phrase_length(在保存之前计算phrase_length)...

# 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM