Class: TxTranslate::SbvProcess

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/tx_translate/sbv_process.rb

Class Method Summary collapse

Class Method Details

.run(filename) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tx_translate/sbv_process.rb', line 5

def self.run(filename)
  file = File.open(filename, 'r')
  basename = File.basename(filename, "sbv")
  contents = file.read + "\n\n"
  contents.gsub!("\r\n", "\n") # 处理 CRLF 转 LF 问题

  subbed = ''

  original_content_array = []
  timeline_content_array = []
  context_content_array = []

  # 先切段
  contents.gsub(/(\d:\d{2}:\d{2}.\d{3}),(\d:\d{2}:\d{2}\.\d{3})\n((?:^.*$\n)*?)\n/).with_index do |m, i|
    original_content_array[i] = m # 旧内容

    timeline_content_array[i] = "#{Regexp.last_match(1)},#{Regexp.last_match(2)}"
    context_content_array[i] = Regexp.last_match(3).to_s
  end

  context_content_array = TxTranslate::ParallelArray.new(context_content_array).parallel_process

  original_content_array.each_with_index do |_item, i|
    subbed += timeline_content_array[i] + "\n" + context_content_array[i] + "\n\n"
  end

  file = File.open("#{basename}zh-si.sbv", 'w') { |f| f.write(subbed) }
end