-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(listener): add local intents via api
local actions for the speaker, such as bluetooth pair, pause, volume currently in English, Spanish and Catalan (few basic examples) this processing is done via API since it already loads into memory, otherwise running a new Python script would use more start time.
- Loading branch information
Showing
5 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from flask import Blueprint, request, jsonify | ||
import yaml | ||
import os | ||
|
||
intents = Blueprint('intents', __name__) | ||
|
||
intents_file = os.path.join(os.path.dirname(__file__), 'intents.yaml') | ||
intents_data = yaml.load(open(intents_file, 'r'), Loader=yaml.FullLoader) | ||
|
||
def remove_accents(input: str) -> str: | ||
characters = { | ||
'á': 'a', 'é': 'e', 'í': 'i', 'ó': 'o', 'ú': 'u', | ||
'à': 'a', 'è': 'e', 'ì': 'i', 'ò': 'o', 'ù': 'u', | ||
} | ||
|
||
for char, repl in characters.items(): | ||
input = input.replace(char, repl) | ||
return input | ||
|
||
@intents.post('/intent') | ||
def get_intents(): | ||
text = None | ||
if request.is_json: | ||
data = request.get_json() | ||
text = data.get('text', '').strip() | ||
else: | ||
text = request.form.get('text', '').strip() | ||
if not text: | ||
return jsonify({'error': 'No text provided'}), 400 | ||
|
||
accepts_json = request.headers.get('Accept') == 'application/json' | ||
|
||
text = text.lower() | ||
text = remove_accents(text) | ||
for char in ['.', ',', '?', '!', ':', ';']: | ||
text = text.replace(char, '') | ||
|
||
for word, replacements in intents_data.get('replaces', {}).items(): | ||
for replacement in replacements: | ||
if text == replacement: | ||
text = word | ||
break | ||
elif f' {replacement} ' in text: | ||
text = text.replace(f' {replacement} ', f' {word} ') | ||
elif f'{replacement} ' in text: | ||
text = text.replace(f'{replacement} ', f'{word} ') | ||
elif text.endswith(replacement): | ||
text = text[:len(text)-len(replacement)] + word | ||
|
||
for entry in intents_data['intents']: | ||
if text in entry['sentences']: | ||
if accepts_json: | ||
return jsonify({'intent': entry['action']}) | ||
return entry['action'] + '\n' | ||
if accepts_json: | ||
return jsonify({'intent': ''}), 404 | ||
return "", 404 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
replaces: | ||
bluetooth: | ||
- bluetooh | ||
- el blog | ||
- blog | ||
- brutus | ||
- bruto | ||
- lotus | ||
- amb l'atom | ||
desconecta: | ||
- desconec | ||
- desco | ||
- desconnect | ||
- desconnecte | ||
|
||
intents: | ||
- action: BluetoothPair | ||
sentences: | ||
- pair bluetooth | ||
- pair bluetooth device | ||
- connect bluetooth | ||
- connect bluetooth device | ||
- connect to my phone | ||
# -- | ||
- pareja bluetooth | ||
- en pareja bluetooth | ||
- empareja bluetooth | ||
- empareja dispositivo bluetooth | ||
- empareja mi movil | ||
- conecta bluetooth | ||
- conectate a mi movil | ||
- conecta dispositivo bluetooth | ||
- busca bluetooth | ||
- buscar bluetooth | ||
# -- | ||
- connectar bluetooth | ||
- emparella bluetooth | ||
- en parella bluetooth | ||
- amb parella bluetooth | ||
- action: BluetoothDisconnect | ||
sentences: | ||
- disconnect | ||
- disconnect bluetooth | ||
# -- | ||
- desconecta | ||
- desconectar | ||
- desconectate | ||
- desconecta bluetooth | ||
- desconectar bluetooth | ||
- desconecta mi movil | ||
# -- | ||
- desconnecta | ||
- desconnectar | ||
- desconnectat | ||
- action: Pause | ||
sentences: | ||
- pause | ||
- stop | ||
- pausa | ||
- pausar | ||
- paus | ||
- para | ||
- para la musica | ||
- action: Next | ||
sentences: | ||
- next | ||
- siguiente | ||
- siguiente canción | ||
- siguiente pista | ||
- pasa a la siguiente | ||
- action: VolumeUp | ||
sentences: | ||
- volume up | ||
- sube | ||
- sube volumen | ||
- subir | ||
- subir volumen | ||
- puja | ||
- puja volum | ||
- pujar | ||
- pujar volum | ||
- action: VolumeDown | ||
sentences: | ||
- volume down | ||
- baja | ||
- baja volumen | ||
- baja la voz | ||
- bajar | ||
- bajar volumen | ||
- baix | ||
- baixa | ||
- baixa volum | ||
- baixar | ||
- baixar volum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters