単語と文から2D配列を作成し、それに一致するが英語に翻訳された別の2D配列を作成しようとしています。
新しいレッスンを作成するときに発生するレッスンモデルからのコールバックは次のとおりです。
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
英語になってしまうのかわかりません。