Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class FR extends CI_Controller
- {
- function __construct()
- {
- parent::__construct();
- $this->load->model('User_Model', '', TRUE);
- $this->load->model('Session_Model', 'ses', TRUE);
- $this->load->driver('crs');
- // $this->output->enable_profiler(TRUE);
- }
- /* public function ui_question_u1() //pertanyaan umum awal
- {
- $data['view'] = 'crs/_menu';
- //$data['menu'] = false;
- $data['fr'] = $this->crs->usermodeling->GenerateGeneralQ();
- echo $this->load->view('home_page', $data, true);
- }*/
- public function ui_question_u4()
- {
- $usermodel = $this->session->userdata('usermodel');
- $data['fr'] = $this->crs->usermodeling->GenerateSpecificQ($usermodel);
- $data['function'] = 'recommend';
- $this->session->set_userdata(array('uu' => 'u4'));
- echo $this->load->view('fr/_fr_no', $data, true);
- }
- public function ui_question_u2()
- {
- if(!$this->input->is_ajax_request()) return false;
- if(!$this->input->post('product')) return false;
- $product = $this->input->post('product');
- $products = explode('||', $product);
- $usermodel = $this->session->userdata('usermodel');
- $result = $this->crs->pre->RecommendMultiple($products, $usermodel);
- $data['result'] = $this->crs->usermodeling->GenerateDistinguishQ($products);
- $test = $this->crs->usermodeling->GenerateDistinguishQ($products);
- $iduser = $this->session->userdata('iduser');
- // var_dump($_SESSION);
- // echo $iduser;
- foreach($products as $pp)
- {
- //save to db
- $this->ses->adding($iduser, $pp, '', 'u2');
- }
- $result_data = $this->crs->pre->explain($result, $usermodel);
- $data['result'] = $result_data;
- $data_session = array(
- 'ipat' => $result_data
- );
- $this->session->set_userdata($data_session);
- // $this->session->set_userdata("dataDed".json_encode($data['result']));
- // $this->session->set_userdata("dataDed".json_encode($data['result']));
- // $this->session->set_userdata("dataDed","bacotafafafa");
- echo $this->load->view('fr/_u2', $data, true);
- // var_dump($_SESSION["data"]);
- // echo json_encode($data['result']);
- }
- public function ui_question_sortir()
- {
- if(!$this->input->is_ajax_request()) return false;
- if(!$this->input->post('product')) return false;
- $text = $this->input->post('product');
- // $text = "saya mau parkir terluas"; //the text we want to send to the server
- $panjangdataset = 54;
- $usermodel = array();
- // $pref = array();
- $iduser = $this->session->userdata('iduser');
- $dictionary = array_map('str_getcsv', file('word_semantics4.csv'));
- $PORT = 23456; //the port on which we are connecting to the "remote" machine
- $HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)
- $sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
- or die("error: could not create socket\n");
- $succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
- or die("error: could not connect to host\n");
- socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket
- or die("error: failed to write to socket\n");
- $reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket
- or die("error: failed to read from socket\n");
- $file = fopen("test.txt", "w");
- //calculate vactor from user input
- $inputVector = array_fill(0, $panjangdataset, 0);
- $relevantWords = explode(" ", $reply);
- foreach ($relevantWords as $rlvWord) {
- fwrite($file, $rlvWord. " ");
- for ($i=0; $i < $panjangdataset; $i++) {
- if(in_array($rlvWord, $dictionary[$i])) {
- $inputVector[$i]++;
- break;
- }
- }
- }
- fwrite($file, PHP_EOL.PHP_EOL);
- //calculate degree of relevance of each category according by words
- $relevanceArray = array_fill(0, $panjangdataset, 0);
- $nonzero = 0;
- $cosineTotal = 0;
- for ($i=0; $i < $panjangdataset; $i++) {
- $pembilang = $inputVector[$i]; //only category value are multiplied by 1 since all other values in input vector are multiplied by 0
- $penyebut = 0;
- for ($j=0; $j < $panjangdataset; $j++) {
- $penyebut += pow($inputVector[$j],2);
- }
- if ($penyebut !== 0) {
- $penyebutRt = sqrt($penyebut);
- $relevanceArray[$i] = $pembilang/$penyebutRt;
- $cosineTotal += $relevanceArray[$i];
- $nonzero++;
- } else {
- $relevanceArray[0] = 2;
- break;
- }
- }
- $testa = "";
- $testb = [];
- if ($relevanceArray[0] !== 2) {
- $threshold = $cosineTotal/$nonzero;
- fwrite($file, print_r($relevanceArray, true).PHP_EOL);
- $ctgs = fopen("categories.txt", "w");
- for ($i=0; $i < $panjangdataset; $i++) {
- if($relevanceArray[$i] > $threshold) {
- fwrite($ctgs, $dictionary[$i][0].PHP_EOL);
- $testb[] = $dictionary[$i][0];
- }
- }
- fclose($ctgs);
- }
- $data = $this->session->ipat;
- $item_tersortir = [];
- // $anu = $data[1];
- // var_dump($anu['luasparkir']);
- // var_dump($testb);
- if (count($testb) > 0){
- if ($testb[0] == "Murah"){
- //echo "data disortir dari termurah";
- // echo json_encode($this->session->ipat);
- $item_tersortir = $data;
- }
- else if ($testb[0] == "Mahal") {
- //echo "data disortir dari termahal";
- $item_tersortir = $data;
- //
- } else if ($testb[0] == "Luas"){
- //echo "data disortir hanya yang parkirannya luas";
- foreach ($data as $item){
- if (count($item["luasparkir"]) && strpos($item["luasparkir"][0], 'Luas') !== false){
- $item_tersortir[] = $item;
- }
- }
- }else if ($testb[0] == "Sempit"){
- foreach ($data as $item){
- if (count($item["luasparkir"]) && strpos($item["luasparkir"][0], 'Sempit') !== false){
- $item_tersortir[] = $item;
- }
- }
- }else if ($testb[0] == "24"){
- //echo "data disortir hanya yang bukanya 24 jam";
- foreach ($data as $item){
- if (count($item["jambukaselasa"]) && strpos($item["jambukaselasa"][0], '24') !== false)
- //or if (count($item["jambukaselasa"]) && strpos($item["jambukaselasa"][0], '24') !== false)
- {
- $item_tersortir[] = $item;
- }
- }
- }
- }
- $data["result"] = $item_tersortir;
- // $data_session = array(
- // 'ipat' => $item_tersortir
- // );
- // $this->session->set_userdata($data_session);
- // $this->session->set_userdata("dataDed".json_encode($data['result']));
- // $this->session->set_userdata("dataDed".json_encode($data['result']));
- // $this->session->set_userdata("dataDed","bacotafafafa");
- echo $this->load->view('fr/_u2', $data, true);
- }
- public function ui_question_u3()
- {
- $usermodel = $this->session->userdata('usermodel');
- $data['fr'] = $this->crs->usermodeling->GenerateUnExploredQ($usermodel);
- $data['function'] = 'recommend';
- $this->session->set_userdata(array('uu' => 'u3'));
- echo $this->load->view('fr/_fr_no', $data, true);
- }
- public function ui_recommend_u1()
- {
- if(!$this->input->is_ajax_request()) return false;
- //if(!$this->input->post('inp')) return false;
- //$inp = $this->input->post('inp');
- $usermodel = array();
- // $pref = array();
- $iduser = $this->session->userdata('iduser');
- $dictionary = array_map('str_getcsv', file('word_semantics4.csv'));
- $PORT = 23456; //the port on which we are connecting to the "remote" machine
- $HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)
- $sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
- or die("error: could not create socket\n");
- $succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
- or die("error: could not connect to host\n");
- $text = $_POST['dataString'];; //the text we want to send to the server
- //echo $text;
- //console.log($text);
- #$text = "Lorem ipsum dolor sit amet"; //the text we want to send to the server
- socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket
- or die("error: failed to write to socket\n");
- $reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket
- or die("error: failed to read from socket\n");/*array(
- array("Cagar_Alam","hutan","suaka"), //0
- array("Camping_Ground","camping","kemah"),
- array("Danau","situ","rawa","telaga"),
- array("Museum_Alam","museum","geologi"),
- array("Museum_Sejarah","sejarah"),
- array("Pantai","pasir","laut","pesisir"),
- array("Hiking","trek","trekking"),
- array("Pemandangan_Alam","pemandangan","panorama","gunung"),
- array("Pemandangan_Kota","pemandangan","kota"),
- array("Pemandian_Air_Panas","spring","kolam", "air", "panas","mandi","pemandian"),
- array("Peternakan","ternak","sapi","susu"), //10
- array("Rekreasi_Kebun_Binatang","binatang",),
- array("Taman_Kota","taman","alun-alun"),
- array("Taman_Wisata","wisata"),
- array("Taman_Bermain","rekreasi","main"),
- array("Monumen","tugu","patung"),
- array("Museum_Budaya","museum","pancasila"),
- array("Wisata_Religi","masjid","spiritual","vihara"),
- array("Jajanan_Es_Krim","krim","es"),
- array("Jajanan_Oleh-oleh","oleh-oleh","suvenir","hadiah"),
- array("Kuliner_Jepang","sushi","jepang"),//20
- array("Kuliner_Korea","korea","bibimbap"),
- array("Kuliner_Eropa","steak","eropa","italia","pasta"),
- array("Kuliner_Timer_Tengah","mandhi","briyani","arab","timur"),
- array("Kuliner_Makassar","makassar","konro"),
- array("Kuliner_Padang","padang","sederhana"),
- array("Kuliner_Sunda","sunda"),
- array("Olah_Raga","olahraga","fitness","jogging","outbound"),
- array("Fashion","butik","outlet","pakaian", "baju"),
- array("Waterpark","kolam","renang"),
- array("Shopping_Mall","mall","mewah"),//30
- array("Berbelanja_Tradisional","pasar","terapung","tradisional"),
- array("Rekreasi_Hiburan_Malam","malam","dewasa","biliar"),
- array("Rekreasi_Keluarga","keluarga","anak","anak-anak"),
- array("Rekreasi_Edukasi","belajar","edukasi"),//34
- );*/
- #$inp = $_POST['dataString'];
- //console.log("asdf");
- $PORT = 23456; //the port on which we are connecting to the "remote" machine
- $HOST = "localhost"; //the ip of the remote machine (in this case it's the same machine)
- $sock = socket_create(AF_INET, SOCK_STREAM, 0) //Creating a TCP socket
- or die("error: could not create socket\n");
- $succ = socket_connect($sock, $HOST, $PORT) //Connecting to to server using that socket
- or die("error: could not connect to host\n");
- $text = $_POST['dataString'];; //the text we want to send to the server
- $panjangdataset = 54;
- //echo $text;
- //console.log($text);
- #$text = "Lorem ipsum dolor sit amet"; //the text we want to send to the server
- socket_write($sock, $text . "\n", strlen($text) + 1) //Writing the text to the socket
- or die("error: failed to write to socket\n");
- $reply = socket_read($sock, 10000, PHP_NORMAL_READ) //Reading the reply from socket
- or die("error: failed to read from socket\n");
- //console_log("hjk");
- #echo $reply;
- #$tmp = $reply;
- #$tagArray = explode(" ", $reply);
- $file = fopen("test.txt", "w");
- #foreach ($tagArray as $arrElm) {
- # fwrite($file, $arrElm);
- #}
- #$words = explode("|", $reply);
- //calculate vactor from user input
- $inputVector = array_fill(0, $panjangdataset, 0);
- $relevantWords = explode(" ", $reply);
- foreach ($relevantWords as $rlvWord) {
- fwrite($file, $rlvWord. " ");
- for ($i=0; $i < $panjangdataset; $i++) {
- if(in_array($rlvWord, $dictionary[$i])) {
- $inputVector[$i]++;
- break;
- }
- }
- }
- fwrite($file, PHP_EOL.PHP_EOL);
- //calculate degree of relevance of each category according by words
- $relevanceArray = array_fill(0, $panjangdataset, 0);
- $nonzero = 0;
- $cosineTotal = 0;
- for ($i=0; $i < $panjangdataset; $i++) {
- $pembilang = $inputVector[$i]; //only category value are multiplied by 1 since all other values in input vector are multiplied by 0
- $penyebut = 0;
- for ($j=0; $j < $panjangdataset; $j++) {
- $penyebut += pow($inputVector[$j],2);
- }
- if ($penyebut !== 0) {
- $penyebutRt = sqrt($penyebut);
- $relevanceArray[$i] = $pembilang/$penyebutRt;
- $cosineTotal += $relevanceArray[$i];
- $nonzero++;
- } else {
- $relevanceArray[0] = 2;
- break;
- }
- }
- $testa = "";
- $testb = [];
- if ($relevanceArray[0] !== 2) {
- $threshold = $cosineTotal/$nonzero;
- fwrite($file, print_r($relevanceArray, true).PHP_EOL);
- $ctgs = fopen("categories.txt", "w");
- for ($i=0; $i < $panjangdataset; $i++) {
- if($relevanceArray[$i] > $threshold) {
- fwrite($ctgs, $dictionary[$i][0].PHP_EOL);
- $testb[] = $dictionary[$i][0];
- }
- }
- fclose($ctgs);
- }
- $inp = array();
- if(!empty($testb)) {
- foreach ($testb as $category) {
- $inp[$category] = 'fh';
- }
- //print($testb[0]);
- //fwrite($file, $testa);
- foreach($inp as $name => $value)
- {
- $usermodel[] = array('name' => $name, 'level' => 1, 'status' => $value, 'leaf' => true);
- //save to db
- //$this->ses->adding($iduser, $name, $value, 'u1');
- }
- //$this->session->set_userdata(array('usermodel' => $usermodel, 'level' => 1, 'pref' => ''));
- /*echo 'recommend 1<pre>';
- echo 'usermodel :';
- var_dump($usermodel);
- //echo 'preferensi :';
- //var_dump($pref);
- //var_dump($level);
- echo '</pre>';*/
- // echo '<pre>'.print_r($usermodel, true).'</pre>';
- // echo "=========================================";
- $result = $this->crs->pre->Recommend($usermodel);
- // echo '<pre>'.print_r($result, true).'</pre>';
- //$this->ses->adding($iduser, 'jumlah', '', 'u1', count($result));
- } else {
- $result = array();
- }
- if(empty($result))
- {
- $cats = "";
- foreach ($testb as $category) {
- $cats = $cats.$category."\n";
- }
- $saveprevinput = fopen("previousinput.txt", "w");
- fwrite($saveprevinput, $text);
- fclose($saveprevinput);
- echo "error;;Kategori yang sesuai = \n".$cats."\nTidak ditemukan lokasi wisata berdasarkan kebutuhan anda";
- }
- else if(!empty($result) && count($result) <= $this->config->item('limit_recommend') && count($testb) <= $this->config->item('limit_category'))
- {
- $data['result'] = $this->crs->pre->explain($result, $usermodel);
- echo $this->load->view('fr/_recommend', $data, true);
- // echo '<pre>'.print_r($data['result'], true).'</pre>';
- }
- else if(!empty($result) && count($testb) > $this->config->item('limit_category'))
- {
- $cats = "";
- foreach ($testb as $category) {
- $cats = $cats.$category."\n";
- }
- echo "error;;Kategori yang sesuai = \n".$cats."\nKebutuhan anda masih terlalu umum\nMohon masukkan kembali kebutuhan anda dengan lebih spesifik";
- $saveprevinput = fopen("previousinput.txt", "w");
- fwrite($saveprevinput, $text);
- fclose($saveprevinput);
- }
- else if(!empty($result) && count($result) > $this->config->item('limit_recommend'))
- {
- $cats = "";
- foreach ($testb as $category) {
- $cats = $cats.$category."\n";
- }
- echo "error;;Kategori yang sesuaai = \n".$cats."\nJumlah lokasi destinasi yang sesuai masih terlalu banyak\nMohon masukkan kembali kebutuhan anda dengan lebih spesifik";
- $saveprevinput = fopen("previousinput.txt", "w");
- fwrite($saveprevinput, $text);
- fclose($saveprevinput);
- }
- fwrite($file, count($testb));
- fwrite($file, "pass");
- fclose($file);
- }
- public function ui_recommend()
- {
- if(!$this->input->is_ajax_request()) return false;
- if(!$this->input->post('inp')) return false;
- $inp = $this->input->post('inp');
- $usermodel = $this->session->userdata('usermodel');
- $level = $this->session->userdata('level');
- //$pref = $this->session->userdata('pref');
- $iduser = $this->session->userdata('iduser');
- $uu = $this->session->userdata('uu');
- //var_dump($usermodel);
- foreach($inp['usermodel'] as $name => $value)
- {
- $usermodel[] = array('name' => $name, 'level' => $level, 'status' => $value, 'leaf' => true);
- //save to db
- $this->ses->adding($iduser, $name, $value, $uu);
- }
- //var_dump($usermodel);
- $this->session->set_userdata(array('usermodel' => $usermodel));
- /*echo 'recommend<pre>';
- echo 'usermodel :';
- var_dump($usermodel);
- //echo 'preferensi :';
- //var_dump($pref);
- var_dump($level);
- echo '</pre>';*/
- $result = $this->crs->pre->Recommend($usermodel);
- $this->ses->adding($iduser, 'jumlah', '', $uu, count($result));
- // echo "====== result ======";
- // var_dump($result);
- if(empty($result))
- {
- echo "error;;u5";
- }
- else if(!empty($result) && count($result) <= $this->config->item('limit_recommend'))
- {
- $data['result'] = $this->crs->pre->explain($result, $usermodel);
- echo $this->load->view('fr/_recommend', $data, true);
- }
- else if(!empty($result) && count($result) > $this->config->item('limit_recommend'))
- {
- 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";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement