main.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. function semantic_search() {
  2. if ($("#search_query").val() != "") {
  3. $("#tag_list").empty();
  4. $("#wordcloud").empty();
  5. $("#results").empty();
  6. $("#add_search").empty();
  7. $("#wordcloud").append(`
  8. <div class="spinner-border text-primary" role="status">
  9. <span class="visually-hidden">Loading...</span>
  10. </div>`);
  11. top_k = parseInt($("#top_k").val());
  12. if (isNaN(top_k)) {
  13. top_k = 100;
  14. }
  15. similarity = parseFloat($("#similarity").val());
  16. if (isNaN(similarity)) {
  17. similarity = 0.0;
  18. }
  19. data = {
  20. query: $("#search_query").val(),
  21. top_k: top_k,
  22. similarity: similarity,
  23. start_date: $("#start_date").val(),
  24. end_date: $("#end_date").val(),
  25. };
  26. data = JSON.stringify(data);
  27. console.log(data);
  28. $.ajax({
  29. url: "/semantic_search",
  30. type: "post",
  31. dataType: "json", // 預期從 server 接收的資料型態
  32. contentType: "application/json; charset=utf-8", // 要送到 server 的資料型態
  33. data: data,
  34. })
  35. .done(function (data) {
  36. data = JSON.parse(data["semantic_search"]);
  37. $("#results").empty();
  38. $("#results").append(`
  39. <table class="table table-striped table-hover">
  40. <thead>
  41. <tr>
  42. <th>title</th>
  43. <th>content_tags</th>
  44. <th>descriptionFilterHtml</th>
  45. <th>masterUnit</th>
  46. <th>startDate</th>
  47. <th>endDate</th>
  48. </tr>
  49. </thead>
  50. <tbody></tbody>
  51. </table>`);
  52. $.each(data, function (indexInArray, valueOfElement) {
  53. $("tbody").append(`
  54. <tr>
  55. <td>${valueOfElement.title}</td>
  56. <td>
  57. <details>
  58. <summary>${
  59. valueOfElement.content_tags.slice(0, 10) + "..."
  60. }</summary>
  61. <p>${valueOfElement.content_tags}</p>
  62. </details>
  63. </td>
  64. <td>
  65. <details>
  66. <summary>${
  67. valueOfElement.descriptionFilterHtml.slice(0, 50) + "..."
  68. }
  69. </summary>
  70. <p>${valueOfElement.descriptionFilterHtml}</p>
  71. </details>
  72. </td>
  73. <td>${valueOfElement.masterUnit}</td>
  74. <td>${valueOfElement.startDate}</td>
  75. <td>${valueOfElement.endDate}</td>
  76. </tr>
  77. `);
  78. });
  79. $("table").DataTable({
  80. order: [],
  81. });
  82. })
  83. .fail(function () {
  84. console.log("error");
  85. });
  86. $.ajax({
  87. url: "/wordcloud",
  88. type: "post",
  89. dataType: "json", // 預期從server接收的資料型態
  90. contentType: "application/json; charset=utf-8", // 要送到server的資料型態
  91. data: data,
  92. })
  93. .done(function (data) {
  94. data = JSON.parse(data["wordcloud"]);
  95. $("#wordcloud").empty();
  96. $("#wordcloud").append(data);
  97. })
  98. .fail(function () {
  99. console.log("error");
  100. });
  101. $.ajax({
  102. url: "/tag_list",
  103. type: "post",
  104. dataType: "json", // 預期從server接收的資料型態
  105. contentType: "application/json; charset=utf-8", // 要送到server的資料型態
  106. data: data,
  107. })
  108. .done(function (data) {
  109. data = JSON.parse(data["tag_list"]);
  110. $("#tag_list").empty();
  111. if (data.length != 0) {
  112. $("#tag_list").append(`Top 100 Tags<br>`);
  113. $.each(data, function (indexInArray, valueOfElement) {
  114. $("#tag_list").append(`
  115. <span class="badge bg-primary">#${valueOfElement}&nbsp;</span>
  116. `);
  117. });
  118. }
  119. let contentList = [];
  120. let falseCount = 0;
  121. let len = (data.length>=5)? 5: data.length;
  122. for (i=0; i<len; i++){
  123. $("#add_search").append(`
  124. <button type="button" onclick="addValue('${data[i]}')" class="btn btn-outline-dark rounded-pill py-0 me-1">
  125. <div class="d-flex align-items-center">
  126. <span>+</span> <p class="m-0 pe-1">${data[i]}</p>
  127. </div>
  128. </button>`);
  129. }
  130. })
  131. .fail(function () {
  132. console.log("tag list error");
  133. });
  134. }
  135. }
  136. $("#search").on("click", function () {
  137. semantic_search();
  138. });
  139. $("#search_query").on("keyup", function (event) {
  140. // Number 13 is the "Enter" key on the keyboard
  141. if (event.keyCode === 13) {
  142. // Cancel the default action, if needed
  143. event.preventDefault();
  144. // Trigger the button element with a click
  145. $("#search").click();
  146. }
  147. });
  148. // 延遲取得輸入值
  149. function delayInput(fn, ms) {
  150. let timer = 0;
  151. return function (...args) {
  152. clearTimeout(timer);
  153. timer = setTimeout(fn.bind(this, ...args), ms || 0);
  154. };
  155. }
  156. // $("#search_query").keyup(
  157. // delayInput(function () {
  158. // console.log("Time elapsed!", this.value);
  159. // getSearchData(this.value);
  160. // }, 500)
  161. // );
  162. function getSearchData(params) {
  163. $.ajax({
  164. url: "static/data.json",
  165. type: "get",
  166. dataType: "json",
  167. contentType: "application/json; charset=utf-8",
  168. })
  169. .done((data) => {
  170. let contentList = [];
  171. let falseCount = 0;
  172. data.map((e) => {
  173. if (params !== "" && e.inputValue === params) {
  174. e.data.map((item) => {
  175. let content = `
  176. <button type="button" onclick="addValue('${item}')" class="btn btn-outline-dark rounded-pill py-0 me-1">
  177. <div class="d-flex align-items-center">
  178. <span>+</span> <p class="m-0 pe-1">${item}</p>
  179. </div>
  180. </button>`;
  181. contentList.push(content);
  182. });
  183. } else {
  184. // 錯誤計數
  185. falseCount++;
  186. }
  187. // 無符合結果則清空
  188. if (falseCount === e.data.length) {
  189. contentList = [];
  190. $(".add_search").html(contentList);
  191. } else {
  192. $(".add_search").html(contentList);
  193. }
  194. });
  195. })
  196. .fail((err) => {
  197. console.log("add search error", err);
  198. });
  199. }
  200. function addValue(params) {
  201. document.getElementById("search_query").value =
  202. document.getElementById("search_query").value + ` ${params}`;
  203. //$("#search").click();
  204. }