Skip to content

Commit b8a66f0

Browse files
committed
Cleaned up speech processor
1 parent 27ba410 commit b8a66f0

3 files changed

Lines changed: 157 additions & 162 deletions

File tree

textToCode/textToCode/JavaCode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class JavaCode: JavaExpression{
1313
private var exp: String;
1414

1515
init(exp: String) {
16-
print("new expression: \(exp)");
16+
/// print("new expression: \(exp)");
1717
self.exp = exp;
1818
}
1919

textToCode/textToCode/SpeechProcessor.swift

Lines changed: 136 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -36,160 +36,6 @@ class SpeechProcessor {
3636
"times": "*",
3737
]
3838

39-
static func processInput(result: String) -> NSMutableAttributedString{
40-
//Convert input into [String]
41-
let lowerCaseResult: String = result.lowercased();
42-
let fullResultArr = lowerCaseResult.components(separatedBy: " ");
43-
//Remove stop from the end of the input
44-
var resultArr = fullResultArr.prefix(fullResultArr.count - 1);
45-
for wordIndex in 0..<resultArr.count{
46-
//UNDO:
47-
if(resultArr[wordIndex] ~= "undo" && state.previousState != nil){
48-
state = state.previousState!;
49-
break;
50-
}
51-
state.updatePrevious()
52-
53-
//NEW CLASS: eg: "new private class dog stop "
54-
if(resultArr[wordIndex] ~= "new" && resultArr[wordIndex + 2].first ~= "c"){
55-
print("In new class");
56-
state.addClass(newClass: JavaClass.init(className: wordListToCamelCase(Array(resultArr[wordIndex + 3..<resultArr.count])).uppercasingFirst, vis: resultArr[wordIndex + 1]));
57-
break;
58-
}
59-
60-
//NEW METHOD: eg: "new public method kick returns boolean stop"
61-
if(resultArr[wordIndex] ~= "new" && resultArr[wordIndex + 2].first ~= "m"){
62-
print("In new method");
63-
state.currentClass?.addMethod(methodName: wordListToCamelCase(Array(resultArr[wordIndex + 3..<resultArr.count - 2])), vis: resultArr[wordIndex + 1], returnType: resultArr[resultArr.count - 1]);
64-
state.currentMethod = state.currentClass?.methods[(state.currentClass?.methods.count)! - 1];
65-
break;
66-
}
67-
68-
//NEW VAR: eg: "new private variable String leg stop"
69-
if(resultArr[wordIndex] ~= "new" && resultArr[wordIndex + 2].first ~= "v"){
70-
if state.currentMethod == nil{
71-
//NEW CLASS VAR: eg: "new private variable String leg"
72-
print("In new class var");
73-
state.currentClass?.addVar(varName: wordListToCamelCase(Array(resultArr[wordIndex + 4..<resultArr.count])), vis: resultArr[wordIndex + 1], type: resultArr[wordIndex + 3]);
74-
break;
75-
}
76-
}
77-
78-
if(resultArr[wordIndex] ~= "new" && resultArr[wordIndex + 1].first ~= "v"){
79-
if(resultArr.contains("equals")){
80-
//NEW METHOD VAR: eg: "new variable int size equals seven"
81-
print("in new method var");
82-
let equalsInt = findIndexOf(targetWord: "equals", phrase: Array(resultArr[wordIndex..<resultArr.count]))
83-
84-
let newVar: JavaExpVariables = JavaExpVariables.init(name: wordListToCamelCase(Array(resultArr[wordIndex + 3..<equalsInt])), type: resultArr[wordIndex + 2], value: scanPhrase(inputPhrase: Array(resultArr[equalsInt + 1..<resultArr.count]), isCondition: true).joined(separator: " "));
85-
state.currentMethod?.addExpression(exp: newVar);
86-
break;
87-
}else{
88-
//NEW method VAR: eg: "new variable String leg"
89-
print("In new method var");
90-
let newVar: JavaExpVariables = JavaExpVariables.init(name: wordListToCamelCase(Array(resultArr[wordIndex + 3..<resultArr.count])), type: resultArr[wordIndex + 2], value: nil);
91-
state.currentMethod?.addExpression(exp: newVar);
92-
break;
93-
}
94-
}
95-
96-
//while bye equals true stop
97-
//WHILE: eg: while bye equals true stop
98-
if(resultArr[wordIndex].first ~= "w"){
99-
print("In new while");
100-
let condition: String = scanPhrase(inputPhrase: Array(resultArr[wordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
101-
let newWhile: JavaWhile = JavaWhile.init(condition: condition)
102-
103-
state.currentMethod?.addExpression(exp: newWhile)
104-
break;
105-
}
106-
107-
//return bye stop
108-
//RETURN: eg: return bye stop
109-
if(resultArr[wordIndex].first ~= "r"){
110-
let returnVal: String = scanPhrase(inputPhrase: Array(resultArr[wordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
111-
let newReturn: JavaReturn = JavaReturn.init(returnString: returnVal);
112-
state.currentMethod?.addExpression(exp: newReturn);
113-
break;
114-
}
115-
116-
// if bye equals true stop
117-
// bye equals false stop
118-
// else stop
119-
// bye equals true stop
120-
121-
//IF: eg: see above^
122-
123-
if(resultArr[wordIndex].first ~= "i"){
124-
let condition: String = scanPhrase(inputPhrase: Array(resultArr[wordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
125-
let newIf: JavaIf = JavaIf.init(condition: condition);
126-
state.currentMethod?.addExpression(exp: newIf);
127-
break;
128-
}
129-
130-
//else
131-
if(resultArr[wordIndex] ~= "else" && resultArr.count == 1){
132-
// let condition: String = Array(resultArr[wordIndex + 1..<resultArr.count]).joined(separator: " ");
133-
let newElse: JavaElse = JavaElse.init();
134-
state.currentMethod?.addExpression(exp: newElse);
135-
break;
136-
}
137-
138-
//ELSE IF:
139-
if(resultArr[wordIndex] ~= "else" && resultArr[wordIndex + 1] ~= "if"){
140-
let condition: String = scanPhrase(inputPhrase: Array(resultArr[wordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
141-
let newElseIf: JavaElseIf = JavaElseIf.init(condition: condition);
142-
state.currentMethod?.addExpression(exp: newElseIf);
143-
break;
144-
}
145-
146-
//PRINT:
147-
if(resultArr[wordIndex] ~= "print"){
148-
let printStatement: String = scanPhrase(inputPhrase: Array(resultArr[wordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
149-
let newPrint: JavaPrint = JavaPrint.init(printStmt: printStatement);
150-
state.currentMethod?.addExpression(exp: newPrint);
151-
break;
152-
}
153-
154-
//COMMENTS:
155-
if(resultArr[wordIndex] ~= "comment"){
156-
let commentStatement: String = scanPhrase(inputPhrase: Array(resultArr[wordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
157-
let newComment: JavaComment = JavaComment.init(commentVal: commentStatement);
158-
state.currentMethod?.addExpression(exp: newComment);
159-
break;
160-
}
161-
162-
if(resultArr[wordIndex] ~= "go" && resultArr[wordIndex + 1] ~= "to" ){
163-
let name: String = wordListToCamelCase(scanPhrase(inputPhrase: Array(resultArr[wordIndex + 2..<resultArr.count]), isCondition: true));
164-
print(name);
165-
state.goto(name);
166-
break;
167-
}
168-
169-
if(resultArr[wordIndex] ~= "exit"){
170-
state.currentMethod?.exitExpression();
171-
break;
172-
}
173-
174-
if(resultArr[wordIndex] ~= "clear"){
175-
SpeechProcessor.state = JavaState()
176-
break;
177-
}
178-
179-
180-
else{
181-
let line: String = scanPhrase(inputPhrase: Array(resultArr[wordIndex..<resultArr.count]), isCondition: false).joined(separator: " ");
182-
let lineOfCode: JavaCode = JavaCode.init(exp: line)
183-
state.currentMethod?.addExpression(exp: lineOfCode);
184-
break;
185-
}
186-
187-
}
188-
189-
return state.toFormattingString();
190-
191-
}
192-
19339
static func wordListToCamelCase(_ words: [String]) -> String{
19440
return words.joined(separator: " ").camelized
19541
}
@@ -201,9 +47,8 @@ class SpeechProcessor {
20147
return -1;
20248
}
20349
}
204-
50+
20551
static func scanPhrase(inputPhrase: [String], isCondition: Bool) -> [String]{
206-
//print(inputPhrase);
20752
var phrase = inputPhrase;
20853
var removeWords: [Int] = [];
20954
var inQuote = false;
@@ -236,9 +81,140 @@ class SpeechProcessor {
23681
return phrase;
23782
}
23883

239-
//input like: new class fish; new private class head shoulders knees toes
240-
//TODO: make this? or don't? idk
241-
static func stringsToClass(_ : [String]) -> JavaClass {
242-
return JavaClass(className: "", vis: "public")
84+
static func otherInput(resultArr: ArraySlice<String>){
85+
let line: String = scanPhrase(inputPhrase: Array(resultArr[0..<resultArr.count]), isCondition: false).joined(separator: " ");
86+
let lineOfCode: JavaCode = JavaCode.init(exp: line)
87+
state.currentMethod?.addExpression(exp: lineOfCode);
88+
}
89+
90+
static func processInput(result: String) -> NSMutableAttributedString{
91+
//Convert input into [String]
92+
let lowerCaseResult: String = result.lowercased();
93+
let fullResultArr = lowerCaseResult.components(separatedBy: " ");
94+
//Remove stop from the end of the input
95+
var resultArr = fullResultArr.prefix(fullResultArr.count - 1);
96+
let firstWordIndex = 0;
97+
var foundInputMatch = false;
98+
99+
//UNDO:
100+
if(resultArr[firstWordIndex] ~= "undo" && state.previousState != nil){
101+
state = state.previousState!;
102+
foundInputMatch = true;
103+
}
104+
state.updatePrevious()
105+
106+
if(resultArr.count > 5 && !foundInputMatch){
107+
//NEW METHOD
108+
if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 2].first ~= "m"){
109+
state.currentClass?.addMethod(methodName: wordListToCamelCase(Array(resultArr[firstWordIndex + 3..<resultArr.count - 2])), vis: resultArr[firstWordIndex + 1], returnType: resultArr[resultArr.count - 1]);
110+
state.currentMethod = state.currentClass?.methods[(state.currentClass?.methods.count)! - 1];
111+
foundInputMatch = true;
112+
}
113+
}
114+
if(resultArr.count > 4 && !foundInputMatch){
115+
//NEW METHOD VARIABLE WITH A VALUE
116+
if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 1].first ~= "v" && resultArr.contains("equals")){
117+
let equalsInt = findIndexOf(targetWord: "equals", phrase: Array(resultArr[firstWordIndex..<resultArr.count]))
118+
119+
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: " "));
120+
state.currentMethod?.addExpression(exp: newVar);
121+
foundInputMatch = true;
122+
}
123+
}
124+
if(resultArr.count > 3 && !foundInputMatch){
125+
//NEW CLASS
126+
if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 2].first ~= "c"){
127+
state.addClass(newClass: JavaClass.init(className: wordListToCamelCase(Array(resultArr[firstWordIndex + 3..<resultArr.count])).uppercasingFirst, vis: resultArr[firstWordIndex + 1]))
128+
foundInputMatch = true;
129+
}
130+
//NEW CLASS VARIABLE
131+
else if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 2].first ~= "v" && state.currentMethod == nil){
132+
state.currentClass?.addVar(varName: wordListToCamelCase(Array(resultArr[firstWordIndex + 4..<resultArr.count])), vis: resultArr[firstWordIndex + 1], type: resultArr[firstWordIndex + 3]);
133+
foundInputMatch = true;
134+
}
135+
//NEW METHOD VARIABLE WITHOUT INITIAL VARIABLE
136+
else if(resultArr[firstWordIndex] ~= "new" && resultArr[firstWordIndex + 1].first ~= "v"){
137+
let newVar: JavaExpVariables = JavaExpVariables.init(name: wordListToCamelCase(Array(resultArr[firstWordIndex + 3..<resultArr.count])), type: resultArr[firstWordIndex + 2], value: nil);
138+
state.currentMethod?.addExpression(exp: newVar);
139+
foundInputMatch = true;
140+
}
141+
}
142+
if(resultArr.count > 2 && !foundInputMatch){
143+
//GOTO
144+
if(resultArr[firstWordIndex] ~= "go" && resultArr[firstWordIndex + 1] ~= "to" ){
145+
let name: String = wordListToCamelCase(scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 2..<resultArr.count]), isCondition: true));
146+
state.goto(name);
147+
foundInputMatch = true;
148+
}
149+
}
150+
if(resultArr.count > 1 && !foundInputMatch){
151+
//WHILE
152+
if(resultArr[firstWordIndex].first ~= "w"){
153+
let condition: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
154+
let newWhile: JavaWhile = JavaWhile.init(condition: condition)
155+
156+
state.currentMethod?.addExpression(exp: newWhile)
157+
foundInputMatch = true;
158+
}
159+
//RETURN
160+
else if(resultArr[firstWordIndex].first ~= "r"){
161+
let returnVal: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
162+
let newReturn: JavaReturn = JavaReturn.init(returnString: returnVal);
163+
state.currentMethod?.addExpression(exp: newReturn);
164+
foundInputMatch = true;
165+
}
166+
//IF
167+
else if(resultArr[firstWordIndex].first ~= "i"){
168+
let condition: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
169+
let newIf: JavaIf = JavaIf.init(condition: condition);
170+
state.currentMethod?.addExpression(exp: newIf);
171+
foundInputMatch = true;
172+
}
173+
//ELSE IF
174+
else if(resultArr[firstWordIndex] ~= "else" && resultArr[firstWordIndex + 1] ~= "if"){
175+
let condition: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
176+
let newElseIf: JavaElseIf = JavaElseIf.init(condition: condition);
177+
state.currentMethod?.addExpression(exp: newElseIf);
178+
foundInputMatch = true;
179+
}
180+
//PRINT
181+
else if(resultArr[firstWordIndex] ~= "print"){
182+
let printStatement: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
183+
let newPrint: JavaPrint = JavaPrint.init(printStmt: printStatement);
184+
state.currentMethod?.addExpression(exp: newPrint);
185+
foundInputMatch = true;
186+
}
187+
//COMMENTS
188+
else if(resultArr[firstWordIndex] ~= "comment"){
189+
let commentStatement: String = scanPhrase(inputPhrase: Array(resultArr[firstWordIndex + 1..<resultArr.count]), isCondition: true).joined(separator: " ");
190+
let newComment: JavaComment = JavaComment.init(commentVal: commentStatement);
191+
state.currentMethod?.addExpression(exp: newComment);
192+
foundInputMatch = true;
193+
}
194+
}
195+
if(!foundInputMatch){
196+
//ELSE
197+
if(resultArr[firstWordIndex] ~= "else"){
198+
let newElse: JavaElse = JavaElse.init();
199+
state.currentMethod?.addExpression(exp: newElse);
200+
foundInputMatch = true;
201+
}
202+
//EXIT
203+
else if(resultArr[firstWordIndex] ~= "exit"){
204+
state.currentMethod?.exitExpression();
205+
foundInputMatch = true;
206+
}
207+
//CLEAR
208+
else if(resultArr[firstWordIndex] ~= "clear"){
209+
SpeechProcessor.state = JavaState()
210+
foundInputMatch = true;
211+
}
212+
//NON-STRUCTURED INPUT
213+
else{
214+
otherInput(resultArr: resultArr);
215+
}
216+
}
217+
218+
return state.toFormattingString();
243219
}
244220
}

textToCode/textToCode/ViewController.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ViewController: UIViewController, SFSpeechRecognizerDelegate {
3535

3636
override func viewDidLoad() {
3737
super.viewDidLoad()
38+
testInput();
3839
recordButton.isEnabled = false
3940
speechRecognizer!.delegate = self
4041
SFSpeechRecognizer.requestAuthorization { (authStatus) in
@@ -58,7 +59,25 @@ class ViewController: UIViewController, SFSpeechRecognizerDelegate {
5859
}
5960
}
6061
}
61-
62+
63+
public func testInput(){
64+
sendInput(input: "New private class dog stop");
65+
sendInput(input: "New public method hello returns string stop");
66+
sendInput(input: "return \"hello how are you\" stop");
67+
sendInput(input: "new public method count legs returns integer stop");
68+
sendInput(input: "new variable integer number of legs equals seven plus five stop");
69+
sendInput(input: "while number of legs greater than 4 stop");
70+
sendInput(input: "numberOfLegs plus plus stop");
71+
72+
73+
sendInput(input: "print number of legs stop");
74+
75+
76+
}
77+
78+
private func sendInput(input: String){
79+
self.textOutput.attributedText = SpeechProcessor.processInput(result: input);
80+
}
6281

6382
@objc func tick(_ timer: Timer) {
6483
timeLeft -= 1;

0 commit comments

Comments
 (0)