|
1 | 1 | var recognition = new webkitSpeechRecognition();
|
2 | 2 | recognition.continuous = true;
|
3 | 3 | recognition.interimResults = true;
|
4 |
| -recognition.lang = 'en-IN'; //recognition locale |
| 4 | +recognition.lang = "en-IN"; //recognition locale |
5 | 5 | var recognizing = false; //is the app listening for voice?
|
6 | 6 | //data types array
|
7 |
| -var dataTypes = ['int', 'char', 'float', 'double', 'void']; |
| 7 | +var dataTypes = ["int", "char", "float", "double", "void"]; |
8 | 8 | //format specifiers for printf and scanf variables
|
9 |
| -var identifiers = {int:'d', float:'f', double:'lf', char:'c'}; |
| 9 | +var identifiers = { int: "d", float: "f", double: "lf", char: "c" }; |
| 10 | +var passArray = ["pass"]; |
10 | 11 | // translation dictionary for pronunciation correction for commonly misinterpreted words
|
11 |
| -var translationDictionary = {integer:'int', mean:'main', character:'char', '=':'equals', 'percent':'%', 'backslash':'\\', percentage:'%', line:'\\n'}; |
| 12 | +var translationDictionary = { |
| 13 | + integer: "int", |
| 14 | + mean: "main", |
| 15 | + character: "char", |
| 16 | + "=": "equals", |
| 17 | + percent: "%", |
| 18 | + backslash: "\\", |
| 19 | + percentage: "%", |
| 20 | + line: "\\n", |
| 21 | + cal : "call", |
| 22 | +}; |
12 | 23 | var programTextArea; //text area to write programs
|
13 | 24 | var includeStatements;
|
14 | 25 | var variables = {}; //object containing variables that the user declares
|
15 | 26 | var splitWords = []; //word array for recognition
|
16 | 27 | var indent = 0; //indentation level, increment when adding braces
|
17 | 28 | var doLoopCalled = 0; //To check if a do-while loop is called
|
18 | 29 |
|
19 |
| - |
20 |
| - |
21 | 30 | //handles voice recognition complete event (for one line)
|
22 |
| -recognition.onresult = function(event) { |
23 |
| - |
24 |
| - // split results into word array |
25 |
| - for (var i = event.resultIndex; i < event.results.length; i++) { |
26 |
| - if (event.results[event.results.length - 1].isFinal) { |
27 |
| - splitWords = event.results[event.results.length - 1][0].transcript.trim().split(' '); |
28 |
| - // console.log(splitWords); |
29 |
| - } |
30 |
| - } |
31 |
| - |
32 |
| - // translate apppropriate words using translation dictionary |
33 |
| - for (var i = 0; i < splitWords.length; i++) { |
34 |
| - if (translationDictionary[splitWords[i]] != undefined) { |
35 |
| - splitWords[i] = translationDictionary[splitWords[i]]; |
36 |
| - } |
| 31 | +recognition.onresult = function (event) { |
| 32 | + // split results into word array |
| 33 | + for (var i = event.resultIndex; i < event.results.length; i++) { |
| 34 | + if (event.results[event.results.length - 1].isFinal) { |
| 35 | + splitWords = event.results[event.results.length - 1][0].transcript |
| 36 | + .trim() |
| 37 | + .split(" "); |
| 38 | + console.log(splitWords); |
37 | 39 | }
|
| 40 | + } |
38 | 41 |
|
39 |
| - //call function according to command (first word(s) spoken) |
40 |
| - if (splitWords[0] === 'include') { |
41 |
| - includeLibrary(); |
42 |
| - } |
43 |
| - if (splitWords[0] == 'function') { |
44 |
| - newFunction(); |
45 |
| - } |
46 |
| - if (arrayContains(dataTypes, splitWords[0])) { |
47 |
| - newVariable(); |
48 |
| - } |
49 |
| - if (splitWords[0] === 'print') { |
50 |
| - printf(); |
51 |
| - } |
52 |
| - if (splitWords[0] === 'scan') { |
53 |
| - scanf(); |
54 |
| - } |
55 |
| - if (splitWords[0] === 'if') { |
56 |
| - ifStatement(); |
57 |
| - } |
58 |
| - if(splitWords[0] === 'while'){ |
59 |
| - whileLoop(); |
60 |
| - } |
61 |
| - if(splitWords[0]==='do'){ |
62 |
| - doLoop(); |
63 |
| - doLoopCalled=1; |
64 |
| - } |
65 |
| - if(splitWords[0]==='for'){ |
66 |
| - forLoop(); |
67 |
| - } |
68 |
| - if(splitWords[0]=='out'){ |
69 |
| - braceOut(); |
70 |
| - } |
71 |
| - if(splitWords[0]==='else' && splitWords[1] !== 'if'){ |
72 |
| - elseStatement(); |
73 |
| - } |
74 |
| - if(splitWords[0]==='else' && splitWords[1]==='if'){ |
75 |
| - elseIfStatement(); |
| 42 | + // translate apppropriate words using translation dictionary |
| 43 | + for (var i = 0; i < splitWords.length; i++) { |
| 44 | + if (translationDictionary[splitWords[i]] != undefined) { |
| 45 | + splitWords[i] = translationDictionary[splitWords[i]]; |
76 | 46 | }
|
| 47 | + } |
77 | 48 |
|
78 |
| - //reset transcript and word array for next line |
79 |
| - transcript = ''; |
80 |
| - splitWords = []; |
| 49 | + //call function according to command (first word(s) spoken) |
| 50 | + if (splitWords[0] === "include") { |
| 51 | + includeLibrary(); |
| 52 | + } |
| 53 | + if (splitWords[0] == "function") { |
| 54 | + newFunction(); |
| 55 | + } |
| 56 | + if (arrayContains(dataTypes, splitWords[0])) { |
| 57 | + newVariable(); |
| 58 | + } |
| 59 | + if (splitWords[0] === "print") { |
| 60 | + printf(); |
| 61 | + } |
| 62 | + if (splitWords[0] === "scan") { |
| 63 | + scanf(); |
| 64 | + } |
| 65 | + if (splitWords[0] === "if") { |
| 66 | + ifStatement(); |
| 67 | + } |
| 68 | + if (splitWords[0] === "while") { |
| 69 | + whileLoop(); |
| 70 | + } |
| 71 | + if (splitWords[0] === "do") { |
| 72 | + doLoop(); |
| 73 | + doLoopCalled = 1; |
| 74 | + } |
| 75 | + if (splitWords[0] === "for") { |
| 76 | + forLoop(); |
| 77 | + } |
| 78 | + if (splitWords[0] == "out") { |
| 79 | + braceOut(); |
| 80 | + } |
| 81 | + if (splitWords[0] === "else" && splitWords[1] !== "if") { |
| 82 | + elseStatement(); |
| 83 | + } |
| 84 | + if (splitWords[0] === "else" && splitWords[1] === "if") { |
| 85 | + elseIfStatement(); |
| 86 | + } |
| 87 | + if(splitWords[0]==='call'){ |
| 88 | + callFunctions(); |
| 89 | + } |
81 | 90 |
|
82 |
| -} |
| 91 | + //reset transcript and word array for next line |
| 92 | + transcript = ""; |
| 93 | + splitWords = []; |
| 94 | +}; |
0 commit comments