Ruby - Automatický prekladač jednoduchých viet
Ukážkový program Automatický prekladač jednoduchých viet. S priloženým zdrojovým kódom v jazyku Ruby k stiahnutiu.
require 'Set' @@invalid_words = Set.new @@invalid_sentences = 0 class Sentence attr_reader :from, :too def initialize(sentence, from, too) @sentence = sentence @from = from @too = too end def translate(dictionary) if (dictionary == nil) puts "Cannot find dictionary: #{@from}->#{@too}" @@invalid_sentences += 1 return @sentence.upcase end # separace slov z vety sentence = @sentence.delete(",.'\"!") iwords = @@invalid_words.length splited = sentence.downcase.split(" ")#/[ ,.'"!]/) splited.delete(" ") # pokusim se prelozit slova ve vete splited.map! {|word| dictionary.translate_word(word)} sentence = splited.join(" ") @@invalid_sentences += 1 if (iwords != @@invalid_words.length) # vratim prelozenou / chybove zvyraznenou vetu return sentence end end class Dictionary attr_reader :from, :too def initialize(from, too) @words = Hash.new @from = from @too = too load_words end # adds word to dictionary def add_word(lang1, lang2) @words[lang1] = lang2 end def translate_word(word) found = @words[word] if (found != nil) return found else @@invalid_words.add(word) return word.upcase end end # lods words from file def load_words filename = @from.downcase + "_" + @too.downcase + ".dict" File.open(filename, "r") do |file| file.each_line do |line| splited = line.strip.split("|") add_word(splited[0], splited[1]) end end end end # -------------------------------- NACITANI DAT ------------------------------- # # 1) nacteni vstupniho souboru do pole sentences sentences = [] begin raise "Wrong parametters" if (ARGV.length != 2) raise "Input file not found" if not (File.exist?(ARGV[0])) lastline = "" File.open(ARGV[0], "r") do |file| counter = 0 file.each_line do |line| line.strip! if (counter % 2 != 0) splited = line.split(">") sentences << Sentence.new(lastline, splited[0], splited[1]) end lastline = line counter += 1 end end rescue Exception => e puts "An error occured: #{e}" exit end # 2) nacteni dostupnych slovniku dictionaries = [] begin dictionaries << Dictionary.new('EN','CZ') dictionaries << Dictionary.new('CZ','EN') dictionaries << Dictionary.new('DE','CZ') dictionaries << Dictionary.new('CZ','DE') rescue puts "Cannot read dictionaries" exit end # ------------------------- SAMOTNY PREKLAD ----------------------------- # 3) sentences_translated = Array.new sentences.each do |sentence| hash = Hash.new hash["lang"] = sentence.too hash["sentence"] = sentence.translate(dictionaries.find { |dict| (dict.from == sentence.from) && (dict.too == sentence.too) }) sentences_translated << hash end # 4) ulozeni prelozenych odpovedi File.open(ARGV[1],"w") do |file| sentences_translated.each do |sentence| file.puts sentence["sentence"] file.puts sentence["lang"] end end puts "I could not translate #{@@invalid_words.length} words" puts "I could not fully translate #{@@invalid_sentences}/#{sentences_translated.length} sentences, it is #{(@@invalid_sentences/(sentences_translated.length.to_f / 100)).to_i} %"
K behu nasledujúceho programu sú potrebné vstupné súbory, ktoré pochádza z materiálov Unicorn College.
Stiahnuť
Stiahnutím nasledujúceho súboru súhlasíš s licenčnými podmienkami
Stiahnuté 357x (4.01 kB)
Aplikácia je vrátane zdrojových kódov v jazyku Ruby