การทำวุ้นแปลภาษาด้วย line chat bot



 


     บทความนี้เราจะมาทำวุ้นแปลภาษา จาก Line chat bot  หลักการทำงานของมันจะไปเรียกใช้ google translate ครับ

   พอดีผมไปดูบทความหนึ่งใน youtube    https://www.youtube.com/watch?v=WwzICJdJ1pg  ของคุณพิเชษฐ์ ขุนใจ เรื่อง  Google Translate API ภาษาไทย ง่ายๆ พร้อมเขียน Jquery Plugin EP Basic  ซึ่งดูแล้วมีประโยชน์มาก เลยขออนุญาตคุณพิเชษฐ์ ขุนใจ แห่ง th Script  มาเผยแพร่ต่อ ซึ่งคุณพิเชษฐ์ ใจดีมากครับ ได้อนุญาตให้ผมมาเผยแพร่ได้ ก็ขอขอบคุณมา ณ โอกาสนี้ด้วยครับ

           วิธีการทำงานของมันก็คือ เราจะใช้โปรแกรม Line บนมือถือพิมพ์ keyword ที่เรากำหนดไว้  ซึ่ง keyword จะเป็นอะไรก็ได้ พร้อมกับประโยคที่ต้องการจะแปล
เช่น
          ภาษาอังกฤษ
                 พิมพ์  แปล + ประโยคทีต้องการแปล
     
          ภาษาสเปน
                      แปลsp + ประโยคทีต้องการแปล

          ภาษาเกาหลี
                     แปลko  + ประโยคทีต้องการแปล




วิธีการทำ

  ก็ใช้งาน Line ผ่าน chat bot abdul ของอาจารย์โคนัน จาก nectec เหมือนเดิมครับ ใครยังไม่ได้อ่าน ก็ไปอ่านได้น่ะครับ ที่  https://havespirit.blogspot.com/2017/08/line-chatbot-as-conversation-ai.html

เราต้องเตรียมฝั่ง server ก่อนครับ เป็น ไฟล์ php จำนวน 2 ไฟล์
คือ 1. GoogleTranslate.php
       2.trans.php



 ////////   ////////////////
<?php 
class GoogleTranslate
{

    public static function translate($source, $target, $text)
    {
        
        $response = self::requestTranslation($source, $target, $text);
        $translation = self::getSentencesFromJSON($response);

        return $translation;
    }

    protected static function requestTranslation($source, $target, $text)
    {

        // Google translate URL
        $url = "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e";

        $fields = array(
            'sl' => urlencode($source),
            'tl' => urlencode($target),
            'q' => urlencode($text)
        );

        // URL-ify the data for the POST
        $fields_string = "";
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }

        rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1');

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }

    protected static function getSentencesFromJSON($json)
    {
        $sentencesArray = json_decode($json, true);
        $sentences = "";

        foreach ($sentencesArray["sentences"] as $s) {
            $sentences .= isset($s["trans"]) ? $s["trans"] : '';
        }

        return $sentences;
    }
}


?>

และ  trans.php  อันนี้แปล ไทยเป็นอังกฤษ

<?php
 require_once "GoogleTranslate.php";
 $word = $_REQUEST['word'];
$GT = NEW GoogleTranslate();
$response = $GT->translate('th','en',$word);  /// ตรง en เราสามารถเปลี่ยนเป็น ภาษาอื่นได้
//echo "<pre>";
echo $word."   =   ".$response;
?>

/// ตรง en เราสามารถเปลี่ยนเป็น ภาษาอื่นได้  เช่น เกาหลี = ko ,สเปน = sp

//// แปลไทยเป็น ญี่ปุ่น
<?php
 require_once "GoogleTranslate.php";
 $word = $_REQUEST['word'];
$GT = NEW GoogleTranslate();
$response = $GT->translate('th','ja',$word);
//echo "<pre>";
echo $word."   =   ".$response;
?>

//// แปลเป็นอังกฤษไทย 

<?php
 require_once "GoogleTranslate.php";
 $word = $_REQUEST['word'];
$GT = NEW GoogleTranslate();
$response = $GT->translate('en','th',$word);
//echo "<pre>";
echo $word."   =   ".$response;
?>
/////////////////////////

การตั้งค่า abdul




/////////////////////

ความคิดเห็น

  1. ลองๆทำแล้ว ทำไมมันส่งกลับมาเป็นรูปสติ๊กเกอร์ตลอดเลยครับ

    ตอบลบ
  2. แจ่มอ่า เดี๋ยวลองมั่ง อิอิ

    ตอบลบ
  3. ความคิดเห็นนี้ถูกผู้เขียนลบ

    ตอบลบ

แสดงความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

แจ้งเตือนเข้าไลน์กลุ่ม ผ่าน Line notify เมื่อมีคน login เข้า server ของเราผ่าน SSH (linux) หรือ remote desktop เข้ามา (windows server)

การทำ cloud iot ด้วย thingsboard ไว้ใช้เองครับ

การประยุกต์ใช้ line notify ในการแจ้งปัญหาการใช้งาน สำหรับ php