简体   繁体   中英

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")
  • "basketball" text links to sport_path("basketball") and
  • "football" text links to 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?

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

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. 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...

create a Model and table called Substitutions with the following fields: phrase, hyperlink, phrase_length (phrase_length calculate before save)...

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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