wbw_dict.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //var ApiWbwLookup = "./dict_find_one.php";
  2. var ApiWbwLookup = "/api/v2/wbwlookup";
  3. /**
  4. *
  5. * @param {array} words 要查询的单词数组
  6. * @param {function} callback
  7. */
  8. function dictFetch(words,callback=null){
  9. //将单词加入查询队列
  10. //0 已经查询完,1,正在查询 2,等待查询
  11. for (const word of words) {
  12. if (!isEmpty(word) && (typeof mDictQueue[word] == "undefined" || typeof mDict[word] == "undefined" )) {
  13. mDictQueue[word] = 2;
  14. }
  15. }
  16. for (const word in mDictQueue) {
  17. if (Object.hasOwnProperty.call(mDictQueue, word)) {
  18. //上次的查询尚未完成,退出
  19. if(mDictQueue[word] == 1) return;
  20. }
  21. }
  22. let wq = new Array();
  23. for (const word in mDictQueue) {
  24. if (Object.hasOwnProperty.call(mDictQueue, word)) {
  25. //上次的查询尚未完成,退出
  26. if(mDictQueue[word] == 2){
  27. wq.push(word);
  28. mDictQueue[word] = 1;
  29. }
  30. }
  31. }
  32. if (wq.length==0) {
  33. //队列为空,没有要查询的词。
  34. if(callback){
  35. callback();
  36. }
  37. } else {
  38. console.log('lookup',wq);
  39. //查询队列不为空,查字典
  40. gCurrLookupWord = words[0];
  41. $.ajax({
  42. type: "GET",
  43. url: ApiWbwLookup,
  44. dataType: "json",
  45. data: {
  46. word:wq.join(),
  47. },
  48. }).done(function (data) {
  49. if(data.ok){
  50. for (const word in mDictQueue) {
  51. if (Object.hasOwnProperty.call(mDictQueue, word)) {
  52. //设置正在查询的为已经查询过的
  53. if(mDictQueue[word] == 1){
  54. mDictQueue[word] = 0;
  55. }
  56. }
  57. }
  58. inline_dict_parse(data.data.rows);
  59. if(callback){
  60. callback();
  61. }
  62. }else{
  63. alert(data.message);
  64. }
  65. }).fail(function(jqXHR, textStatus, errorThrown){
  66. ntf_show(textStatus);
  67. switch (textStatus) {
  68. case "timeout":
  69. break;
  70. case "error":
  71. switch (jqXHR.status) {
  72. case 404:
  73. break;
  74. case 500:
  75. break;
  76. default:
  77. break;
  78. }
  79. break;
  80. case "abort":
  81. break;
  82. case "parsererror":
  83. console.log("parsererror",jqXHR.responseText);
  84. break;
  85. default:
  86. break;
  87. }
  88. });
  89. }
  90. }
  91. const db_name = "../../tmp/user/wbw.db3";
  92. //载入我的字典中的各位公式
  93. function load_my_formula() {
  94. //如果有内存字典里面没有的单词,查询
  95. console.log("load_my_formula - dict_find_one.php");
  96. $.get(
  97. ApiWbwLookup,
  98. {
  99. word: "_formula_",
  100. dict_name: db_name,
  101. deep: 1,
  102. },
  103. function (data, status) {
  104. try {
  105. myFormula = JSON.parse(data);
  106. } catch (e) {
  107. console.error(e.error);
  108. }
  109. }
  110. );
  111. }
  112. //解析字典数据
  113. function inline_dict_parse(data) {
  114. let worddata = data;
  115. if (worddata.length > 0) {
  116. //如果有数据 解析查询数据
  117. let spell = new Array();
  118. for (const iterator of worddata) {
  119. if (mDict[iterator.word]) {
  120. spell[iterator.word] = 1;
  121. } else {
  122. spell[iterator.word] = 0;
  123. }
  124. }
  125. for (const key in spell) {
  126. if (spell.hasOwnProperty(key)) {
  127. const element = spell[key];
  128. if (element == 0) {
  129. mDict[key] = new Array();
  130. }
  131. }
  132. }
  133. for (const iterator of worddata) {
  134. if (spell[iterator.word] == 0) {
  135. mDict[iterator.word].push(iterator);
  136. mDictQueue[iterator.word] = 0;
  137. }
  138. }
  139. let currWordParent = new Array();
  140. if (mDict[gCurrLookupWord]) {
  141. for (const iterator of mDict[gCurrLookupWord]) {
  142. if (typeof iterator.parent == "string") {
  143. if (iterator.parent.length > 1) {
  144. currWordParent[iterator.parent] = 1;
  145. }
  146. }
  147. }
  148. if (currWordParent.length == 0) {
  149. //
  150. inline_dict_auto_case(gCurrLookupWord);
  151. }
  152. } else {
  153. //如果没有查到数据 添加自动格位
  154. mDict[gCurrLookupWord] = new Array();
  155. inline_dict_auto_case(gCurrLookupWord);
  156. }
  157. } else {
  158. //如果没有查到数据 添加自动格位
  159. mDict[gCurrLookupWord] = new Array();
  160. inline_dict_auto_case(gCurrLookupWord);
  161. }
  162. }
  163. //添加自动格位数据到内存字典
  164. function inline_dict_auto_case(paliword) {
  165. for (const it of gCaseTable) {
  166. if (it.type != ".v.") {
  167. let sEnd2 = gCurrLookupWord.slice(0 - it.end2.length);
  168. if (sEnd2 == it.end2) {
  169. let wordParent = gCurrLookupWord.slice(0, 0 - it.end2.length) + it.end1;
  170. let newWord = new Object();
  171. newWord.pali = gCurrLookupWord;
  172. newWord.type = it.type;
  173. newWord.gramma = it.gramma;
  174. newWord.parent = wordParent;
  175. newWord.mean = "";
  176. newWord.note = "";
  177. newWord.parts = wordParent + "+[" + it.end2 + "]";
  178. newWord.partmean = "";
  179. newWord.confidence = it.confidence;
  180. mDict[paliword].push(newWord);
  181. }
  182. }
  183. }
  184. }
  185. function getAutoEnding(pali, base) {
  186. let ending = Array();
  187. for (let i in gCaseTable) {
  188. if (gCaseTable[i].type != ".v.") {
  189. let sEnd2 = pali.slice(0 - gCaseTable[i].end2.length);
  190. if (sEnd2 == gCaseTable[i].end2) {
  191. let wordParent = pali.slice(0, 0 - gCaseTable[i].end2.length) + gCaseTable[i].end1;
  192. if (base == wordParent) {
  193. ending[gCaseTable[i].end2] = 1;
  194. }
  195. }
  196. }
  197. }
  198. return ending;
  199. }
  200. //查字典结果
  201. function on_dict_lookup(data, status) {
  202. //解析查询数据
  203. inline_dict_parse(data);
  204. render_word_menu(_curr_mouse_enter_wordid);
  205. }
  206. //查词
  207. function lookupNewWord(param,callback){
  208. $.get(
  209. ApiWbwLookup,
  210. param,
  211. function (data, status) {
  212. try {
  213. var worddata = JSON.parse(data);
  214. } catch (e) {
  215. console.error(e.error);
  216. }
  217. if (worddata.length > 0) {
  218. var spell = new Array();
  219. for (var i in worddata) {
  220. if (mDict[worddata[i].pali]) {
  221. spell[worddata[i].pali] = 1;
  222. } else {
  223. spell[worddata[i].pali] = 0;
  224. }
  225. }
  226. for (var word in spell) {
  227. if (spell[word] == 0) {
  228. mDict[word] = new Array();
  229. }
  230. }
  231. for (var i in worddata) {
  232. if (spell[worddata[i].pali] == 0) {
  233. mDict[worddata[i].pali].push(worddata[i]);
  234. }
  235. }
  236. } else {
  237. }
  238. callback();
  239. }
  240. );
  241. }
  242. /**
  243. * 根据拆分,自动给出拆分意思
  244. * @param {string} factors
  245. * @returns
  246. */
  247. function getAutoFactorMeaning(factors){
  248. let fm = new Array();
  249. for (const part of factors.split('+')) {
  250. fm.push($.trim(findFirstPartMeanInDict(part)));
  251. }
  252. return fm.join('+');
  253. }
  254. //自动查词典
  255. var _para_list = new Array();
  256. function AutoLookup() {
  257. let book;
  258. let para = new Array();
  259. xBlock = gXmlBookDataBody.getElementsByTagName("block");
  260. for (const block of xBlock) {
  261. xmlParInfo = block.getElementsByTagName("info")[0];
  262. xmlParData = block.getElementsByTagName("data")[0];
  263. book = getNodeText(xmlParInfo, "book");
  264. paragraph = getNodeText(xmlParInfo, "paragraph");
  265. let xWord = block.getElementsByTagName("word");
  266. let words = [];
  267. for (const word of xWord) {
  268. let real = getNodeText(word,'real');
  269. let type = getNodeText(word,'type');
  270. if(real != '' && type != '.ctl.'){
  271. words.push(real);
  272. }
  273. }
  274. //不查询重复的段落
  275. para[book + "-" + paragraph] = { book: book, para: paragraph,word:words };
  276. }
  277. _para_list = new Array();
  278. for (const key in para) {
  279. if (Object.hasOwnProperty.call(para, key)) {
  280. const element = para[key];
  281. _para_list.push(element);
  282. }
  283. }
  284. if (_para_list.length > 0) {
  285. auto_lookup_wbw(0);
  286. }
  287. }
  288. //自动查词典
  289. function auto_lookup_wbw(para_index) {
  290. dictFetch(_para_list[para_index].word,function(){
  291. FillAllWord();
  292. //计算查字典的进度
  293. let precent = (para_index * 100) / (_para_list.length - 1);
  294. ntf_show(
  295. gLocal.gui.auto_fill +
  296. _para_list[para_index].book +
  297. "-" +
  298. _para_list[para_index].para +
  299. "-" +
  300. precent.toFixed(1) +
  301. "%" +
  302. gLocal.gui.finished
  303. );
  304. //查询下一个段落
  305. para_index++;
  306. if (para_index < _para_list.length) {
  307. auto_lookup_wbw(para_index);
  308. }
  309. });
  310. return;
  311. $.get(
  312. "./dict_find_auto.php",
  313. {
  314. book: _para_list[para_index].book,
  315. para: _para_list[para_index].para,
  316. },
  317. function (data, status) {
  318. if (data.length > 0) {
  319. var dict_data = new Array();
  320. try {
  321. dict_data = JSON.parse(data);
  322. } catch (error) {
  323. ntf_show("Error:" + error + "<br>" + data);
  324. }
  325. var counter = 0;
  326. var xAllWord = gXmlBookDataBody.getElementsByTagName("word");
  327. for (var x = 0; x < xAllWord.length; x++) {
  328. let wordStatus = getNodeText(xAllWord[x], "status");
  329. if (parseInt(wordStatus) > 3) {
  330. //忽略已经被用户修改的词
  331. continue;
  332. }
  333. let wid = getNodeText(xAllWord[x], "id");
  334. let aid = wid.split("-");
  335. let book = aid[0].substr(1);
  336. let para = aid[1];
  337. let num = aid[2];
  338. for (var i = 0; i < dict_data.length; i++) {
  339. if (dict_data[i].book == book && dict_data[i].paragraph == para && dict_data[i].num == num) {
  340. if (dict_data[i].type) {
  341. setNodeText(xAllWord[x], "type", dict_data[i].type);
  342. }
  343. if (dict_data[i].gramma) {
  344. setNodeText(xAllWord[x], "gramma", dict_data[i].gramma);
  345. }
  346. setNodeText(xAllWord[x], "case", dict_data[i].type + "#" + dict_data[i].gramma);
  347. if (dict_data[i].mean) {
  348. setNodeText(xAllWord[x], "mean", dict_data[i].mean);
  349. }
  350. if (dict_data[i].parent) {
  351. setNodeText(xAllWord[x], "parent", dict_data[i].parent);
  352. }
  353. if (dict_data[i].parts) {
  354. setNodeText(xAllWord[x], "org", dict_data[i].parts);
  355. }
  356. if (dict_data[i].partmean) {
  357. setNodeText(xAllWord[x], "om", dict_data[i].partmean);
  358. }
  359. setNodeText(xAllWord[x], "status", "3");
  360. counter++;
  361. modifyWordDetailByWordId(wid);
  362. user_wbw_push_word(wid);
  363. break;
  364. }
  365. }
  366. }
  367. user_wbw_commit();
  368. }
  369. //计算查字典的进度
  370. var precent = (para_index * 100) / (_para_list.length - 1);
  371. ntf_show(
  372. gLocal.gui.auto_fill +
  373. _para_list[para_index].book +
  374. "-" +
  375. _para_list[para_index].para +
  376. "-" +
  377. precent.toFixed(1) +
  378. "%" +
  379. gLocal.gui.finished
  380. );
  381. para_index++;
  382. if (para_index < _para_list.length) {
  383. auto_match_wbw(para_index);
  384. }
  385. }
  386. );
  387. }
  388. /**
  389. * 清空逐词译数据
  390. */
  391. function InitWbw(){
  392. let xWord = gXmlBookDataBody.getElementsByTagName("word");
  393. for (let index = 0; index < xWord.length; index++) {
  394. let word = xWord[index];
  395. setNodeText(word,'mean','');
  396. setNodeText(word,'org','');
  397. setNodeText(word,'om','');
  398. setNodeText(word,'parent','');
  399. setNodeText(word,'gramma','');
  400. setNodeText(word,'type','');
  401. setNodeText(word,'case','');
  402. setNodeText(word,'status',0);
  403. setNodeAttr(word,'org','status',0);
  404. setNodeAttr(word,'om','status',0);
  405. setNodeAttr(word,'parent','status',0);
  406. setNodeAttr(word,'gramma','status',0);
  407. setNodeAttr(word,'type','status',0);
  408. setNodeAttr(word,'case','status',0);
  409. let wid = getNodeText(word, "id");
  410. modifyWordDetailByWordId(wid);
  411. user_wbw_push_word(wid);
  412. }
  413. user_wbw_commit();
  414. return 'init wbw ' + xWord.length;
  415. }