Skip to content

Commit ba7f012

Browse files
committed
Optimization
1 parent ace4146 commit ba7f012

File tree

10 files changed

+124
-147
lines changed

10 files changed

+124
-147
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ npm-debug.log
3131
testem.log
3232
/typings
3333
readme.txt
34+
package-lock.json
3435

3536
# e2e
3637
/e2e/*.js

Procfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

angular-cli.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@
5050
},
5151
"defaults": {
5252
"styleExt": "scss"
53+
},
54+
"warnings": {
55+
"typescriptMismatch": false
5356
}
5457
}

docs/.gitkeep

Whitespace-only changes.
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
<mat-card>
2-
<h1 class="mat-display-1">Angular 5 AI</h1>
2+
<h1 class="mat-display-1">Angular 5 AI</h1>
33

4-
<mat-card-content *ngFor="let message of allMessages | async">
5-
<div
6-
class="message"
7-
[ngClass]="{ 'from': message.sentBy === 'bot', 'to': message.sentBy === 'user' }">
8-
{{ message.content }}
9-
</div>
10-
</mat-card-content>
11-
12-
<div class="message-container">
13-
<mat-card-actions class="voice-button">
14-
<button mat-icon-button (click)="startTalkingToBot()">
15-
<mat-icon>keyboard_voice</mat-icon>
16-
</button>
17-
</mat-card-actions>
18-
<mat-input-container>
19-
<input matInput [(ngModel)]="formInput" min="2" placeholder="Your Message..." (keyup.enter)="sendMessageToBot()" type="text">
20-
</mat-input-container>
4+
<mat-card-content *ngFor="let message of allMessages | async">
5+
<div
6+
class="message"
7+
[ngClass]="{ 'from': message.sentBy === 'bot', 'to': message.sentBy === 'user' }">
8+
{{ message.content }}
219
</div>
10+
</mat-card-content>
2211

23-
<mat-card-actions class="button center">
24-
<button mat-raised-button (click)="sendMessageToBot()">Send</button>
12+
<div class="message-container">
13+
<mat-card-actions class="voice-button">
14+
<button mat-icon-button (click)="startTalkingToBot()">
15+
<mat-icon>keyboard_voice</mat-icon>
16+
</button>
2517
</mat-card-actions>
18+
<mat-input-container>
19+
<input matInput [(ngModel)]="formInput" min="2" placeholder="Your Message..." (keyup.enter)="sendMessageToBot()" type="text">
20+
</mat-input-container>
21+
</div>
22+
23+
<mat-card-actions class="button center">
24+
<button mat-raised-button (click)="sendMessageToBot()">Send</button>
25+
</mat-card-actions>
2626

2727
</mat-card>
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
@import "./src/assets/scss/base/variables";
22

33
.message {
4-
border-radius: 2px;
5-
line-height: 36px;
6-
margin: 0 15px 10px;
7-
padding: 15px 20px;
8-
vertical-align: baseline;
9-
position: relative;
10-
font-weight: bold;
11-
word-wrap: break-word;
4+
border-radius: 2px;
5+
line-height: 36px;
6+
margin: 0 15px 10px;
7+
padding: 15px 20px;
8+
vertical-align: baseline;
9+
position: relative;
10+
font-weight: bold;
11+
word-wrap: break-word;
1212
}
1313

1414
.message-container {
15-
display: flex;
16-
flex-direction: row;
17-
justify-content: center;
15+
display: flex;
16+
flex-direction: row;
17+
justify-content: center;
1818
}
1919

2020
.message.to {
21-
background-color: $darkblue;
22-
color: $white;
23-
margin: -10px 0 -10px 100px;
21+
background-color: $darkblue;
22+
color: $white;
23+
margin: -10px 0 -10px 100px;
2424
}
2525

2626
.message.from {
27-
background-color: $pink;
28-
color: $white;
29-
margin: -10px 100px -10px 0;
27+
background-color: $pink;
28+
color: $white;
29+
margin: -10px 100px -10px 0;
3030
}
3131

3232
.center {
33-
display: flex;
34-
justify-content: center;
33+
display: flex;
34+
justify-content: center;
3535
}
3636

3737
.mat-input-container {
38-
display: inline;
38+
display: inline;
3939
}
4040

4141
.mat-form-field {
42-
width: 100%;
42+
width: 100%;
4343
}
4444

4545
.mat-raised-button {
46-
background-color: $darkblue;
47-
color: $white;
46+
background-color: $darkblue;
47+
color: $white;
4848
}
4949

5050
.voice-button {
51-
border-radius: 50%;
52-
width: 40px;
53-
height: 40px;
54-
line-height: 24px;
55-
padding: 8px 20px 8px 8px;
56-
}
51+
border-radius: 50%;
52+
width: 40px;
53+
height: 40px;
54+
line-height: 24px;
55+
padding: 8px 20px 8px 8px;
56+
}

src/app/components/ai/ai.component.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ import { Message } from '../../model/message';
1414
export class AiComponent implements OnInit, OnDestroy {
1515
allMessages: Observable<Message[]>;
1616
formInput: string;
17-
18-
constructor(public ai: AiService) {
17+
18+
constructor(public ai: AiService) {
1919
this.formInput = '';
2020
}
21-
21+
2222
ngOnInit() {
2323
this.allMessages = this.ai.conversation.asObservable()
2424
.pipe(
2525
scan((acc, val) => acc.concat(val))
2626
)
2727
}
28-
28+
2929
ngOnDestroy() {
3030
this.ai.destroyVoiceConversation();
3131
}
32-
32+
3333
sendMessageToBot() {
3434
this.ai.textConversation(this.formInput);
3535
this.formInput = '';
3636
}
37-
37+
3838
startTalkingToBot() {
3939
this.ai.voiceConversation()
4040
.subscribe(
@@ -44,7 +44,7 @@ export class AiComponent implements OnInit, OnDestroy {
4444
},
4545
(err) => {
4646
console.log(err);
47-
if (err.error == "no-speech") {
47+
if (err.error === 'no-speech') {
4848
// console.log("Talking error");
4949
this.startTalkingToBot();
5050
}
@@ -55,4 +55,3 @@ export class AiComponent implements OnInit, OnDestroy {
5555
});
5656
}
5757
}
58-

src/app/interface/Iwindow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export interface IWindow extends Window {
22
webkitSpeechRecognition: any;
33
SpeechRecognition: any;
4-
}
4+
}

src/app/service/ai.service.ts

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable, NgZone } from '@angular/core';
22
import { environment } from '../../environments/environment';
3-
import * as lodash from "lodash";
3+
import * as lodash from 'lodash';
44

55
import { Observable } from 'rxjs/Observable';
66
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@@ -11,74 +11,67 @@ import { IWindow } from '../interface/iwindow'
1111

1212
@Injectable()
1313
export class AiService {
14-
readonly token = environment.dialogflow.angularAIBot;
15-
readonly client = new ApiAiClient({ accessToken: this.token });
16-
17-
speechRecognition: any;
18-
conversation = new BehaviorSubject<Message[]>([]);
19-
20-
constructor(private zone: NgZone) {
21-
}
22-
23-
textConversation(msg: string) {
24-
const userMessage = new Message(msg, 'user');
25-
this.update(userMessage);
26-
return this.client.textRequest(msg)
27-
.then(res => {
28-
const speech = res.result.fulfillment.speech;
29-
const botMessage = new Message(speech, 'bot');
30-
this.update(botMessage);
31-
});
32-
}
33-
34-
update(msg: Message) {
35-
this.conversation.next([msg]);
36-
}
37-
38-
voiceConversation(): Observable<string> {
39-
return Observable.create(observer => {
40-
const { webkitSpeechRecognition }: IWindow = <IWindow>window;
41-
this.speechRecognition = new webkitSpeechRecognition();
42-
this.speechRecognition.continuous = false;
43-
this.speechRecognition.interimResults = false;
44-
this.speechRecognition.lang = 'en-us';
45-
this.speechRecognition.maxAlternatives = 0;
46-
47-
this.speechRecognition.onresult = speech => {
48-
let sentence: string = "";
49-
if (speech.results) {
50-
var result = speech.results[speech.resultIndex];
51-
var transcript = result[0].transcript;
52-
if (result.isFinal) {
53-
if (result[0].confidence < 0.1) {
54-
// console.log("Unrecognized result - Please try again");
55-
}
56-
else {
57-
sentence = lodash.trim(transcript);
58-
// console.log("Did you said? -> " + sentence + " , If not then say something else...");
59-
}
60-
}
61-
}
62-
this.zone.run(() => {
63-
observer.next(sentence);
64-
});
65-
};
66-
67-
this.speechRecognition.onerror = error => {
68-
observer.error(error);
69-
};
70-
71-
this.speechRecognition.onend = () => {
72-
observer.complete();
73-
};
74-
75-
this.speechRecognition.start();
76-
// console.log("I'm listening...");
14+
readonly token = environment.dialogflow.angularAIBot;
15+
readonly client = new ApiAiClient({ accessToken: this.token });
16+
17+
speechRecognition: any;
18+
conversation = new BehaviorSubject<Message[]>([]);
19+
20+
constructor(private zone: NgZone) {
21+
}
22+
23+
update(msg: Message) {
24+
this.conversation.next([msg]);
25+
}
26+
27+
textConversation(msg: string) {
28+
const userMessage = new Message(msg, 'user');
29+
this.update(userMessage);
30+
return this.client.textRequest(msg)
31+
.then(res => {
32+
const speech = res.result.fulfillment.speech;
33+
const botMessage = new Message(speech, 'bot');
34+
this.update(botMessage);
35+
});
36+
}
37+
38+
voiceConversation(): Observable<string> {
39+
return Observable.create(observer => {
40+
const { webkitSpeechRecognition }: IWindow = <IWindow>window;
41+
this.speechRecognition = new webkitSpeechRecognition();
42+
this.speechRecognition.continuous = false;
43+
this.speechRecognition.interimResults = false;
44+
this.speechRecognition.lang = 'en-us';
45+
this.speechRecognition.maxAlternatives = 0;
46+
47+
this.speechRecognition.onresult = speech => {
48+
let sentence = '';
49+
if (speech.results) {
50+
const result = speech.results[speech.resultIndex];
51+
const transcript = result[0].transcript;
52+
if (result.isFinal) {
53+
result[0].confidence < 0.1 ? console.log('Unrecognized result') : sentence = lodash.trim(transcript);
54+
}
55+
}
56+
this.zone.run(() => {
57+
observer.next(sentence);
7758
});
78-
}
79-
80-
destroyVoiceConversation() {
81-
if (this.speechRecognition)
82-
this.speechRecognition.stop();
83-
}
59+
};
60+
61+
this.speechRecognition.onerror = error => {
62+
observer.error(error);
63+
};
64+
65+
this.speechRecognition.onend = () => {
66+
observer.complete();
67+
};
68+
69+
this.speechRecognition.start();
70+
// console.log('Listening...');
71+
});
72+
}
73+
74+
destroyVoiceConversation() {
75+
this.speechRecognition ? this.speechRecognition.stop() : this.speechRecognition.stop();
76+
}
8477
}

src/manifest.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)