简体   繁体   English

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

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

I've searched quite a bit for the answer, but I keep finding articles about how to turn plain text links into clickable hyperlinks or strip hyperlinks out of text. 我已经搜索了相当多的答案,但我一直在寻找有关如何将纯文本链接转换为可点击的超链接或从文本中删除超链接的文章。 This is about neither. 这两者都不是。

I'd like to be able to parse out words/phrases at runtime & create hyperlinks from them based on some backend logic/data. 我希望能够在运行时解析单词/短语,并根据一些后端逻辑/数据从它们创建超链接。 For example, a user profile might have an "About me" section as follows: 例如,用户个人资料可能包含“关于我”部分,如下所示:

I went to xyz university and like basketball & football.

I'd like to have some functionality that would create hyperlinks where: 我想要一些可以创建超链接的功能:

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

The user could change her/his profile with different sports, music, etc at any time, and I'd like to be able to account for that. 用户可以随时通过不同的体育,音乐等改变他/她的个人资料,我希望能够解释这一点。 And if the word or phrase doesn't exist in the list of words/phrases I specify links for, then nothing happens. 如果单词/短语列表中不存在单词或短语我指定链接,则没有任何反应。

Is there a certain term I should be Googling, some hidden Ruby class that does this, or a gem out there I'm not finding? 是否有一个术语我应该谷歌搜索,一些隐藏的Ruby类,这样做,或在那里我找不到的宝石?

I appreciate any help you can provide! 我感谢您提供的任何帮助!

Kyle 凯尔

I think first off your talking about the subject of machine learning, specifically keyword matching. 我想首先谈谈机器学习的主题,特别是关键词匹配。

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

I'm not sure of the best approach, but my starting point might be to do some type of postgres keyword searching that is heavily indexed and contains an attribute that gives you the route. 我不确定最好的方法,但我的出发点可能是做一些严格索引的postgres关键字搜索,并包含一个给你路由的属性。 You'll have to build some type of key, value lookup, and you might have to start building the dictionary yourself as I'm not sure how you'd go about getting subjective information based on a word. 您将不得不构建某种类型的键,值查找,并且您可能必须自己开始构建字典,因为我不确定如何基于单词获取主观信息。

8One approach... others may suggest some improvements... 8一种方法......其他人可能会提出一些改进......

create a Model and table called Substitutions with the following fields: phrase, hyperlink, phrase_length (phrase_length calculate before save)... 创建一个名为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