Created
March 26, 2023 17:14
-
-
Save vpnry/ba3a60c84ae8737c081af161895e2a7c to your computer and use it in GitHub Desktop.
JavaScript algorithm/snippet to split concatenated words to word again according to dictionary input, get clicked word functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getClickedWord(e) { | |
| /** Generated with Bing AI ChatGPT */ | |
| var defiEl = document.getElementById("dictionary-res"); | |
| var tocDivBox = document.getElementById("tocDivBox"); | |
| /** Not getting words on these elements */ | |
| if ( | |
| !defiEl.contains(e.target) && | |
| !tocDivBox.contains(e.target) && | |
| !e.target.classList.contains("tocDivBox_a") | |
| ) { | |
| var node, offset; | |
| if (document.caretRangeFromPoint) { | |
| var range = document.caretRangeFromPoint(e.clientX, e.clientY); | |
| node = range.startContainer; | |
| offset = range.startOffset; | |
| } else if (document.caretPositionFromPoint) { | |
| var caretPos = document.caretPositionFromPoint(e.clientX, e.clientY); | |
| node = caretPos.offsetNode; | |
| offset = caretPos.offset; | |
| } | |
| var textNodeContent = node.textContent; | |
| var start = offset; | |
| var end = offset; | |
| while (start > 0 && /\S/.test(textNodeContent[start - 1])) { | |
| start--; | |
| } | |
| while (end < textNodeContent.length && /\S/.test(textNodeContent[end])) { | |
| end++; | |
| } | |
| var word = textNodeContent.slice(start, end); | |
| if (word) { | |
| word = word.toLowerCase(); | |
| // Remove all punctuation from word | |
| word = word.replace( | |
| new RegExp(`^[${puncSpecialChars}]+|[${puncSpecialChars}]+$`, "g"), | |
| "" | |
| ); | |
| return word.trim(); | |
| } | |
| } | |
| } | |
| function appendCloseMe() { | |
| function closeMe(e, head = false) { | |
| if (head) { | |
| e = document.getElementById("pnrydict"); | |
| } | |
| e.innerHTML = ""; | |
| e.style.display = "none"; | |
| } | |
| var head = document.getElementsByTagName("head")[0]; | |
| var script = document.createElement("script"); | |
| script.type = "text/javascript"; | |
| script.innerHTML = closeMe; | |
| head.appendChild(script); | |
| } | |
| Text to speech | |
| var TTS = "speechSynthesis" in window ? window.speechSynthesis : null; | |
| function SKS(word) { | |
| //Slow version | |
| if (!TTS) { | |
| console.log("TTS is not available in window"); | |
| return; | |
| } | |
| // stop the previous speech | |
| TTS.cancel(); | |
| let utterance = new SpeechSynthesisUtterance(word); | |
| utterance.lang = "en-GB"; //en-US, en-GB | |
| utterance.pitch = "0.8"; | |
| utterance.rate = "0.5"; | |
| TTS.speak(utterance); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function findStrFromSelection() { | |
| // Adapted from https://stackoverflow.com/a/70541520 | |
| if (!window.getSelection) { | |
| console.log("Not supported window.getSelection"); | |
| return; | |
| } | |
| var t = ""; | |
| var s = window.getSelection(); | |
| var range = s.getRangeAt(0); | |
| var node = s.anchorNode; | |
| var content = node.textContent; | |
| var startOffset = range.startOffset; | |
| var endOffset = range.endOffset; | |
| // Find starting point | |
| // We move the cursor back until we find a space a line break or the start of the node | |
| do { | |
| startOffset--; | |
| } while ( | |
| startOffset > 0 && | |
| content[startOffset - 1] != " " && | |
| content[startOffset - 1] != "\n" | |
| ); | |
| // Find ending point | |
| // We move the cursor forward until we find a space a line break or the end of the node | |
| do { | |
| endOffset++; | |
| } while ( | |
| content[endOffset] != " " && | |
| content[endOffset] != "\n" && | |
| endOffset < content.length | |
| ); | |
| t = content.substring(startOffset, endOffset); | |
| // Remove all punctuation from word | |
| t = t.replace(punRex, ""); | |
| return t; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const DICTOBJ {key: "value"} | |
| funtion wrapSection(text) { | |
| // format result | |
| return text; | |
| } | |
| function splitAndLookUp(text) { | |
| text = text.trim(); | |
| const rawInput = text; | |
| let syllables = segment(text); | |
| // filter punctuation, whitespace | |
| syllables = syllables | |
| .filter((element) => element.replace(punRex, "").trim().length > 0) | |
| .map((element) => element.trim()); | |
| let result = []; | |
| let wordList = []; | |
| let i = 0; | |
| while (i < syllables.length) { | |
| for (let j = syllables.length; j > i; j--) { | |
| let word = syllables.slice(i, j).join(""); | |
| if (word in DICTOBJ) { | |
| wordList.push(word); | |
| let x = `<p style='color:brown;'><b>${word}</b></p>${DICTOBJ[word]}`; | |
| x = wrapSection(x); | |
| result.push(x); | |
| i = j; | |
| break; | |
| } else { | |
| if (j === i + 1) { | |
| let s = syllables[i]; | |
| wordList.push(s); | |
| let x = `<p style='color:brown;'><b>${s}</b></p> ?`; | |
| x = wrapSection(x); | |
| result.push(x); | |
| i++; | |
| } | |
| } | |
| } | |
| } | |
| let split_list = | |
| `<span style="color:blue"><b>${rawInput}</b></span>` + | |
| `<br>=> (${wordList.length}): ` + | |
| wordList.join(" + ") + | |
| '<br><div style="text-align:center;">🤗</div>'; | |
| // console.log(result); | |
| let html = wrapSectionHead(split_list) + result.join(" "); | |
| return html; | |
| } | |
| function lookUpWord(input) { | |
| let i = input.length; | |
| if (DICTOBJ.hasOwnProperty(input)) { | |
| let x = `<p style='color:brown;'>💯 <b>${input}</b></p>${DICTOBJ[input]}`; | |
| x = wrapSection(x); | |
| return x; | |
| } | |
| return splitAndLookUp(input); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment