This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
detect.py
124 lines (97 loc) · 3.33 KB
/
detect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Author: [email protected]
#
# A comman line script to detect the language of provided text.
#
# ml detect aztranslate [<text>]
#
# https://github.com/MicrosoftTranslator/Text-Translation-API-V3-Python
#
# ----------------------------------------------------------------------
# Import the required libraries.
# ----------------------------------------------------------------------
import os
import sys
import argparse
import requests
import uuid
import json
from mlhub.pkg import azkey
from mlhub.utils import get_cmd_cwd
# ----------------------------------------------------------------------
# Parse command line arguments: text
# ----------------------------------------------------------------------
option_parser = argparse.ArgumentParser(add_help=False)
option_parser.add_argument(
'text',
nargs="*",
help='text to translate')
option_parser.add_argument(
'--header',
action='store_true')
args = option_parser.parse_args()
# ----------------------------------------------------------------------
# Request subscription key and endpoint from user.
# ----------------------------------------------------------------------
SERVICE = "Text Translator"
KEY_FILE = os.path.join(os.getcwd(), "private.txt")
# Request.
key, endpoint = azkey(KEY_FILE, SERVICE, verbose=False, baseurl=True)
# ----------------------------------------------------------------------
# Build the REST API URLs.
# ----------------------------------------------------------------------
path = '/detect?api-version=3.0'
url = endpoint + path
headers = {
'Ocp-Apim-Subscription-Key': key,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# ------------------------------------------------------------------------
# Helper function.
# ------------------------------------------------------------------------
def helper(txt):
smpl = [{'text': txt}]
request = requests.post(url, headers=headers, json=smpl)
result = request.json()
sys.stdout.write(f"{result[0]['language']}," +
f"{result[0]['score']:0.2f}," +
f"{result[0]['isTranslationSupported']}," +
f"{result[0]['isTransliterationSupported']}" )
# ------------------------------------------------------------------------
# Translate text obtained from command line, pipe, or interactively.
# ------------------------------------------------------------------------
txt = " ".join(args.text)
fname = os.path.join(get_cmd_cwd(), txt)
if args.header: print("language,score,translate,transliterate")
if len(args.text) == 1 and os.path.isfile(fname):
with open(fname) as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for l in lines:
helper(l)
print()
elif txt != "":
helper(txt)
print()
elif not sys.stdin.isatty():
for txt in sys.stdin.readlines():
helper(txt)
else:
print("Enter text to be analysed. Quit with Empty or Ctrl-d.\n")
prompt = '> '
try:
txt = input(prompt)
except EOFError:
print()
sys.exit(0)
while txt != '':
helper(txt)
try:
print()
txt = input(prompt)
except EOFError:
print()
sys.exit(0)