1

I want to make app which will switch vocabulary in desired url of webpage Japanese to English. But firstable I want start form just simply display desired url of webpage inline lust like Google Translate.(See here)

I got html data from desired url using code below, and now I want to replace text in html all at same time based data on database.

def submit
        require 'open-uri'

        charset = nil
        @html = open(params[:url]) do |f|
        charset = f.charset
        f.read
        end
    end

Database is undone, but I am going to contain Japanese vocabulary which should be switched, and English vocabulary which should be switched instead of Japanese vocabulary.

Any ideas or ways to do this? Also, I just started learning Ruby on Rails recently so it would be nice if you explain it with some examples or detailed explanation :)

I just want to replace particular word in text based on item on database,I don't want to multilingualism.

EDIT:

For example i got following html below from desired webpage.

 <html>
   <head>
   </head>
   <body>
     <p>I want to switch "aaa" this and "ccc"</p>
   </body>
 </html>

Lets say I want to switch(Replace) "aaa" to "bbb", "ccc" to "ddd". Word that should be switched and be switched instead of previous word are in database.(Target:"aaa","ccc" Switch:"bbb","ddd")

since this html is the one i got it using open-uri, i can't implement code like #{target}.

LEoREo_2247
  • 100
  • 9
  • Possible duplicate of [Best practice in making Rails applications multilingual](http://stackoverflow.com/questions/21948854/best-practice-in-making-rails-applications-multilingual) – Ken Y-N Jun 09 '16 at 23:51
  • 1
    No. I just want to replace particular word on text based on database, i don't want to multilingual my app. – LEoREo_2247 Jun 10 '16 at 00:06
  • Well then, possible dup of https://stackoverflow.com/questions/554666/ruby-merging-variables-in-to-a-string? – Ken Y-N Jun 10 '16 at 00:26

1 Answers1

1

Working based on the code in this answer and this answer, you could do something like this:

replacements = {'aaa' => 'ccc', 'bbb' => 'ddd' }
regex = Regexp.new(replacements.keys.map { |x| Regexp.escape(x) }.join('|'))
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.traverse do |x|
  if x.text?
    x.content = x.content.gsub(regex, replacements)
  end
end

I've also tested that:

replacements = {'こんにちは' => 'Good day', 'bbb' => 'ddd' }
regex = Regexp.new(replacements.keys.map { |x| Regexp.escape(x) }.join('|'))
"こんにちは Mr bbb".gsub(regex, replacements)

Gives the expected:

Good day Mr ddd

You might also want to use:

regex = Regexp.new(replacements.keys.map { |x| '\\b'+Regexp.escape(x)+'\\b' }.join('|'))

to prevent "aaardvark" being changed into "cccrdvark".

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • I will look at code and try to understand what is going on with this code now. – LEoREo_2247 Jun 10 '16 at 07:22
  • I have question. lets say i have there replacements below: `replacements = {'1杯の'=>'a cup of','たくさん'=>'a lot','たくさんの'=>'a lot of','1週間'=>'a week','午前'=>'a.m.','およそ'=>'about','住所'=>'adress','大人'=>'adult','アフリカ'=>'Africa','の後で'=>'after','のあとで'=>'after','午後'=>'afternoon','再び'=>'again','ふたたび'=>'again','空港'=>'airport','全て'=>'all','一日中'=>'all day','1日中'=>'all day','いつも'=>'always',}` and i want to add tag to all of english words. any easier way to do it? – LEoREo_2247 Jun 10 '16 at 13:14
  • I added comment on your answer. please check it. – LEoREo_2247 Jun 12 '16 at 06:19
  • That's a good question! I'm not 100% sure how to do that myself; perhaps post this as a separate question? Changing the replace text to `a cup of` and using [`add_next_sibling`](http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html#creating_new_nodes) will get you some of the way, but you might be pushing the limits of `gsub`. – Ken Y-N Jun 13 '16 at 02:16