From d55efff7d4624e2c0fa1d09fb983ceafd101c12a Mon Sep 17 00:00:00 2001 From: lzzfnc Date: Wed, 6 Oct 2021 17:53:14 +0200 Subject: [PATCH] trimmed stringg --- 0_speech/index.js | 2 +- 1_pythoning/1-2_NLTKing.py | 72 ++++++++++----------- speech/index.js | 127 ------------------------------------- 3 files changed, 34 insertions(+), 167 deletions(-) delete mode 100644 speech/index.js diff --git a/0_speech/index.js b/0_speech/index.js index ea36047..50918f8 100644 --- a/0_speech/index.js +++ b/0_speech/index.js @@ -106,7 +106,7 @@ function startConverting() { transcript.replace("\n", "
"); if (event.results[i].isFinal) { - finalTranscripts += transcript + "\n"; + finalTranscripts += transcript.trim() + "\n"; } else { // There are also shown the interim results and according to their "confidence" (the percentage of how much the word is correct) the color of each word could change interimTranscripts += transcript; diff --git a/1_pythoning/1-2_NLTKing.py b/1_pythoning/1-2_NLTKing.py index 2cd8101..df1d084 100644 --- a/1_pythoning/1-2_NLTKing.py +++ b/1_pythoning/1-2_NLTKing.py @@ -2,53 +2,51 @@ # NLTK (Natural Language ToolKit) is a library for Natural Language Process. -# We will use it to get the Part Of Speech (POS) of the speech-to-text results. -# +# We will use it to get the Part Of Speech (POS) of the speech-to-text results. +# # What does it mean? # # It works as grammar tagging: for instance, the sentence "Around the clouds" -# would have this output: -# +# would have this output: +# # [('Around', 'IN'), ('the', 'DT'), ('clouds', 'NN')] -# +# # 'IN' means 'preposition' - 'DT' means 'determiner' - 'NN' means 'noun, common, singular or mass' - -import time # to create delays :: for having a few seconds to check the console + import nltk # to use NLTK +# to create delays :: for having a few seconds to check the console +import time - # Open the speech-to-text result :: downloaded from the web interface >> +# Open the speech-to-text result :: downloaded from the web interface >> -with open('../speech.txt','r') as speech: # let's import the text +with open('../speech.txt', 'r') as speech: # let's import the text text = speech.read() # and make python read it :) print(text) # print it! time.sleep(2) # check it in the console! - -text = text.replace('','').replace('\n','. ') # delete this from the results + +text = text.replace('', + '').replace('\n', '. ') # delete this from the results tokens = nltk.word_tokenize(text) # Tokenize the words :: split each word -pos = nltk.pos_tag(tokens) # Elaborate the Part of Speech! It will create an array, a list +# Elaborate the Part of Speech! It will create an array, a list +pos = nltk.pos_tag(tokens) print(pos) # print the array! time.sleep(2) # check it in the console! +# To see all the POS tags, open the terminal and copy: +# +# python3 +# import nltk +# nltk.help.upenn_tagset() - - # To see all the POS tags, open the terminal and copy: - # - # python3 - # import nltk - # nltk.help.upenn_tagset() - - - - - # start the layouting :: html + css + paged.js >> - # - # declare html :: we will fill it in the process with loops - # declare the first part of the text for two html files with different CSS +# start the layouting :: html + css + paged.js >> +# +# declare html :: we will fill it in the process with loops +# declare the first part of the text for two html files with different CSS html = '' @@ -78,9 +76,8 @@ html2 = ''' ''' - - # Process each element of the list - +# Process each element of the list + for e in pos: # e is the current element, pos is the array to process if e[0] == '.': # if e is a dot, its class will be 'dot' @@ -88,23 +85,20 @@ for e in pos: # e is the current element, pos is the a else: # fill the html with each word and assign it as class its POS html += " "+e[0]+" \n" - - - # Close the html text + + # Close the html text html += ''' ''' -html = html.replace(' .','.').replace(" '", "'") # to tidy wrong " . " and " ' " position +# to tidy wrong " . " and " ' " position +html = html.replace(' .', '.').replace(" '", "'") - - # Save the files! +# Save the files! -with open('../2_layout/1.html','w') as index: +with open('../2_layout/1.html', 'w') as index: index.write(html1) index.write(html) -with open('../2_layout/2.html','w') as index: +with open('../2_layout/2.html', 'w') as index: index.write(html2) index.write(html) - - \ No newline at end of file diff --git a/speech/index.js b/speech/index.js deleted file mode 100644 index fd407a1..0000000 --- a/speech/index.js +++ /dev/null @@ -1,127 +0,0 @@ -// The Myth of the Natural Language %% -// Speech2Design!! - -// Welcome to the core-code of the Speech-to-text - -// The tool we are gonna use is the "Web Speech API". - -// What is an API? -// An API is a set of defined rules that explain how computers or applications communicate with one another. -// APIs sit between an application and the web server, acting as an intermediary layer that processes data transfer between systems. - - - -// GLOBAL VARIABLES - -let interimTranscripts = ""; // Variable for interim results, the Speech-to-text try different worlds before to give us the most correct one. -let allTheInterim = ""; // Variable to store *all* the interim results -let finalTranscripts = ""; // Variable for the... final transcripts - - // To define bridges to the html file: -let speech = document.getElementById("result"); // where to print the final result of the recognition -let process = document.getElementById("process"); // and here the process, print the current sentence with interim results - -// TEXT STORAGE - -let textStorage = localStorage.getItem("speech"); // This define where to save the results. -speech.innerHTML = textStorage; // LocalStorage is a type of web storage that allows you to access a local Storage object and store the data in the browser with no expiration date. - -// RESET STORAGE - -let resetStorage = document.getElementById("reset"); // This will just reset, through a button, all the results got by that moment. - -resetStorage.addEventListener("click", () => { // Reset everything!!! - allTheInterim = ""; - finalTranscripts = ""; - interimTranscripts = ""; - speech.innerHTML = ""; - textStorage = ""; - localStorage.setItem("speech", ""); -}); - -// SAVE FILE - -let saveButton = document.getElementById("save"); // This will let you save the results in your desktop through a button -saveButton.addEventListener("click", () => { - download("speech.html", localStorage.getItem("speech")); -}); - -function download(filename, text) { - var element = document.createElement("a"); - element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(text)); - element.setAttribute("download", filename); - - element.style.display = "none"; - document.body.appendChild(element); - - element.click(); - - document.body.removeChild(element); -} - -// START LISTENING - -startConverting(); // Finally, here is where the magic happen. - -function startConverting() { - if ("webkitSpeechRecognition" in window) { // Declaring here the API - let speechRecognizer = new webkitSpeechRecognition() || new SpeechRecognition(); - - // And here the settings, like - speechRecognizer.continuous = true; // if the recognition should continue or stop when you finish to talk - speechRecognizer.interimResults = true; // if you want also get the interim results - speechRecognizer.lang = "en-US"; // which language you want to recognize (!!) - speechRecognizer.start(); // and then start :)) - - finalTranscripts = ""; - - // EVENTS - - // ON END - speechRecognizer.onend = function () { // If the Speech-to-text stops to work, it will be notified in the console... - console.log("Speech recognition service disconnected"); - speechRecognizer.start(); // and then restart itself - }; - - // ON SOUND START - speechRecognizer.onsoundstart = function () { // When it starts the Speech-to-text, it will be notified in the console - console.log("Some sound is being received"); - }; - - // ON ERROR - speechRecognizer.onerror = function (event) {}; - - // ON RESULT - speechRecognizer.onresult = function (event) { // Here is where the Speech-to-text show itself on the web page. - interimTranscripts = ""; - - for (let i = event.resultIndex; i < event.results.length; i++) { - let transcript = event.results[i][0].transcript; - // console.log(event.results[i][0]); - transcript.replace("\n", "

"); - if (event.results[i].isFinal) { - finalTranscripts += transcript; - } else { // There are also shown the interim results and according to their "confidence" (the percentage of how much the word is correct) the color of each word could change - interimTranscripts += transcript; - allTheInterim += `${interimTranscripts} `; - } - } - process.innerHTML = allTheInterim; - let final = - finalTranscripts + '' + interimTranscripts + ""; - - speech.innerHTML = final; - - textStorage = final; - console.log(textStorage); - localStorage.setItem("speech", final); // Here is where is stored the recognized text in the Local Storage - }; - } else { // Unfortunately this API works only on Chrome... - speech.innerHTML = "At the moment this works only in Chrome, sorry"; - } -} - - -// 2021, copyleft Kamome and Funix \ No newline at end of file