コード例 #1
0
ファイル: Mypattern.php プロジェクト: narum/jocomunicoLAB
 public function launchCleanerES($tipusfrase)
 {
     $CI =& get_instance();
     $CI->load->library('Mymatching');
     $CI->load->library('Myslot');
     $matching = new Mymatching();
     // TREURE ELS "YO" I "TÚ" i agafar info dels verbs per després
     $keymainverb = null;
     $indexmainverb = -1;
     $indexsecondaryverb = -1;
     $indextheme1 = -1;
     $indextheme2 = -1;
     $indexdesire = -1;
     $indexpermission = -1;
     $mainverbinf = false;
     $secondaryverbinf = false;
     $numslots = count($this->ordrefrase);
     for ($i = 0; $i < $numslots; $i++) {
         $slotaux = $this->slotarray[$this->ordrefrase[$i]];
         if ($slotaux->category == "Main Verb") {
             $indexmainverb = $i;
             if ($slotaux->isInfinitive) {
                 $mainverbinf = true;
             }
             $keymainverb = $this->ordrefrase[$i];
             // agafem el key del main verb pel pas dels modificadors
         } else {
             if ($slotaux->category == "Secondary Verb") {
                 $indexsecondaryverb = $i;
                 if ($slotaux->isInfinitive) {
                     $secondaryverbinf = true;
                 }
             } else {
                 if ($slotaux->category == "Theme") {
                     if ($slotaux->level == 1 && $slotaux->defvalueused && $slotaux->defvalue == "lo") {
                         $indextheme1 = $i;
                     } else {
                         if ($slotaux->level == 2 && $slotaux->defvalueused && $slotaux->defvalue == "lo") {
                             $indextheme2 = $i;
                         }
                     }
                 } else {
                     if ($slotaux->category == "Desire") {
                         $indexdesire = $i;
                     } else {
                         if ($slotaux->category == "Permission") {
                             $indexpermission = $i;
                         } else {
                             if ($slotaux->category == "Subject") {
                                 if ($slotaux->defvalueused) {
                                     // esborrem el tú o yo
                                     if ($slotaux->defvalue == '1' || $slotaux->defvalue == '2') {
                                         $slotaux->slotstring = array();
                                     }
                                 } else {
                                     // esborrem el tú o el yo
                                     if ($slotaux->paraulafinal->text == "yo" || $slotaux->paraulafinal->text == "tú") {
                                         $slotaux->slotstring = array();
                                     }
                                     // esborrem el subjecte del verb secundari, si és el mateix que el del principal
                                     if ($slotaux->level == 2 && $this->subjsiguals) {
                                         $slotaux->slotstring = array();
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $indexreceiver1 = -1;
     $indexreceiver2 = -1;
     $receiver1pron = false;
     $receiver2pron = false;
     // si és una ordre els pronoms aniran darrere el verb i tindran una altra forma
     $ordre = $tipusfrase == "ordre";
     $elementaux = array();
     // TRANSFORMAR ELS PRONOMS A FEBLES DEL RECEIVER
     for ($i = 0; $i < $numslots; $i++) {
         $slotaux = $this->slotarray[$this->ordrefrase[$i]];
         if ($slotaux->category == "Receiver") {
             // si hi ha valors per defecte
             if ($slotaux->defvalueused) {
                 if ($slotaux->defvalue == "mí") {
                     // posar "me" en tots els casos
                     if ($slotaux->level == 1) {
                         $slotaux->slotstring = array();
                         $elementaux[0] = "me";
                         $elementaux[1] = null;
                         $slotaux->slotstring[] = $elementaux;
                         $indexreceiver1 = $i;
                         $receiver1pron = true;
                     } else {
                         if ($slotaux->level == 2) {
                             $slotaux->slotstring = array();
                             $elementaux[0] = "me";
                             $elementaux[1] = null;
                             $slotaux->slotstring[] = $elementaux;
                             $indexreceiver2 = $i;
                             $receiver2pron = true;
                         }
                     }
                 } else {
                     if ($slotaux->defvalue == "tú") {
                         // posar "te"
                         if ($slotaux->level == 1) {
                             $slotaux->slotstring = array();
                             $elementaux[0] = "te";
                             $elementaux[1] = null;
                             $slotaux->slotstring[] = $elementaux;
                             $indexreceiver1 = $i;
                             $receiver1pron = true;
                         } else {
                             if ($slotaux->level == 2) {
                                 $slotaux->slotstring = array();
                                 $elementaux[0] = "te";
                                 $elementaux[1] = null;
                                 $slotaux->slotstring[] = $elementaux;
                                 $indexreceiver2 = $i;
                                 $receiver2pron = true;
                             }
                         }
                     }
                 }
             } else {
                 if ($slotaux->level == 1 && $mainverbinf || $slotaux->level == 1 && $ordre && !$this->frasenegativa) {
                     // si són pronoms personals, posem la forma correcta pels receivers de darrere el verb
                     if ($matching->isPronomPersES($slotaux->paraulafinal->text)) {
                         $slotaux->slotstring = array();
                         $elementaux[0] = $matching->pronomsPersonalsReceiverES[$slotaux->paraulafinal->text];
                         $elementaux[1] = null;
                         $slotaux->slotstring[] = $elementaux;
                         $indexreceiver1 = $i;
                         $receiver1pron = true;
                     }
                 } else {
                     if ($slotaux->level == 1) {
                         // si són pronoms personals, posem la forma correcta pels receivers d'abans del verb
                         if ($matching->isPronomPersES($slotaux->paraulafinal->text)) {
                             $slotaux->slotstring = array();
                             $elementaux[0] = $matching->pronomsPersonalsReceiverES[$slotaux->paraulafinal->text];
                             $elementaux[1] = null;
                             $slotaux->slotstring[] = $elementaux;
                             $indexreceiver1 = $i;
                             $receiver1pron = true;
                         }
                     } else {
                         if ($slotaux->level == 2 && $secondaryverbinf) {
                             // si són pronoms personals, posem la forma correcta pels receivers de darrere el verb
                             if ($matching->isPronomPersES($slotaux->paraulafinal->text)) {
                                 $slotaux->slotstring = array();
                                 $elementaux[0] = $matching->pronomsPersonalsReceiverES[$slotaux->paraulafinal->text];
                                 $elementaux[1] = null;
                                 $slotaux->slotstring[] = $elementaux;
                                 $indexreceiver2 = $i;
                                 $receiver2pron = true;
                             }
                         } else {
                             if ($slotaux->level == 2) {
                                 // si són pronoms personals, posem la forma correcta pels receivers d'abans del verb
                                 if ($matching->isPronomPersES($slotaux->paraulafinal->text)) {
                                     $slotaux->slotstring = array();
                                     $elementaux[0] = $matching->pronomsPersonalsReceiverES[$slotaux->paraulafinal->text];
                                     $elementaux[1] = null;
                                     $slotaux->slotstring[] = $elementaux;
                                     $indexreceiver2 = $i;
                                     $receiver2pron = true;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // Fi if slotaux = receiver
     }
     // Fi for transformar pronoms
     /*
      * ESTIC AQUÍ DEL CLEANER EN CASTELLÀ!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      */
     // ORDRE DELS PRONOMS
     // amb tota la info recollida, movem els pronoms de lloc si cal
     // pel main verb si és infinitiu (els d'ordre ja són a darrere)
     if ($mainverbinf) {
         // el theme 1
         if ($indextheme1 != -1) {
             $temp = $this->ordrefrase[$indextheme1];
             // esborrem el theme 1 per moure'l de lloc
             array_splice($this->ordrefrase, $indextheme1, 1);
             // l'insertem just després del main verb
             array_splice($this->ordrefrase, $indexmainverb, 0, $temp);
             $indextheme1 = $indexmainverb;
             $indexmainverb -= 1;
         }
         // el receiver 1
         if ($indexreceiver1 != -1) {
             $temp = $this->ordrefrase[$indexreceiver1];
             // esborrem el receiver 1 per moure'l de lloc
             array_splice($this->ordrefrase, $indexreceiver1, 1);
             // l'insertem just després del main verb
             array_splice($this->ordrefrase, $indexmainverb, 0, $temp);
             $indexreceiver1 = $indexmainverb;
             $indexmainverb -= 1;
         }
     }
     // pel verb secundari si és infinitiu
     if ($secondaryverbinf) {
         // el theme 2
         if ($indextheme2 != -1) {
             $temp = $this->ordrefrase[$indextheme2];
             // esborrem el theme 2 per moure'l de lloc
             array_splice($this->ordrefrase, $indextheme2, 1);
             // l'insertem just després del main verb
             array_splice($this->ordrefrase, $indexsecondaryverb, 0, $temp);
             $indextheme2 = $indexsecondaryverb;
             $indexsecondaryverb -= 1;
         }
         // el receiver 2
         if ($indexreceiver2 != -1) {
             $temp = $this->ordrefrase[$indexreceiver2];
             // esborrem el receiver 2 per moure'l de lloc
             array_splice($this->ordrefrase, $indexreceiver2, 1);
             // l'insertem just després del main verb
             array_splice($this->ordrefrase, $indexsecondaryverb, 0, $temp);
             $indexreceiver2 = $indexsecondaryverb;
             $indexsecondaryverb -= 1;
         }
     }
     // ORDRE MODIFICADORS DE FRASE QUE NO VAN AL PRINCIPI: "NO", "TAMBÉ"
     // agafem els modificadors, si n'hi ha
     $slotmainverb = $this->slotarray[$keymainverb];
     $counter = 0;
     $indexsmodifs = array();
     $janegatiu = false;
     if ($slotmainverb->CModassigned) {
         for ($i = 0; $i < count($slotmainverb->cmpMod); $i++) {
             $keymodifaux = $slotmainverb->CModassignedkey[$i];
             $slotmodifaux = $slotmainverb->cmpMod[$keymodifaux];
             // si és del grup que va darrere el subjecte (ex: no, también)
             if ($matching->isModAfterSubjES($slotmodifaux->paraulafinal->text)) {
                 // indiquem, que si la frase era negativa, ja no caldrà afegir el no
                 if ($slotmodifaux->paraulafinal->text == "no") {
                     $janegatiu = true;
                 }
                 // Creem un slot, el posem a slotarray i de moment al final d'ordrefrase
                 $counter += 1;
                 $newslotmodif = new Myslot();
                 $newslotmodif->category = "Modifier " . $counter;
                 $newslotmodif->grade = "opt";
                 $newslotmodif->type = "modif";
                 $newslotmodif->full = true;
                 $newslotmodif->paraulafinal = $slotmodifaux->paraulafinal;
                 $elementaux = array();
                 $elementaux[0] = $slotmodifaux->paraulafinal->text;
                 $elementaux[1] = $slotmodifaux->paraulafinal;
                 $newslotmodif->slotstring[] = $elementaux;
                 $newslotmodif->puntsfinal = 7;
                 $this->slotarray["Modifier " . $counter] = $newslotmodif;
                 $this->ordrefrase[] = "Modifier " . $counter;
                 $indexsmodifs[] = $numslots;
             }
         }
     }
     // afegim un slot amb el no si la frase era negativa i no l'hem afegit ja
     if ($this->frasenegativa && !$janegatiu) {
         // Creem un slot, el posem a slotarray i de moment al final d'ordrefrase
         $counter += 1;
         $newslotmodif = new Myslot();
         $newslotmodif->category = "Modifier " . $counter;
         $newslotmodif->grade = "opt";
         $newslotmodif->type = "modif";
         $newslotmodif->full = true;
         $elementaux = array();
         $elementaux[0] = "no";
         $elementaux[1] = null;
         $newslotmodif->slotstring[] = $elementaux;
         $newslotmodif->puntsfinal = 7;
         $this->slotarray["Modifier " . $counter] = $newslotmodif;
         $this->ordrefrase[] = "Modifier " . $counter;
         $indexsmodifs[] = $numslots;
     }
     // si hem trobat algun d'aquests slots, els col·loquem al lloc on toqui
     if ($counter > 0) {
         for ($i = 0; $i < count($indexsmodifs); $i++) {
             $indexmodifaux = $indexsmodifs[$i];
             // si hi ha un verb de desig, posar-los abans, amb els de permís van darrere del permís
             if ($indexdesire != -1) {
                 $indexaux = $indexdesire;
                 $temp = $this->ordrefrase[$indexmodifaux];
                 // esborrem el modif per moure'l de lloc
                 array_splice($this->ordrefrase, $indexmodifaux, 1);
                 // l'insertem just abans del desig
                 array_splice($this->ordrefrase, $indexaux, 0, $temp);
             } else {
                 if (!$mainverbinf && (!$ordre || $this->frasenegativa)) {
                     // si hi ha receiver, que sempre va abans del theme en versió pronominal
                     // el posem abans del receiver
                     if ($receiver1pron) {
                         $temp = $this->ordrefrase[$indexmodifaux];
                         // esborrem el modif per moure'l de lloc
                         array_splice($this->ordrefrase, $indexmodifaux, 1);
                         // l'insertem just abans del receiver
                         array_splice($this->ordrefrase, $indexreceiver1, 0, $temp);
                     } else {
                         if ($indextheme1 != -1) {
                             $temp = $this->ordrefrase[$indexmodifaux];
                             // esborrem el modif per moure'l de lloc
                             array_splice($this->ordrefrase, $indexmodifaux, 1);
                             // l'insertem just abans del theme
                             array_splice($this->ordrefrase, $indextheme1, 0, $temp);
                         } else {
                             $temp = $this->ordrefrase[$indexmodifaux];
                             // esborrem el modif per moure'l de lloc
                             array_splice($this->ordrefrase, $indexmodifaux, 1);
                             // l'insertem just abans del mainverb
                             array_splice($this->ordrefrase, $indexmainverb, 0, $temp);
                         }
                     }
                 } else {
                     $temp = $this->ordrefrase[$indexmodifaux];
                     // esborrem el modif per moure'l de lloc
                     array_splice($this->ordrefrase, $indexmodifaux, 1);
                     // l'insertem just abans del mainverb
                     array_splice($this->ordrefrase, $indexmainverb, 0, $temp);
                 }
             }
         }
     }
     // Fi si hi ha algun slot de modificador d'aquesta mena
     // AJUNTAR PREPS+ARTS / PRONOMS FEBLES I APÒSTROFS
     $frasebruta = $this->frasefinal . $this->printFraseFinalSlotString();
     // perps + arts
     $patterns[0] = '/[[:space:]][d][e][[:space:]][e][l][[:space:]]/u';
     $patterns[1] = '/[[:space:]][a][[:space:]][e][l][[:space:]]/u';
     // verb + pronoms de theme a darrere
     $patterns[2] = '/(?<=@VERBUM)[[:space:]]lo[[:space:]]/u';
     // verb + pronoms de receiver a darrere
     $patterns[3] = '/(?<=@VERBUM)[[:space:]]me[[:space:]]/u';
     $patterns[4] = '/(?<=@VERBUM)[[:space:]]te[[:space:]]/u';
     $patterns[5] = '/(?<=@VERBUM)[[:space:]]se[[:space:]]/u';
     $patterns[6] = '/(?<=@VERBUM)[[:space:]]nos[[:space:]]/u';
     $patterns[7] = '/(?<=@VERBUM)[[:space:]]os[[:space:]]/u';
     // verb+pronom feble de receiver ja enganxat, seguit de "lo"
     $patterns[8] = "/(?<=@VERBUM)me[[:space:]]lo[[:space:]]/u";
     $patterns[9] = "/(?<=@VERBUM)te[[:space:]]lo[[:space:]]/u";
     $patterns[10] = "/(?<=@VERBUM)se[[:space:]]lo[[:space:]]/u";
     $patterns[11] = "/(?<=@VERBUM)nos[[:space:]]lo[[:space:]]/u";
     $patterns[12] = "/(?<=@VERBUM)os[[:space:]]lo[[:space:]]/u";
     // netejar @VERBUM
     $patterns[13] = "/@VERBUM/u";
     $replacements[0] = ' del ';
     $replacements[1] = ' al ';
     $replacements[2] = "lo";
     $replacements[3] = "me ";
     $replacements[4] = "te ";
     $replacements[5] = "se ";
     $replacements[6] = "nos ";
     $replacements[7] = "os ";
     $replacements[8] = "melo ";
     $replacements[9] = "telo ";
     $replacements[10] = "selo ";
     $replacements[11] = "noslo ";
     $replacements[12] = "roslo ";
     $replacements[13] = "";
     $frasebruta = preg_replace($patterns, $replacements, $frasebruta);
     // fem una assignació prèvia
     $this->frasefinal = $frasebruta;
     // afegim les expressions de temps que van a darrere, si n'hi havia
     for ($i = 0; $i < count($this->timeexpr); $i++) {
         $wordaux = $this->timeexpr[$i];
         if (!$matching->isFrontAdvTempsES($wordaux->text)) {
             // el posem darrere de la frase
             $frasebruta .= $wordaux->text . " ";
         }
     }
     // afegim les expressions a darrere de la frase, si la frase no era buida,
     // afegim una coma abans
     $numexprs = count($this->exprsarray);
     if ($numexprs > 0 && $this->frasefinal != " ") {
         $frasebruta .= ", ";
         $frasebruta = preg_replace("/[[:space:]],/u", ",", $frasebruta);
     }
     for ($i = 0; $i < $numexprs; $i++) {
         // l'hola sempre va a davant
         if ($this->exprsarray[$i] == "hola") {
             $fraseaux = $frasebruta;
             $frasebruta = " " . $this->exprsarray[$i];
             // si la frase no era buida, afegim una coma
             if ($this->frasefinal != " ") {
                 $frasebruta = $frasebruta . ", " . $fraseaux;
             }
         } else {
             $frasebruta .= $this->exprsarray[$i] . " ";
         }
     }
     // POSAR ELS PUNTS O EXCLAMACIONS O INTERROGANTS
     // esborrar l'últim espai i una coma al final si hi és. També l'espai del principi
     $frasebruta = preg_replace("/[[:space:]]\$/u", "", $frasebruta);
     $frasebruta = preg_replace("/,\$/u", "", $frasebruta);
     $frasebruta = substr($frasebruta, 1);
     $frasebruta[0] = strtoupper($frasebruta[0]);
     $llargfrase = strlen($frasebruta);
     $lastcharacter = $frasebruta[$llargfrase - 1];
     if ($lastcharacter != "?") {
         if ($tipusfrase == "exclamacio") {
             $frasebruta .= "!";
             $frasebruta = "¡" . $frasebruta;
         } else {
             if ($tipusfrase == "pregunta" || $tipusfrase == "permis") {
                 $frasebruta .= "?";
                 $frasebruta = "¿" . $frasebruta;
             } else {
                 $frasebruta .= ".";
             }
         }
     }
     $this->frasefinal = $frasebruta;
     // DEBUG
     // echo $frasebruta.'<br /><br />';
 }