12/08/2018, 13:03

Porting Amharic Translation System ( RoR + MySQL )

Nowadays, there is not Amharic Translation System even if Google translation. So it seems big business chance if the translation system is developed. I had tried developing it on Windows7 + Oracle Database 11g. So in this time I have tried Porting Amharic Translation System to Ruby on Rails. ...

Nowadays, there is not Amharic Translation System even if Google translation. So it seems big business chance if the translation system is developed. I had tried developing it on Windows7 + Oracle Database 11g. So in this time I have tried Porting Amharic Translation System to Ruby on Rails.

Amharic is an official language in Ethiopia. It is the second-most spoken Semitic language in the world, after Arabic. The grammatical style is similar with Japanese language. So to translate to Japanese is easier than other languages. If you want to know more detail, look at below: https://en.wikipedia.org/wiki/Amharic

Ruby 2.2.1 Rails 4.2.5 MySQL 5.5.44

Create new project

rails new translation_app -d mysql

When you want to use MySQL, add "-d mysql" option.

Edit "database.yml".

development:
  <<: *default
  database: translation_app_development

test:
  <<: *default
  database: translation_app_test

production:
  <<: *default

Note: The code is extract. This style is applied over all.

Create database

create database translation_app_development default character set utf8;
create database translation_app_test default character set utf8;

Create controller and model.

rails generate controller AmharicTranslations index
rails generate model AmharicTranslation input:text output:text
rails generate model AmharicDictionary amharic:string japanese:string
rails generate migration AmharicTranslationProcedure

Edit "routes.rb".

Rails.application.routes.draw do
  root      'amharic_translations#index'
  resources :amharic_translations
end

Edit "amharic_translations_controller.rb".

class AmharicTranslationsController < ApplicationController
  def index
    @amharic_translations = AmharicTranslation.all
  end

  def create
    AmharicTranslation.delete_all
    @amharic_translation = AmharicTranslation.new
    @amharic_translation.input = params[:amharic_translation][:input]
    @amharic_translation.save
    ActiveRecord::Base.connection.execute( "CALL P_AMHARIC_TRANSLATION()" )
    redirect_to '/amharic_translations/'
  end
end

Edit "index.html.erb".

<% @amharic_translations.each do |amharic_translation| %>
  <%= form_for AmharicTranslation.new do |f| %>
    <p>
      <%= f.label :"አማርኛ" %><br>
      <%= f.text_area :input, :value => amharic_translation.input %>
    </p>
    <%= f.submit "翻訳 መተርጐም" %>
    <p>
      <%= f.label :"日本語" %><br>
      <%= f.text_area :output, :value => amharic_translation.output %>
    </p>
  <% end %>
<% end %>

Edit "application.css".

textarea{
	resize: none;
	awidth: 100%;
}

@media all and (min-device-awidth: 855px){
	body{
		margin: 0 auto;
		awidth: 800px;
	}

	textarea{
		height: 200px;
	}
}

@media all and (max-device-awidth: 854px){
	body{
		awidth: 100%;
	}

	textarea{
		height: 100px;
	}
}

Edit "YYYYMMDDhhmmss_amharic_translation_procedure.rb".

class AmharicTranslationProcedure < ActiveRecord::Migration
  def self.up
    execute <<-SQL

# contents

    SQL
    def self.down
      execute "DROP PROCEDURE P_AMHARIC_TRANSLATION;"
    end
  end
end

The contents is like bellow:

CREATE PROCEDURE P_AMHARIC_TRANSLATION(
/*=====================================================
    機能:   翻訳エンジン
    作成日: 2015/11/10
    作成者: 横田  薫
    更新履歴: 2015/11/27 Ruby on Rails対応
  =====================================================*/
/*-----------------------------------------------------
    引数定義
  -----------------------------------------------------*/
)
BEGIN
/*-----------------------------------------------------
  変数定義
  -----------------------------------------------------*/
    DECLARE LnDICTIONARY INT;         -- CURSOR制御用
    DECLARE LsINPUT      VARCHAR(40); -- DB読込用
    DECLARE LsOUTPUT     VARCHAR(40); -- DB読込用
/*-----------------------------------------------------
  CURSOR定義
  -----------------------------------------------------*/
    DECLARE C_DICTIONARY CURSOR FOR
        select
             LOWER( amharic )
            ,japanese
        from
            amharic_dictionaries
        where
            amharic IS NOT NULL
        order by
            length( amharic ) desc;
/*-----------------------------------------------------
  イベント制御規定
  -----------------------------------------------------*/
    DECLARE EXIT HANDLER FOR NOT FOUND SET LnDICTIONARY = 0;
/*-----------------------------------------------------
    ▼▼ Main routine  ここから ▼▼
  -----------------------------------------------------*/

    -- ワークテーブルへの書き出し
    UPDATE amharic_translations SET output = LOWER( input );

    -- CURSOR開く
    OPEN C_DICTIONARY;

    SET LnDICTIONARY = 1;
    WHILE LnDICTIONARY DO
        --  1レコード読みだして,  各変数へ代入
        FETCH
            C_DICTIONARY
        INTO
             LsINPUT
            ,LsOUTPUT;
        UPDATE
            amharic_translations
        SET
            output = REPLACE( output, LsINPUT, LsOUTPUT );
    END WHILE;

    -- CURSOR閉じる
    CLOSE C_DICTIONARY;
/*-----------------------------------------------------
    ▲▲ Main routine  ここまで ▲▲
  -----------------------------------------------------*/
END

Edit "seeds.rb".

Dir.glob(File.join(Rails.root, 'db', 'seeds', '*.rb')) do |file|
  load(file)
end

Create "seeds" derectory and *.rb files there.

amharic_dictionaries.rb

AmharicDictionary.connection.execute("TRUNCATE TABLE amharic_dictionaries;")
AmharicDictionary.create!( amharic: "ጤና ይስጥልኝ", japanese: "こんにちは" )
 .
 .
 .
AmharicDictionary.create!( amharic: "ሀኖይ", japanese: "ハノイ" )

amharic_translations.rb

AmharicTranslation.connection.execute("TRUNCATE TABLE amharic_translations;")
AmharicTranslation.create!( input: "アムハラ語の文章を入れてください" )

Create table and inset data

bundle exec rake db:migrate
bundle exec rake db:seed

"bundle exec rake db:migrate" is refer to create tables and prpcedures. "bundle exec rake db:seed" is refer to insert data.

Before execute the program, to check of stred procedure is recommended.

mysql> select * from amharic_translations;
+----+------------------------+-----------------+---------------------+---------------------+
| id | input                  | output          | created_at          | updated_at          |
+----+------------------------+-----------------+---------------------+---------------------+
|  6 | ጤና ይስጥልኝ             | こんにちは       | 2015-11-30 07:44:33 | 2015-11-30 07:44:33 |
+----+------------------------+-----------------+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> update amharic_translations set input = "ትናንትና ሀኖይ ደረስን።";
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from amharic_translations;
+----+--------------------+-----------------+---------------------+---------------------+
| id | input              | output          | created_at          | updated_at          |
+----+--------------------+-----------------+---------------------+---------------------+
|  6 | ትናንትና ሀኖይ ደረስን።  | こんにちは       | 2015-11-30 07:44:33 | 2015-11-30 07:44:33 |
+----+--------------------+-----------------+---------------------+---------------------+
1 row in set (0.01 sec)

mysql> call P_AMHARIC_TRANSLATION();
Query OK, 0 rows affected, 1 warning (0.26 sec)

mysql> select * from amharic_translations;
+----+--------------------+-------------------------------+---------------------+---------------------+
| id | input              | output                        | created_at          | updated_at          |
+----+--------------------+-------------------------------+---------------------+---------------------+
|  6 | ትናንትና ሀኖይ ደረስን።  | 昨日 ハノイ 着いた。            | 2015-11-30 07:44:33 | 2015-11-30 07:44:33 |
+----+--------------------+-------------------------------+---------------------+---------------------+
1 row in set (0.00 sec)

When you want to use Heroku and MySQL, set addons.

heroku addons:add cleardb
heroku config

After that, you can get DB infomation like bellow:

CLEARDB_DATABASE_URL:     mysql://AAAAA:BBBBB@us-cdbr-iron-east-03.cleardb.net/CCCCC?reconnect=true

You can test DB connection.

mysql -h us-cdbr-iron-east-03.cleardb.net -u AAAAA -pBBBBB -D CCCCC

If there is not any errors, set like bellow.

heroku config:set DATABASE_URL='mysql2://AAAAA:BBBBB@us-cdbr-iron-east-03.cleardb.net/CCCCC?reconnect=true'

Of course you have to create table and procedure like below.

git push heroku master
heroku run rake db:migrate
heroku run rake db:seed

Open https://yokota-kaoru.herokuapp.com/ and type Amharic sentences. The example is like below: ያንድ ሰው የልብ ትርታ በደቂቃ ከ60 እስከ 80 ይደርሳል። በዓመት 40 ሚሊዮን ያህል ጊዜ ይመታል ማለት ነው። በያንዳንዷ ትርታ ወቅት 1/4 ሊተር ደም ወደ ልብ ይገባል ማለት ነው። ልብ በአንድ ቀን ውሎው 2200 ጋሎን ያህል ደም ይረጫል። በሌላ አነጋገር 56 ሚሊዮን ጋሎን ያህል ደም በአማካኝ የሕይወት ዘመን ውስጥ ይረጫል ማለት ነው። 11.png Source code https://github.com/yokotakaoru/translation_app

  • From Amharic to Japanese only
  • Not support for multi meaning words

Thank you for your reading!

0