Advertisement
Guest User

abcd

a guest
Jun 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.11 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class FR extends CI_Controller
  4. {
  5.     function __construct()
  6.     {
  7.         parent::__construct();
  8.         $this->load->model('User_Model', '', TRUE);
  9.         $this->load->model('Session_Model', 'ses', TRUE);
  10.         $this->load->driver('crs');
  11.         // $this->output->enable_profiler(TRUE);
  12.     }
  13.  
  14.  
  15. /*    public function ui_question_u1() //pertanyaan umum awal
  16.     {
  17.         $data['view']   = 'crs/_menu';
  18.         //$data['menu'] = false;
  19.         $data['fr']     = $this->crs->usermodeling->GenerateGeneralQ();
  20.        
  21.         echo $this->load->view('home_page', $data, true);
  22.     }*/
  23.    
  24.     public function ui_question_u4()
  25.     {
  26.         $usermodel  = $this->session->userdata('usermodel');
  27.         $data['fr'] = $this->crs->usermodeling->GenerateSpecificQ($usermodel);
  28.         $data['function'] = 'recommend';
  29.        
  30.         $this->session->set_userdata(array('uu' => 'u4'));
  31.        
  32.         echo $this->load->view('fr/_fr_no', $data, true);
  33.     }
  34.    
  35.     public function ui_question_u2()
  36.     {
  37.         if(!$this->input->is_ajax_request()) return false;
  38.         if(!$this->input->post('product')) return false;
  39.        
  40.         $product    = $this->input->post('product');
  41.         $products   = explode('||', $product);
  42.         $usermodel  = $this->session->userdata('usermodel');
  43.  
  44.         $result = $this->crs->pre->RecommendMultiple($products, $usermodel);
  45.  
  46.  
  47.         $data['result'] = $this->crs->usermodeling->GenerateDistinguishQ($products);
  48.  
  49.  
  50.         $test = $this->crs->usermodeling->GenerateDistinguishQ($products);
  51.  
  52.  
  53.  
  54.         $iduser     = $this->session->userdata('iduser');
  55.         // var_dump($_SESSION);
  56.         // echo $iduser;
  57.         foreach($products as $pp)
  58.         {
  59.             //save to db
  60.             $this->ses->adding($iduser, $pp, '', 'u2');
  61.         }
  62.         $result_data = $this->crs->pre->explain($result, $usermodel);
  63.         $data['result'] = $result_data;
  64.        
  65.         $data_session  = array(
  66.             'ipat' => $result_data
  67.  
  68.         );
  69.         $this->session->set_userdata($data_session);
  70.         // $this->session->set_userdata("dataDed".json_encode($data['result']));
  71.         // $this->session->set_userdata("dataDed".json_encode($data['result']));
  72.         // $this->session->set_userdata("dataDed","bacotafafafa");
  73.         echo $this->load->view('fr/_u2', $data, true);
  74.         // var_dump($_SESSION["data"]);
  75.         // echo json_encode($data['result']);
  76.     }
  77.  
  78.     public function ui_question_sortir()
  79.     {
  80.         if(!$this->input->is_ajax_request()) return false;
  81.         if(!$this->input->post('product')) return false;
  82.        
  83.         $text   = $this->input->post('product');
  84.         // $text = "saya mau parkir terluas"; //the text we want to send to the server
  85.         $panjangdataset = 54;
  86.         $usermodel  = array();
  87.         // $pref        = array();
  88.         $iduser     = $this->session->userdata('iduser');
  89.         $dictionary = array_map('str_getcsv', file('word_semantics4.csv'));
  90.         $PORT = 23456; //the port on which we are connecting to the "remote" machine   
  91.         $HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)
  92.  
  93.         $sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
  94.             or die("error: could not create socket\n");
  95.  
  96.         $succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
  97.             or die("error: could not connect to host\n");
  98.  
  99.         socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket
  100.             or die("error: failed to write to socket\n");
  101.  
  102.         $reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket
  103.             or die("error: failed to read from socket\n");
  104.         $file = fopen("test.txt", "w");
  105.  
  106.         //calculate vactor from user input
  107.         $inputVector = array_fill(0, $panjangdataset, 0);
  108.         $relevantWords = explode(" ", $reply);
  109.         foreach ($relevantWords as $rlvWord) {
  110.             fwrite($file, $rlvWord. " ");
  111.             for ($i=0; $i < $panjangdataset; $i++) {
  112.                 if(in_array($rlvWord, $dictionary[$i])) {
  113.                     $inputVector[$i]++;
  114.                     break;
  115.                 }
  116.             }
  117.         }
  118.         fwrite($file, PHP_EOL.PHP_EOL);
  119.         //calculate degree of relevance of each category according by words
  120.         $relevanceArray = array_fill(0, $panjangdataset, 0);
  121.         $nonzero = 0;
  122.         $cosineTotal = 0;
  123.         for ($i=0; $i < $panjangdataset; $i++) {
  124.             $pembilang = $inputVector[$i]; //only category value are multiplied by 1 since all other values in input vector are multiplied by 0
  125.             $penyebut = 0;
  126.             for ($j=0; $j < $panjangdataset; $j++) {
  127.                 $penyebut += pow($inputVector[$j],2);
  128.             }
  129.             if ($penyebut !== 0) {
  130.                 $penyebutRt = sqrt($penyebut);
  131.                 $relevanceArray[$i] = $pembilang/$penyebutRt;
  132.  
  133.                 $cosineTotal += $relevanceArray[$i];
  134.                 $nonzero++;
  135.             } else {
  136.                 $relevanceArray[0] = 2;
  137.                 break;
  138.             }
  139.         }
  140.        
  141.         $testa = "";
  142.         $testb = [];
  143.        
  144.         if ($relevanceArray[0] !== 2) {
  145.             $threshold = $cosineTotal/$nonzero;
  146.             fwrite($file, print_r($relevanceArray, true).PHP_EOL);
  147.             $ctgs = fopen("categories.txt", "w");
  148.             for ($i=0; $i < $panjangdataset; $i++) {
  149.                 if($relevanceArray[$i] > $threshold) {
  150.                     fwrite($ctgs, $dictionary[$i][0].PHP_EOL);
  151.                     $testb[] = $dictionary[$i][0];
  152.                 }
  153.             }
  154.             fclose($ctgs);
  155.         }
  156.  
  157.         $data = $this->session->ipat;
  158.         $item_tersortir = [];
  159.         // $anu = $data[1];
  160.         // var_dump($anu['luasparkir']);
  161.         // var_dump($testb);
  162.         if (count($testb) > 0){
  163.             if ($testb[0] == "Murah"){
  164.                 //echo "data disortir dari termurah";
  165.                 // echo json_encode($this->session->ipat);
  166.                 $item_tersortir = $data;
  167.  
  168.             }
  169.             else if ($testb[0] == "Mahal") {
  170.                 //echo "data disortir dari termahal";
  171.                 $item_tersortir = $data;
  172.                 //
  173.             } else if ($testb[0] == "Luas"){
  174.                 //echo "data disortir hanya yang parkirannya luas";
  175.                 foreach ($data as $item){
  176.                     if (count($item["luasparkir"]) && strpos($item["luasparkir"][0], 'Luas') !== false){
  177.                         $item_tersortir[] = $item;
  178.                     }
  179.                 }
  180.             }else if ($testb[0] == "Sempit"){
  181.                 foreach ($data as $item){
  182.                     if (count($item["luasparkir"]) && strpos($item["luasparkir"][0], 'Sempit') !== false){
  183.                         $item_tersortir[] = $item;
  184.                     }
  185.                 }
  186.             }else if ($testb[0] == "24"){
  187.                 //echo "data disortir hanya yang bukanya 24 jam";
  188.                 foreach ($data as $item){
  189.                     if (count($item["jambukaselasa"]) && strpos($item["jambukaselasa"][0], '24') !== false)
  190.                     //or if (count($item["jambukaselasa"]) && strpos($item["jambukaselasa"][0], '24') !== false)
  191.                     {
  192.                         $item_tersortir[] = $item;
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.         $data["result"] = $item_tersortir;
  198.         // $data_session  = array(
  199.         //  'ipat' => $item_tersortir
  200.  
  201.         // );
  202.         // $this->session->set_userdata($data_session);
  203.         // $this->session->set_userdata("dataDed".json_encode($data['result']));
  204.         // $this->session->set_userdata("dataDed".json_encode($data['result']));
  205.         // $this->session->set_userdata("dataDed","bacotafafafa");
  206.         echo $this->load->view('fr/_u2', $data, true);
  207.     }
  208.    
  209.     public function ui_question_u3()
  210.     {
  211.         $usermodel  = $this->session->userdata('usermodel');
  212.         $data['fr'] = $this->crs->usermodeling->GenerateUnExploredQ($usermodel);
  213.         $data['function'] = 'recommend';
  214.        
  215.         $this->session->set_userdata(array('uu' => 'u3'));
  216.        
  217.         echo $this->load->view('fr/_fr_no', $data, true);
  218.  
  219.     }
  220.     public function ui_recommend_u1()
  221.     {
  222.  
  223.         if(!$this->input->is_ajax_request()) return false;
  224.         //if(!$this->input->post('inp')) return false;
  225.        
  226.         //$inp      = $this->input->post('inp');   
  227.         $usermodel  = array();
  228.         // $pref        = array();
  229.         $iduser     = $this->session->userdata('iduser');
  230.         $dictionary = array_map('str_getcsv', file('word_semantics4.csv'));
  231.         $PORT = 23456; //the port on which we are connecting to the "remote" machine   
  232.         $HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)
  233.  
  234.         $sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
  235.             or die("error: could not create socket\n");
  236.  
  237.         $succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
  238.             or die("error: could not connect to host\n");
  239.  
  240.         $text = $_POST['dataString'];; //the text we want to send to the server
  241.         //echo $text;
  242.         //console.log($text);
  243.         #$text = "Lorem ipsum dolor sit amet"; //the text we want to send to the server
  244.  
  245.         socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket
  246.             or die("error: failed to write to socket\n");
  247.  
  248.         $reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket
  249.             or die("error: failed to read from socket\n");/*array(
  250.             array("Cagar_Alam","hutan","suaka"), //0
  251.             array("Camping_Ground","camping","kemah"),
  252.             array("Danau","situ","rawa","telaga"),
  253.             array("Museum_Alam","museum","geologi"),
  254.             array("Museum_Sejarah","sejarah"),
  255.             array("Pantai","pasir","laut","pesisir"),
  256.             array("Hiking","trek","trekking"),
  257.             array("Pemandangan_Alam","pemandangan","panorama","gunung"),
  258.             array("Pemandangan_Kota","pemandangan","kota"),
  259.             array("Pemandian_Air_Panas","spring","kolam", "air", "panas","mandi","pemandian"),
  260.             array("Peternakan","ternak","sapi","susu"), //10
  261.             array("Rekreasi_Kebun_Binatang","binatang",),
  262.             array("Taman_Kota","taman","alun-alun"),
  263.             array("Taman_Wisata","wisata"),
  264.             array("Taman_Bermain","rekreasi","main"),
  265.             array("Monumen","tugu","patung"),
  266.             array("Museum_Budaya","museum","pancasila"),
  267.             array("Wisata_Religi","masjid","spiritual","vihara"),
  268.             array("Jajanan_Es_Krim","krim","es"),
  269.             array("Jajanan_Oleh-oleh","oleh-oleh","suvenir","hadiah"),
  270.             array("Kuliner_Jepang","sushi","jepang"),//20
  271.             array("Kuliner_Korea","korea","bibimbap"),
  272.             array("Kuliner_Eropa","steak","eropa","italia","pasta"),
  273.             array("Kuliner_Timer_Tengah","mandhi","briyani","arab","timur"),
  274.             array("Kuliner_Makassar","makassar","konro"),
  275.             array("Kuliner_Padang","padang","sederhana"),
  276.             array("Kuliner_Sunda","sunda"),
  277.             array("Olah_Raga","olahraga","fitness","jogging","outbound"),
  278.             array("Fashion","butik","outlet","pakaian", "baju"),
  279.             array("Waterpark","kolam","renang"),
  280.             array("Shopping_Mall","mall","mewah"),//30
  281.             array("Berbelanja_Tradisional","pasar","terapung","tradisional"),
  282.             array("Rekreasi_Hiburan_Malam","malam","dewasa","biliar"),
  283.             array("Rekreasi_Keluarga","keluarga","anak","anak-anak"),
  284.             array("Rekreasi_Edukasi","belajar","edukasi"),//34
  285.         );*/
  286.            
  287.         #$inp = $_POST['dataString'];
  288.         //console.log("asdf");
  289.         $PORT = 23456; //the port on which we are connecting to the "remote" machine   
  290.         $HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)
  291.  
  292.         $sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
  293.             or die("error: could not create socket\n");
  294.  
  295.         $succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
  296.             or die("error: could not connect to host\n");
  297.        
  298.         $text = $_POST['dataString'];; //the text we want to send to the server
  299.         $panjangdataset = 54;
  300.         //echo $text;
  301.         //console.log($text);
  302.         #$text = "Lorem ipsum dolor sit amet"; //the text we want to send to the server
  303.  
  304.         socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket
  305.             or die("error: failed to write to socket\n");
  306.  
  307.         $reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket
  308.             or die("error: failed to read from socket\n");
  309.         //console_log("hjk");
  310.         #echo $reply;
  311.         #$tmp = $reply;
  312.         #$tagArray = explode(" ", $reply);
  313.         $file = fopen("test.txt", "w");
  314.         #foreach ($tagArray as $arrElm) {
  315.         #   fwrite($file, $arrElm);
  316.         #}
  317.         #$words = explode("|", $reply);
  318.  
  319.         //calculate vactor from user input
  320.         $inputVector = array_fill(0, $panjangdataset, 0);
  321.         $relevantWords = explode(" ", $reply);
  322.         foreach ($relevantWords as $rlvWord) {
  323.             fwrite($file, $rlvWord. " ");
  324.             for ($i=0; $i < $panjangdataset; $i++) {
  325.                 if(in_array($rlvWord, $dictionary[$i])) {
  326.                     $inputVector[$i]++;
  327.                     break;
  328.                 }
  329.             }
  330.         }
  331.         fwrite($file, PHP_EOL.PHP_EOL);
  332.         //calculate degree of relevance of each category according by words
  333.         $relevanceArray = array_fill(0, $panjangdataset, 0);
  334.         $nonzero = 0;
  335.         $cosineTotal = 0;
  336.         for ($i=0; $i < $panjangdataset; $i++) {
  337.             $pembilang = $inputVector[$i]; //only category value are multiplied by 1 since all other values in input vector are multiplied by 0
  338.             $penyebut = 0;
  339.             for ($j=0; $j < $panjangdataset; $j++) {
  340.                 $penyebut += pow($inputVector[$j],2);
  341.             }
  342.             if ($penyebut !== 0) {
  343.                 $penyebutRt = sqrt($penyebut);
  344.                 $relevanceArray[$i] = $pembilang/$penyebutRt;
  345.  
  346.                 $cosineTotal += $relevanceArray[$i];
  347.                 $nonzero++;
  348.             } else {
  349.                 $relevanceArray[0] = 2;
  350.                 break;
  351.             }
  352.         }
  353.         $testa = "";
  354.         $testb = [];
  355.        
  356.         if ($relevanceArray[0] !== 2) {
  357.             $threshold = $cosineTotal/$nonzero;
  358.             fwrite($file, print_r($relevanceArray, true).PHP_EOL);
  359.             $ctgs = fopen("categories.txt", "w");
  360.             for ($i=0; $i < $panjangdataset; $i++) {
  361.                 if($relevanceArray[$i] > $threshold) {
  362.                     fwrite($ctgs, $dictionary[$i][0].PHP_EOL);
  363.                     $testb[] = $dictionary[$i][0];
  364.                 }
  365.             }
  366.             fclose($ctgs);
  367.         }
  368.  
  369.         $inp = array();
  370.         if(!empty($testb)) {
  371.             foreach ($testb as $category) {
  372.                 $inp[$category] = 'fh';
  373.             }
  374.             //print($testb[0]);
  375.             //fwrite($file, $testa);
  376.            
  377.  
  378.             foreach($inp as $name => $value)
  379.             {
  380.                 $usermodel[] = array('name' => $name, 'level' => 1, 'status' => $value, 'leaf' => true);
  381.                
  382.                 //save to db
  383.                 //$this->ses->adding($iduser, $name, $value, 'u1');
  384.             }
  385.            
  386.            
  387.             //$this->session->set_userdata(array('usermodel' => $usermodel, 'level' => 1, 'pref' => ''));
  388.            
  389.             /*echo 'recommend 1<pre>';
  390.             echo 'usermodel :';
  391.             var_dump($usermodel);
  392.             //echo 'preferensi :';
  393.             //var_dump($pref);
  394.             //var_dump($level);
  395.             echo '</pre>';*/
  396.             // echo '<pre>'.print_r($usermodel, true).'</pre>';
  397.             // echo "=========================================";
  398.             $result = $this->crs->pre->Recommend($usermodel);
  399.             // echo '<pre>'.print_r($result, true).'</pre>';
  400.            
  401.             //$this->ses->adding($iduser, 'jumlah', '', 'u1', count($result));
  402.         } else {
  403.             $result = array();
  404.         }
  405.         if(empty($result))
  406.         {
  407.             $cats = "";
  408.             foreach ($testb as $category) {
  409.                 $cats = $cats.$category."\n";
  410.             }
  411.             $saveprevinput = fopen("previousinput.txt", "w");
  412.             fwrite($saveprevinput, $text);
  413.             fclose($saveprevinput);
  414.             echo "error;;Kategori yang sesuai = \n".$cats."\nTidak ditemukan lokasi wisata berdasarkan kebutuhan anda";
  415.            
  416.         }
  417.         else if(!empty($result) && count($result) <= $this->config->item('limit_recommend') && count($testb) <= $this->config->item('limit_category'))
  418.         {
  419.             $data['result'] = $this->crs->pre->explain($result, $usermodel);
  420.             echo $this->load->view('fr/_recommend', $data, true);
  421.             // echo '<pre>'.print_r($data['result'], true).'</pre>';
  422.         }
  423.         else if(!empty($result) && count($testb) > $this->config->item('limit_category'))
  424.         {
  425.             $cats = "";
  426.             foreach ($testb as $category) {
  427.                 $cats = $cats.$category."\n";
  428.             }
  429.             echo "error;;Kategori yang sesuai = \n".$cats."\nKebutuhan anda masih terlalu umum\nMohon masukkan kembali kebutuhan anda dengan lebih spesifik";
  430.             $saveprevinput = fopen("previousinput.txt", "w");
  431.             fwrite($saveprevinput, $text);
  432.             fclose($saveprevinput);
  433.         }
  434.         else if(!empty($result) && count($result) > $this->config->item('limit_recommend'))
  435.         {
  436.             $cats = "";
  437.             foreach ($testb as $category) {
  438.                 $cats = $cats.$category."\n";
  439.             }
  440.             echo "error;;Kategori yang sesuaai = \n".$cats."\nJumlah lokasi destinasi yang sesuai masih terlalu banyak\nMohon masukkan kembali kebutuhan anda dengan lebih spesifik";
  441.             $saveprevinput = fopen("previousinput.txt", "w");
  442.             fwrite($saveprevinput, $text);
  443.             fclose($saveprevinput);
  444.         }
  445.        
  446.         fwrite($file, count($testb));
  447.         fwrite($file, "pass");
  448.         fclose($file);
  449.     }
  450.    
  451.     public function ui_recommend()
  452.     {
  453.         if(!$this->input->is_ajax_request()) return false;
  454.         if(!$this->input->post('inp')) return false;
  455.        
  456.         $inp        = $this->input->post('inp');
  457.         $usermodel  = $this->session->userdata('usermodel');
  458.         $level      = $this->session->userdata('level');
  459.         //$pref     = $this->session->userdata('pref');
  460.         $iduser     = $this->session->userdata('iduser');
  461.         $uu         = $this->session->userdata('uu');
  462.        
  463.         //var_dump($usermodel);
  464.         foreach($inp['usermodel'] as $name => $value)
  465.         {
  466.             $usermodel[] = array('name' => $name, 'level' => $level, 'status' => $value, 'leaf' => true);
  467.            
  468.             //save to db
  469.             $this->ses->adding($iduser, $name, $value, $uu);
  470.         }
  471.        
  472.         //var_dump($usermodel);
  473.         $this->session->set_userdata(array('usermodel' => $usermodel));
  474.         /*echo 'recommend<pre>';
  475.         echo 'usermodel :';
  476.         var_dump($usermodel);
  477.         //echo 'preferensi :';
  478.         //var_dump($pref);
  479.         var_dump($level);
  480.         echo '</pre>';*/
  481.        
  482.  
  483.         $result = $this->crs->pre->Recommend($usermodel);
  484.         $this->ses->adding($iduser, 'jumlah', '', $uu, count($result));
  485.  
  486.         // echo "====== result ======";
  487.         // var_dump($result);
  488.  
  489.         if(empty($result))
  490.         {
  491.             echo "error;;u5";
  492.            
  493.         }
  494.         else if(!empty($result) && count($result) <= $this->config->item('limit_recommend'))
  495.         {
  496.             $data['result'] = $this->crs->pre->explain($result, $usermodel);
  497.             echo $this->load->view('fr/_recommend', $data, true);
  498.         }
  499.         else if(!empty($result) && count($result) > $this->config->item('limit_recommend'))
  500.         {
  501.             echo "error;;Destinasi yang sesuai = ".count($result)." Destinasi\nJumlah Destinasi yang sesuai masih terlalu banyak\nTekan ok untuk pertanyaan berikutnya yang lebih spesifik atau cancel untuk berhenti";
  502.         }
  503.     }
  504.    
  505.    
  506.  
  507.  
  508.  
  509.  
  510.  
  511. }
  512. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement