main.js 7.7 KB

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