示例#1
0
 public static function dumpTagsToFile($filePath)
 {
     $wordAnnotationTypeModel = ClassRegistry::init('WordAnnotationType');
     $wordAnnotationTypes = $wordAnnotationTypeModel->find('all', array('order' => 'position'));
     $sentenceModel = ClassRegistry::init('Sentence');
     $sentenceModel->recursive = 3;
     $sentences = $sentenceModel->find('all');
     $outputFile = fopen($filePath, "w");
     for ($sentenceIndex = 0; $sentenceIndex < count($sentences); $sentenceIndex++) {
         $sentence = $sentences[$sentenceIndex];
         for ($wordIndex = 0; $wordIndex < count($sentence['Word']); $wordIndex++) {
             $wordData = array('Word' => array('id' => $sentence['Word'][$wordIndex]['id'], 'text' => $sentence['Word'][$wordIndex]['text'], 'stem' => $sentence['Word'][$wordIndex]['stem'], 'suffix' => $sentence['Word'][$wordIndex]['suffix'], 'split' => $sentence['Word'][$wordIndex]['split']), 'WordAnnotation' => $sentence['Word'][$wordIndex]['WordAnnotation']);
             $annotatedWord = new AnnotatedWord($wordData);
             $annotatedWordData = $annotatedWord->getSuggestionData($wordAnnotationTypes);
             fwrite($outputFile, $annotatedWordData['text']);
             if ($wordIndex < count($sentence['Word']) - 1) {
                 fwrite($outputFile, " ");
             }
         }
         if ($sentenceIndex < count($sentences) - 1) {
             fwrite($outputFile, "\n");
         }
     }
     fclose($outputFile);
 }
示例#2
0
 public function getSuggestions()
 {
     $this->autoRender = false;
     if ($this->request->is('post')) {
         $gridX = $this->request->data['gridX'];
         $wordId = $this->request->data['wordId'];
         $this->Word->recursive = 0;
         $word = $this->Word->findById($wordId);
         $identicalWords = array();
         //	        CakeLog::write('debug', 'Getting suggestions for word: '.print_r($word,true));
         $this->Word->recursive = 2;
         if ($word['Word']['split']) {
             if (isset($word['Word']['stem']) || isset($word['Word']['suffix'])) {
                 $identicalWords = $this->Word->find('all', array('conditions' => array('id !=' => $wordId, 'stem' => $word['Word']['stem'], 'suffix' => $word['Word']['suffix'])));
             }
         } else {
             if (isset($word['Word']['text'])) {
                 $identicalWords = $this->Word->find('all', array('conditions' => array('id !=' => $wordId, 'text' => $word['Word']['text'])));
             }
         }
         //	        CakeLog::write('debug', 'Identical words: '.print_r($identicalWords,true));
         $annotatedWords = array();
         foreach ($identicalWords as $identicalWord) {
             $annotatedWord = new AnnotatedWord($identicalWord);
             if ($annotatedWord->hasAnnotations() && !$this->arrayContainsAnnotation($annotatedWords, $annotatedWord)) {
                 $this->insertIntoWordsArray($annotatedWords, $annotatedWord);
             }
         }
         usort($annotatedWords, array($this, 'compareAnnotatedWords'));
         //  	        CakeLog::write('debug', 'Annotated words: '.print_r($annotatedWords,true));
         $wordAnnotationTypeModel = ClassRegistry::init('WordAnnotationType');
         $wordAnnotationTypes = $wordAnnotationTypeModel->find('all', array('order' => 'position'));
         $suggestionsData = array();
         $limit = 3;
         $awIndex = 0;
         $suggestionsIndex = 0;
         while ($awIndex < count($annotatedWords) && $suggestionsIndex < $limit) {
             array_push($suggestionsData, array('wordId' => $annotatedWords[$awIndex]['word']->getId(), 'suggestionCount' => $annotatedWords[$awIndex]['count'], 'suggestion' => $annotatedWords[$awIndex]['word']->getSuggestionData($wordAnnotationTypes)));
             $awIndex++;
             $suggestionsIndex++;
         }
         $acIndex = 0;
         $autocompleteWords = $this->getAutocompleteWords($wordId);
         while ($acIndex < count($autocompleteWords) && $suggestionsIndex < $limit) {
             array_push($suggestionsData, array('suggestionCount' => 0, 'suggestion' => $autocompleteWords[$acIndex]->getSuggestionData($wordAnnotationTypes)));
             $acIndex++;
             $suggestionsIndex++;
         }
         //        	CakeLog::write('debug', 'suggestionsData: '.print_r($suggestionsData, true));
         return json_encode(array("gridX" => $gridX, "count" => count($suggestionsData), "data" => $suggestionsData));
     }
 }