我正在尝试用单词和句子制作一个二维数组,然后制作另一个与之匹配但翻译成英文的二维数组。
这是我创建新课程时发生的课程模型的回调:
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
也会以英语结束。