AnnotationRepository.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Annotation;
  4. use App\Models\Word;
  5. use App\Models\Sentence;
  6. use Illuminate\Support\Facades\DB;
  7. class AnnotationRepository extends ResourceRepository {
  8. public function __construct(Annotation $annotation, Word $words, Sentence $sentence) {
  9. $this->annotation = $annotation;
  10. $this->words = $words;
  11. $this->sentence = $sentence;
  12. }
  13. private function save(Annotation $annotation, Array $inputs) {
  14. $annotation->user_id = $inputs['user_id'];
  15. $annotation->postag_id = $inputs['postag_id'];
  16. $annotation->word_id = $inputs['word_id'];
  17. $annotation->confidence_score = $inputs['confidence_score'];
  18. $annotation->save();
  19. }
  20. public function store(Array $inputs) {
  21. $annotation = new $this->annotation;
  22. $this->save($annotation, $inputs);
  23. return $annotation;
  24. }
  25. public function getByWordId($word_id) {
  26. return $this->game->where('word_id', $word_id)->first();
  27. }
  28. public function get_number_correct_annotations_on_reference($user_id) {
  29. return($this->annotation->select(DB::raw('count(word_id) as count'))
  30. ->join('words', 'word_id', '=', 'words.id')
  31. ->join('sentences', 'sentence_id', '=', 'sentences.id')
  32. ->join('corpora', 'corpus_id', '=', 'corpora.id')
  33. ->whereRaw("annotations.user_id=?
  34. AND corpora.is_training=true
  35. AND (word_id, annotations.postag_id) IN
  36. (select word_id, annotations.postag_id FROM annotations, words, sentences, corpora, users
  37. WHERE annotations.user_id=users.id
  38. AND users.id=92
  39. AND words.id=annotations.word_id
  40. AND sentences.id=words.sentence_id
  41. AND corpora.id=sentences.corpus_id
  42. AND corpora.is_training=true group by word_id order by annotations.confidence_score desc)
  43. ", Array($user_id))
  44. ->first());
  45. }
  46. public function get_number_annotations_on_reference($user_id) {
  47. /* */
  48. return($this->annotation->select(DB::raw('count(word_id) as count'))
  49. ->join('words', 'word_id', '=', 'words.id')
  50. ->join('sentences', 'sentence_id', '=', 'sentences.id')
  51. ->join('corpora', 'corpus_id', '=', 'corpora.id')
  52. ->whereRaw("annotations.user_id=?
  53. AND words.id=annotations.word_id
  54. AND sentences.id=words.sentence_id
  55. AND corpora.id=sentences.corpus_id
  56. AND corpora.is_training=true", Array($user_id))->first());
  57. }
  58. public function get_user_annotation_count($user_id) {
  59. /* returns number of annotations on all sentences */
  60. return $this->annotation->select(DB::raw('count(*) as annotation_count'))
  61. ->where('user_id', $user_id)->first();
  62. }
  63. public function get_users_scores_and_annotation_counts() {
  64. /* returns number of annotations on all sentences */
  65. return $this->annotation->select(DB::raw('name, score, count(*) as annotation_count'))->get();
  66. }
  67. public function get_total_non_admin_annotations() {
  68. return $this->annotation->select(DB::raw('count(*) as annotation_count'))
  69. ->join('users', 'user_id', '=', 'users.id')
  70. ->where('is_admin', false)
  71. ->first();
  72. }
  73. public function get_annotated_sentences_words($user_id) {
  74. return $this->annotation->select(DB::raw('count(*) as annotation_count'))
  75. ->where('user_id', $user_id)->first();
  76. }
  77. public function get_pretag_by_sentence_id($sentence_id) {
  78. $melt_tags_array = $this->get_pretag_by_sentence_and_tagger($sentence_id, "MElt");
  79. $treetagger_tags_array = $this->get_pretag_by_sentence_and_tagger($sentence_id, "TreeTagger");
  80. $collection_melt = collect();
  81. foreach ($melt_tags_array as $result) {
  82. $collection_melt->put($result->word_id, ['postag_name' => $result->postag_name, 'postag_id' => $result->postag_id]);
  83. }
  84. $melt_tags = $collection_melt->toArray();
  85. $collection_tt = collect();
  86. foreach ($treetagger_tags_array as $key => $result) {
  87. $collection_tt->put($result->word_id, ['postag_name' => $result->postag_name, 'postag_id' => $result->postag_id]);
  88. }
  89. $treetagger_tags = $collection_tt->toArray();
  90. debug("treetagger");
  91. debug($treetagger_tags);
  92. $collection_pretag = collect();
  93. foreach ($melt_tags as $key => $melt_tag) {
  94. if ($melt_tag == $treetagger_tags[$key]) {
  95. $collection_pretag->put($key, $melt_tag);
  96. } else {
  97. $collection_pretag->put($key, null);
  98. }
  99. }
  100. debug($collection_pretag);
  101. return $collection_pretag;
  102. }
  103. public function get_pretag_by_sentence_and_tagger($sentence_id, $tagger_name) {
  104. return $this->annotation->select(DB::raw('words.id as word_id, postags.id as postag_id, postags.name as postag_name'))
  105. ->join('words', 'words.id', '=', 'annotations.word_id')
  106. ->join('sentences', 'sentences.id', '=', 'words.sentence_id')
  107. ->join('postags', 'postags.id', '=', 'annotations.postag_id')
  108. ->where('sentence_id', '=', $sentence_id)
  109. ->where('annotations.tagger', 'like', $tagger_name)
  110. ->get();
  111. }
  112. public function get_unannotated_words($corpus_id) {
  113. return $this->words->select(DB::raw('count(words.id) as count'))
  114. ->join('sentences', 'sentences.id', '=', 'words.sentence_id')
  115. ->join('corpora', 'corpus_id', '=', 'corpora.id')
  116. ->where('corpus_id', '=', $corpus_id)
  117. ->whereNotIn('words.id', $this->get_annotated_words($corpus_id))
  118. ->first();
  119. }
  120. public function get_annotated_words($corpus_id) {
  121. return($this->annotation->select(DB::raw('words.id'))
  122. ->join('words', 'words.id', '=', 'annotations.word_id')
  123. ->join('sentences', 'sentences.id', '=', 'words.sentence_id')
  124. ->join('corpora', 'corpus_id', '=', 'corpora.id')
  125. ->where('annotations.confidence_score', '<', '10')
  126. ->where('corpus_id', '=', $corpus_id)
  127. // ->where('user_id', '>', '192')
  128. ->get());
  129. }
  130. public function get_distinct_annotated_words($corpus_id) {
  131. return($this->annotation->select(DB::raw('distinct(words.id)'))
  132. ->join('words', 'words.id', '=', 'annotations.word_id')
  133. ->join('sentences', 'sentences.id', '=', 'words.sentence_id')
  134. ->join('corpora', 'corpus_id', '=', 'corpora.id')
  135. ->where('annotations.confidence_score', '<', '10')
  136. ->where('corpus_id', '=', $corpus_id)
  137. ->where('user_id', '>', '192')
  138. ->get());
  139. }
  140. public function count_annotable_words($corpus_id) {
  141. return($this->words->select(DB::raw('count(words.id) as count'))
  142. ->join('sentences', 'sentences.id', '=', 'words.sentence_id')
  143. ->join('corpora', 'corpus_id', '=', 'corpora.id')
  144. ->where('corpus_id', '=', $corpus_id)
  145. ->first());
  146. }
  147. public function get_total_annotations() {
  148. return($this->annotation->select(DB::raw('count(annotations.id) as count'))
  149. ->where('user_id', '>', 202)
  150. ->first());
  151. }
  152. public function get_total_words_annotated() {
  153. return($this->annotation->select(DB::raw('count(distinct(annotations.word_id)) as count'))
  154. ->where('user_id', '>', 202)
  155. ->first());
  156. }
  157. public function get_total_annotations_not_reference() {
  158. return($this->annotation->select(DB::raw('count(annotations.id) as count'))
  159. ->join('words', 'words.id', '=', 'annotations.word_id')
  160. ->join('sentences', 'sentences.id', '=', 'words.sentence_id')
  161. ->join('corpora', 'corpus_id', '=', 'corpora.id')
  162. ->where('user_id', '>', 202)
  163. ->where('corpora.is_training', '=', false)
  164. ->first());
  165. }
  166. public function get_total_sentences_annotated_not_reference() {
  167. return($this->sentence->select(DB::raw('count(distinct(sentences.id)) as count'))
  168. ->join('words', 'words.sentence_id', '=', 'sentences.id')
  169. ->join('annotations', 'annotations.word_id', '=', 'words.id')
  170. ->join('corpora', 'corpora.id', '=', 'corpus_id')
  171. ->where('user_id', '>', 202)
  172. ->where('corpora.is_training', '=', false)
  173. ->first());
  174. }
  175. }