Detect the language of the specified text :return: String with the language of text or None
(text)
| 285 | |
| 286 | |
| 287 | def detect_language(text): |
| 288 | """ |
| 289 | Detect the language of the specified text |
| 290 | |
| 291 | :return: |
| 292 | String with the language of text or None |
| 293 | """ |
| 294 | |
| 295 | POST = "/api/{}/tr.json/detect?key={}&text={}".format( |
| 296 | YANDEX_API_VERSION, YANDEX_CONFIG["API_key"], text |
| 297 | ) |
| 298 | logger = Settings.logger |
| 299 | |
| 300 | try: |
| 301 | req = requests.get(YANDEX_HOST + POST) |
| 302 | except SSLError as exc: |
| 303 | print("") |
| 304 | logger.exception( |
| 305 | "{}\t~there was a connection error :<" |
| 306 | "\n{}\n".format(YANDEX_FAILURE_MSG, str(exc).encode("utf-8")) |
| 307 | ) |
| 308 | return None |
| 309 | |
| 310 | data = json.loads(req.text) |
| 311 | # check if there are any errors in the request |
| 312 | request_state = lift_yandex_request(data) |
| 313 | if request_state is not True: |
| 314 | return None |
| 315 | |
| 316 | # get the result |
| 317 | if "lang" in data.keys() and data["lang"]: |
| 318 | language_of_text = data["lang"] |
| 319 | return language_of_text |
| 320 | |
| 321 | else: |
| 322 | return None |
| 323 | |
| 324 | |
| 325 | def yandex_supported_languages(language_code="en"): |
no test coverage detected