-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeechProcessor.swift
More file actions
220 lines (206 loc) · 10.4 KB
/
SpeechProcessor.swift
File metadata and controls
220 lines (206 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// SpeechProcesser.swift
// textToCode
//
// Created by Michael Smith on 4/30/19.
// Copyright © 2019 Michael Smith. All rights reserved.
//
import Foundation
class SpeechProcessor {
static var state: JavaState = JavaState.init();
static let specialWords = ["equals": "=",
"plus": "+",
"minus": "-",
"divide": "/",
"divided": "/",
"open parenthesis": "(",
"close parenthiesis": ")",
"and": "&&",
"or": "||",
"less": "<",
"greater": ">",
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
"ten": "10",
"times": "*",
]
static func wordListToCamelCase(_ words: [String]) -> String{
return words.joined(separator: " ").camelized
}
static func findIndexOf(targetWord: String, phrase: [String]) -> Int{
if let index = phrase.firstIndex(of: targetWord){
return index
}else{
return -1;
}
}
static func scanPhrase(inputPhrase: [String], isCondition: Bool) -> [String]{
var phrase = inputPhrase;
var removeWords: [Int] = [];
var inQuote = false;
for i in 0..<phrase.count{
if(phrase[i].contains("\"")){
inQuote = !inQuote;
}else{
if(!inQuote){
if(phrase[i] == "equals" && isCondition){
phrase[i] = "==";
}else{
if((phrase[i] == "greater" || phrase[i] == "less") && phrase[i + 1] == "than"){
phrase[i] = specialWords[phrase[i]]!;
removeWords.append(i + 1);
}else{
let hasVal = specialWords[phrase[i]] != nil
if(hasVal){
phrase[i] = specialWords[phrase[i]]!;
}
}
}
}
}
}
removeWords = removeWords.reversed();
for num in removeWords{
phrase.remove(at: num);
}
return phrase;
}
static func otherInput(resultArr: ArraySlice<String>){
let line: String = scanPhrase(inputPhrase: Array(resultArr[0..<resultArr.count]), isCondition: false).joined(separator: " ");
let lineOfCode: JavaCode = JavaCode.init(exp: line)
state.currentMethod?.addExpression(exp: lineOfCode);
}
static func processInput(result: String) -> NSMutableAttributedString{
//Convert input into [String]
let lowerCaseResult: String = result.lowercased();
let fullResultArr = lowerCaseResult.components(separatedBy: " ");
//Remove stop from the end of the input
var resultArr = fullResultArr.prefix(fullResultArr.count - 1);
let firstWordIndex = 0;
var foundInputMatch = false;
//UNDO:
if(resultArr[firstWordIndex] ~= "undo" && state.previousState != nil){
state = state.previousState!;
foundInputMatch = true;
}
state.updatePrevious()
if(resultArr.count > 5 && !foundInputMatch){
//NEW METHOD
if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 2].first ~= "m"){
state.currentClass?.addMethod(methodName: wordListToCamelCase(Array(resultArr[firstWordIndex + 3..<resultArr.count - 2])), vis: resultArr[firstWordIndex + 1], returnType: resultArr[resultArr.count - 1]);
state.currentMethod = state.currentClass?.methods[(state.currentClass?.methods.count)! - 1];
foundInputMatch = true;
}
}
if(resultArr.count > 4 && !foundInputMatch){
//NEW METHOD VARIABLE WITH A VALUE
if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 1].first ~= "v" && resultArr.contains("equals")){
let equalsInt = findIndexOf(targetWord: "equals", phrase: Array(resultArr[firstWordIndex..<resultArr.count]))
let newVar: JavaExpVariables = JavaExpVariables.init(name: wordListToCamelCase(Array(resultArr[firstWordIndex + 3..<equalsInt])), type: resultArr[firstWordIndex + 2], value: scanPhrase(inputPhrase: Array(resultArr[equalsInt + 1..<resultArr.count]), isCondition: true).joined(separator: " "));
state.currentMethod?.addExpression(exp: newVar);
foundInputMatch = true;
}
}
if(resultArr.count > 3 && !foundInputMatch){
//NEW CLASS
if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 2].first ~= "c"){
state.addClass(newClass: JavaClass.init(className: wordListToCamelCase(Array(resultArr[firstWordIndex + 3..<resultArr.count])).uppercasingFirst, vis: resultArr[firstWordIndex + 1]))
foundInputMatch = true;
}
//NEW CLASS VARIABLE
else if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 2].first ~= "v" && state.currentMethod == nil){
state.currentClass?.addVar(varName: wordListToCamelCase(Array(resultArr[firstWordIndex + 4..<resultArr.count])), vis: resultArr[firstWordIndex + 1], type: resultArr[firstWordIndex + 3]);
foundInputMatch = true;
}
//NEW METHOD VARIABLE WITHOUT INITIAL VARIABLE
else if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 1].first ~= "v"){
let newVar: JavaExpVariables = JavaExpVariables.init(name: wordListToCamelCase(Array(resultArr[firstWordIndex + 3..<resultArr.count])), type: resultArr[firstWordIndex + 2], value: nil);
state.currentMethod?.addExpression(exp: newVar);
foundInputMatch = true;
}
}
if(resultArr.count > 2 && !foundInputMatch){
//GOTO
if(resultArr[firstWordIndex] ~= "go" && resultArr[firstWordIndex + 1] ~= "to" ){
let name: String = wordListToCamelCase(scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 2..<resultArr.count]), isCondition: true));
state.goto(name);
foundInputMatch = true;
}
}
if(resultArr.count > 1 && !foundInputMatch){
//WHILE
if(resultArr[firstWordIndex].first ~= "w"){
let condition: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
let newWhile: JavaWhile = JavaWhile.init(condition: condition)
state.currentMethod?.addExpression(exp: newWhile)
foundInputMatch = true;
}
//RETURN
else if(resultArr[firstWordIndex].first ~= "r"){
let returnVal: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
let newReturn: JavaReturn = JavaReturn.init(returnString: returnVal);
state.currentMethod?.addExpression(exp: newReturn);
foundInputMatch = true;
}
//IF
else if(resultArr[firstWordIndex].first ~= "i"){
let condition: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
let newIf: JavaIf = JavaIf.init(condition: condition);
state.currentMethod?.addExpression(exp: newIf);
foundInputMatch = true;
}
//ELSE IF
else if(resultArr[firstWordIndex] ~= "else" && resultArr[firstWordIndex + 1] ~= "if"){
let condition: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
let newElseIf: JavaElseIf = JavaElseIf.init(condition: condition);
state.currentMethod?.addExpression(exp: newElseIf);
foundInputMatch = true;
}
//PRINT
else if(resultArr[firstWordIndex] ~= "print"){
let printStatement: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
let newPrint: JavaPrint = JavaPrint.init(printStmt: printStatement);
state.currentMethod?.addExpression(exp: newPrint);
foundInputMatch = true;
}
//COMMENTS
else if(resultArr[firstWordIndex] ~= "comment"){
let commentStatement: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
let newComment: JavaComment = JavaComment.init(commentVal: commentStatement);
state.currentMethod?.addExpression(exp: newComment);
foundInputMatch = true;
}
}
if(!foundInputMatch){
//ELSE
if(resultArr[firstWordIndex] ~= "else"){
let newElse: JavaElse = JavaElse.init();
state.currentMethod?.addExpression(exp: newElse);
foundInputMatch = true;
}
//EXIT
else if(resultArr[firstWordIndex] ~= "exit"){
state.currentMethod?.exitExpression();
foundInputMatch = true;
}
//CLEAR
else if(resultArr[firstWordIndex] ~= "clear"){
SpeechProcessor.state = JavaState()
foundInputMatch = true;
}
//NON-STRUCTURED INPUT
else{
otherInput(resultArr: resultArr);
}
}
return state.toFormattingString();
}
}