PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/php/strip-vis/getsentence.php

https://bitbucket.org/silverasm/wordseer
PHP | 127 lines | 113 code | 7 blank | 7 comment | 14 complexity | 2c65257cb4c3b3f44b58bedd0be2d91b MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /* Copyright 2012 Aditi Muralidharan. See the file "LICENSE" for the full license governing this code. */
  3. /** Utilities and functions for getting a specific set of sentences
  4. */
  5. include_once '../util.php';
  6. include_once '../document/get-metadata.php';
  7. $wordseer_instance = getGetParam('instance');
  8. $path = '../../../instances/'.$wordseer_instance.'/config.php';
  9. include_once $path;
  10. /** Returns a the sentence with the given number in the given document
  11. */
  12. function getSentence($documentID, $number){
  13. $result = array();
  14. $query = "INSERT INTO filtered_sent_ids (id)
  15. SELECT id from sentence
  16. WHERE number <= ".($number)."
  17. AND number >= ".($number)."
  18. AND document_id =$documentID;";
  19. $results = mysql_query($query);
  20. $query = "SELECT * from sentence
  21. WHERE number <= ".($number)."
  22. AND number >= ".($number)."
  23. AND document_id =$documentID;";
  24. $results = mysql_query($query);
  25. $sentences = array();
  26. $gov_words = str_replace("\"", "", getGetParam('gov'));
  27. if ($govtype == 'word-set') {
  28. $gov_words = getWordsFromWordSet($gov_words);
  29. }
  30. $gov = explode(' ', strtolower($gov_words));
  31. $dep_words = str_replace("\"", "", getGetParam('dep'));
  32. if ($deptype == 'word-set') {
  33. $dep_words = getWordsFromWordSet($dep_words);
  34. }
  35. $dep = explode(' ', $dep_words);
  36. $result['govIndex'] = array(-1);
  37. $result['depIndex'] = -1;
  38. while($row = mysql_fetch_assoc($results)){
  39. $words = getWordsInSentence($row['id']);
  40. $sentences = array_merge($sentences, $words);
  41. if($row['number'] == $number){
  42. $result['sentenceID'] = $row['id'];
  43. $i = 0;
  44. foreach($words as $word){
  45. if(in_array(strtolower($word['word']), $gov)){
  46. array_push($result['govIndex'], $i);
  47. }
  48. if(in_array(strtolower($word['word']), $dep)){
  49. $result['depIndex'] = $i;
  50. }
  51. $i += 1;
  52. }
  53. }
  54. $result['documentID'] = $row['document_id'];
  55. }
  56. $result['words'] = $sentences;
  57. $result['metadata'] = getMetadataTreeFromSentenceIDs(
  58. array($result['sentenceID']), false);
  59. return $result;
  60. }
  61. //if used as a script
  62. $document = getGetParam('document');
  63. $numbers = getGetParam('numbers');
  64. if($document){
  65. $sentenceNumbers = explode(" ", $numbers);
  66. $sentences = array();
  67. foreach($sentenceNumbers as $number){
  68. $sent = getSentence($document, $number);
  69. array_push($sentences, $sent);
  70. }
  71. echo json_encode($sentences);
  72. }
  73. else if($_GET['id'] && $_GET['wordtree']){
  74. $id = getGetParam('id');
  75. $sql = "INSERT INTO filtered_sent_ids (id) VALUES ($id);";
  76. $result = mysql_query($sql);
  77. // get document information
  78. $ids = explode(" ", trim($_GET['id']));
  79. $info = getMetadataTreeFromSentenceIDs(array($id), false);
  80. if ($timing) {
  81. echo "<br>".json_encode($info)."<br><br>";
  82. }
  83. $query2 = "SELECT * FROM `sentence`
  84. WHERE `sentence`.`id`='$id' LIMIT 1";
  85. $result2 = mysql_query($query2) or die(mysql_error().' SQL error,
  86. <br/> at getsentence.php line 86 on query '.$query2);
  87. $row = mysql_fetch_assoc($result2);
  88. $sentence = iconv('UTF-8', 'UTF-8//IGNORE', $row['sentence']); //remove non-UTF-8 characters like smartquotes
  89. $to_encode = array(
  90. 'metadata'=>$info,
  91. 'sentence'=>$sentence,
  92. 'words'=> getWordsInSentence($id),
  93. 'sentence_id'=>$id,
  94. 'document_id'=> $row['document_id'],
  95. );
  96. echo json_encode($to_encode);
  97. }
  98. else if($_GET['ids']){
  99. $ids = explode(" ", trim($_GET['ids']));
  100. $sentences = array();
  101. for($i= 0; $i < count($ids); $i++){
  102. $html = "";
  103. $sql = 'SELECT * from sentence_xref_word WHERE sentence_id = '.$ids[$i].' order by position ASC;';
  104. $results = mysql_query($sql) or die(mysql_error().'<br>
  105. SQL error, getsentence.php line 86.On query:
  106. '.$sql);
  107. $previous = "'";
  108. while($row = mysql_fetch_array($results)){
  109. $word = $row['surface'];
  110. $wordID = $row['word_id'];
  111. $sentenceID = $row['sentence_id'];
  112. $space = str_replace("\n", "<br>", $row['space_after']);
  113. $html = $html.$space.'<span class="word" word-id="'.$wordID.'" sentence-id="'.$sentenceID.'">'.$word.'</span>';
  114. $previous = $word;
  115. }
  116. array_push($sentences, array('id'=>$id, 'sentence'=>$html));
  117. }
  118. echo json_encode($sentences);
  119. }
  120. ?>