Find and replace with values from array with gsub

Discussion in 'Ruby' started by Allan, Mar 3, 2010.

  1. Allan

    Allan Guest

    Hello,

    I may be over thinking this situation, so I need another person to
    give me a hint. I have an app that has a database of baseball players.
    I'm trying to create a feature where you can copy and paste your blog
    text, the feature will scan the text and return the text with the
    proper html links of my app that point to the player. For example, I'm
    a blog writer, I create a entry with one sentence "I am a big fan of
    Derek Jeter and Jorge Posada of the Yankees" and I click submit. I'm
    basically trying to create this: http://www.baseball-reference.com/friv/link_players.cgi

    #This code is taking the sentence and scanning it for two words that
    are next to each other that have the first letter capitalized
    @full_text = params[:q]
    @full_text_array = @full_text.scan(/([A-Z]+[a-zA-Z]* [A-Z]+[a-zA-
    Z]*)/)

    I then run each name in the array through my ferret search and it will
    return proper html link to the player in my DB and insert that into a
    new array (@new_link_array)
    link = link_to "#{master.nameFirst} #{master.nameLast}",
    :controller => :masters,
    :action => :show,
    :playerID => "#{master.playerID}",
    :nameFirst => "#{master.nameFirst}".gsub(".",""),
    :nameLast => "#{master.nameLast}".gsub(" ", "").gsub("'", "")

    Now this is where I'm stuck. I don't understand how to take the
    original sentence and replace "Derek Jeter" and "Jorge Posada" with
    the linked name.

    I've done this:
    <%= @full_text.gsub(@full_text_array[0].to_s,
    @new_link_array[0]).gsub(@full_text_array[1].to_s, @new_link_array[1])
    %>

    I run into a few problems here and I'd appreciate a better way to
    write and scale this for multiple names (hundreds maybe).
     
    Allan, Mar 3, 2010
    #1
    1. Advertising

  2. Allan

    Raul Jara Guest

    @full_text_array.each_with_index do |original_name, i|
    @full_text.gsub!(original_name, @new_link_array)
    end


    Although if you have "Derek Jeter Derek Jeter" fifty times in a row
    you'll end up doing a lot of extra work. You might want to do something
    like:

    results = @full_text.scan(/([A-Z]+[a-zA-Z]* [A-Z]+[a-zA-Z]*)/)

    names_links = {}

    results.each do |name|
    unless names_links[name]
    names_links[name] = method_to_construct_link_for_name(name)
    end
    end

    results.each_pair |name, link|
    @full_text.gsub!(name, link)
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Raul Jara, Mar 3, 2010
    #2
    1. Advertising

  3. Allan

    Raul Jara Guest

    Raul Jara wrote:
    > @full_text_array.each_with_index do |original_name, i|
    > @full_text.gsub!(original_name, @new_link_array)
    > end
    >
    >
    > Although if you have "Derek Jeter Derek Jeter" fifty times in a row
    > you'll end up doing a lot of extra work. You might want to do something
    > like:
    >
    > results = @full_text.scan(/([A-Z]+[a-zA-Z]* [A-Z]+[a-zA-Z]*)/)
    >
    > names_links = {}
    >
    > results.each do |name|
    > unless names_links[name]
    > names_links[name] = method_to_construct_link_for_name(name)
    > end
    > end
    >
    > results.each_pair do |name, link|
    > @full_text.gsub!(name, link)
    > end



    Stupid hitting submit before I meant to. The first post contains at
    least one typo, and also I meant to add that I hope that helps.

    -Raúl
    --
    Posted via http://www.ruby-forum.com/.
     
    Raul Jara, Mar 3, 2010
    #3
  4. Allan

    Raul Jara Guest

    Gah! The last little bit should be:

    names_links.each_pair do |name, link|
    @full_text.gsub!(name, link)
    end

    results is an array, not a hash, so it wouldn't make much sense to run
    each pair on it.

    Sorry about that. I really wish there were a way to edit posts.
    --
    Posted via http://www.ruby-forum.com/.
     
    Raul Jara, Mar 3, 2010
    #4
  5. Allan

    Allan Guest

    Thanks for your help. I'm still seeing a small problem:

    My original input is: "I am a big fan of Derek Jeter and Jorge Posada
    of the Yankees"


    Output is: I am a big fan of Derek Jeter and Jorge Posada of the
    Yankees I am a big fan of Jorge Posada and Jorge Posada of the
    Yankees
    How do I fix that last loop to provide me with the correct return?

    Thanks in advance. You've been a lot of help!

    -A


    On Mar 2, 6:54 pm, Raul Jara <> wrote:
    > Gah!  The last little bit should be:
    >
    > names_links.each_pair do |name, link|
    >    @full_text.gsub!(name, link)
    > end
    >
    > results is an array, not a hash, so it wouldn't make much sense to run
    > each pair on it.
    >
    > Sorry about that.  I really wish there were a way to edit posts.
    > --
    > Posted viahttp://www.ruby-forum.com/.
     
    Allan, Mar 3, 2010
    #5
  6. Allan

    Allan Guest

    I'm trying to debug this right now, but I'm hoping that I could use a
    lending hand:

    Input1: I am a big fan of Derek Jeter and Jorge Posada of the Yankees
    Output1: I am a big fan of Derek Jeter and Jorge Posada of the Yankees
    I am a big fan of Jorge Posada and Jorge Posada of the Yankees (all
    names have the proper links)

    I then switch the names around

    Input2: I am a big fan of Jorge Posada and Derek Jeter of the Yankees
    Output2: I am a big fan of Derek Jeter and Derek Jeter of the Yankees
    I am a big fan of Derek Jeter and Derek Jeter of the Yankees

    -A

    On Mar 2, 7:20 pm, Allan <> wrote:
    > Thanks for your help. I'm still seeing a small problem:
    >
    > My original input is: "I am a big fan of Derek Jeter and Jorge Posada
    > of the Yankees"
    >
    > Output is: I am a big fan of Derek Jeter and Jorge Posada of the
    > Yankees I am a big fan of Jorge Posada and Jorge Posada of the
    > Yankees
    > How do I fix that last loop to provide me with the correct return?
    >
    > Thanks in advance.  You've been a lot of help!
    >
    > -A
    >
    > On Mar 2, 6:54 pm, Raul Jara <> wrote:
    >
    > > Gah!  The last little bit should be:

    >
    > > names_links.each_pair do |name, link|
    > >    @full_text.gsub!(name, link)
    > > end

    >
    > > results is an array, not a hash, so it wouldn't make much sense to run
    > > each pair on it.

    >
    > > Sorry about that.  I really wish there were a way to edit posts.
    > > --
    > > Posted viahttp://www.ruby-forum.com/.

    >
    >
     
    Allan, Mar 3, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Gundlach

    gsub!, replace with \'

    Patrick Gundlach, May 30, 2004, in forum: Ruby
    Replies:
    13
    Views:
    242
    Simon Strandgaard
    May 31, 2004
  2. gsub (replace path)

    , Dec 29, 2004, in forum: Ruby
    Replies:
    1
    Views:
    102
    Robert Klemme
    Dec 29, 2004
  3. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    164
    Robert Klemme
    Nov 9, 2005
  4. Ahmad Azizan
    Replies:
    5
    Views:
    152
    Ahmad Azizan
    Nov 17, 2009
  5. Ryan Smith
    Replies:
    11
    Views:
    245
    Ryan Smith
    Jan 29, 2010
Loading...

Share This Page