我想用單詞和句子製作一個二維數組,然後再製作一個與之匹配的二維數組,但翻譯成英文。爲什麼這兩個變量結果相同?
這裏是當我創建一個新的教訓出現這種情況的課程模式回調:
before_create do |lesson|
require 'rmmseg'
require "to_lang"
require "bing_translator"
lesson.parsed_content =[]
lesson.html_content = []
RMMSeg::Dictionary.load_dictionaries
text = lesson.content
text = text.gsub("。","^^.")
text = text.gsub("?","~~?")
text = text.gsub("!", "||!")
text = text.split(/[.?!]/u) #convert to an array
text.each do |s|
s.gsub!("^^","。")
s.gsub!("~~","?")
s.gsub!("||","!")
end
text.each_with_index do |val, index|
algor = RMMSeg::Algorithm.new(text[index])
splittext = []
loop do
tok = algor.next_token
break if tok.nil?
tex = tok.text.force_encoding('UTF-8')
splittext << tex
text[index] = splittext
end
end
lesson.parsed_content = text
textarray = text
translator = BingTranslator.new(BING_CLIENT_ID, BING_API_KEY)
ToLang.start(GOOGLE_TRANSLATE_API)
textarray.each_with_index do |sentence, si| #iterate array of sentence
textarray[si] = []
sentence.each_with_index do |word,wi| #iterate sentence's array of words
entry = DictionaryEntry.find_by_simplified(word) #returns a DictionaryEntry object hash
if entry == nil #for cases where there is no DictionaryEntry
textarray[si] << word
else
textarray[si] << entry.definition
end
end
lesson.html_content = textarray
end
end
爲什麼我的變量lesson.parsed_content
和lesson.html_content
結束了彼此相等?
我期待lesson.parsed_content
是中文,lesson.html_content
是英文,但最終都是英文。我可能太累了,但我不明白爲什麼lesson.parsed_content
也會結束英語。
謝謝。那工作。我每天從字面上學習新東西。 – webmagnets