Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Ruby - Automatický překladač jednoduchých vět

Ukázkový program Automatický překladač jednoduchých vět. S přiloženým zdrojovým kódem v jazyce Ruby ke stažení.

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 běhu následujícího programu jsou potřeba vstupní soubory, které pochází z materiálů Unicorn College.


 

Stáhnout

Stažením následujícího souboru souhlasíš s licenčními podmínkami

Staženo 355x (4.01 kB)
Aplikace je včetně zdrojových kódů v jazyce Ruby

 

Všechny články v sekci
Ruby
Program pro vás napsal David Hartinger
Avatar
Uživatelské hodnocení:
1 hlasů
David je zakladatelem ITnetwork a programování se profesionálně věnuje 15 let. Má rád Nirvanu, nemovitosti a svobodu podnikání.
Unicorn university David se informační technologie naučil na Unicorn University - prestižní soukromé vysoké škole IT a ekonomie.
Aktivity