· 4 years ago · May 26, 2021, 03:56 PM
1
2from rasa.nlu.components import Component
3from rasa.nlu import utils
4from rasa.nlu.model import Metadata
5
6import requests
7import uuid
8from langid import classify
9
10class Translator(Component):
11
12 name = "Translator_component"
13 provides = ["text"]
14 requires = ["text"]
15 defaults = {}
16 language_list = ["en"]
17
18 def __init__(self, component_config=None):
19 super(Translator, self).__init__(component_config)
20
21 def check_language(self, intent: str) -> str:
22 """
23 Checks the language of user input
24 :param intent: string of any length
25 :return: str of predicted language
26 """
27 lang = classify(intent)[0]
28 return lang
29
30 def request_translation(self, user_text: str) -> str:
31 """
32 Invoking Microsoft API to get a translation for any non-English language to English language
33 :param user_text: string of any length, received from user
34 :return: string of translated into English text
35 """
36 lang = self.check_language(user_text)
37 if lang == 'en':
38 return user_text
39 # Add your subscription key and endpoint
40 subscription_key = "970983b6aea246128df9401059ae7c86"
41 endpoint = "https://api.cognitive.microsofttranslator.com"
42
43 # Add your location, also known as region. The default is global.
44 # This is required if using a Cognitive Services resource.
45 location = "westeurope"
46
47 path = '/translate'
48 constructed_url = endpoint + path
49
50 params = {
51 'api-version': '3.0',
52 'from': lang,
53 'to': 'en'
54 }
55 # constructed_url = endpoint + path
56
57 headers = {
58 'Ocp-Apim-Subscription-Key': subscription_key,
59 'Ocp-Apim-Subscription-Region': location,
60 'Content-type': 'application/json',
61 'X-ClientTraceId': str(uuid.uuid4())
62 }
63 # You can pass more than one object in body.
64 body = [dict(text=user_text)]
65 request = requests.post(constructed_url, params=params, headers=headers, json=body)
66 response = request.json()
67 translation = response[0]['translations'][0]['text']
68 return translation
69
70 def train(self, training_data, cfg, **kwargs):
71 """Not needed, because the the model is pretrained"""
72 pass
73
74 def process(self, message, **kwargs):
75 """Retrieve the text message, do spelling correction word by word,
76 then append all the words and form the sentence,
77 pass it to next component of pipeline"""
78 print(dir(message))
79 mt = message.text
80 new_message = self.request_translation(mt)
81 message.text = new_message
82
83 def persist(self, *args, **kwargs):
84 """Pass because a pre-trained model is already persisted"""
85 pass
86