test.py
3.65 KB
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
import time
import urllib
from urllib import parse
import json
import random,datetime
import numpy as np
import pandas
import os, requests, urllib
class AzureTranslatorClient:
def __init__(self, translate_key=None, translate_region=None, translate_endpoint=None):
self.translate_key = 'c10efac7aa6940c0ac1a6582e61aca73'
self.translate_region = 'southcentralus'
self.translate_endpoint = 'https://api.cognitive.microsofttranslator.com/'
self.api_version = "3.0"
# if os.getenv('VNET_DEPLOYMENT', 'false') == 'false':
self.detect_endpoint = urllib.parse.urljoin(self.translate_endpoint, f"/detect?api-version={self.api_version}")
self.translate_endpoint = urllib.parse.urljoin(self.translate_endpoint, f"/translate?api-version={self.api_version}")
# else:
# self.detect_endpoint = urllib.parse.urljoin(self.translate_endpoint, f"/translator/text/v3.0/detect?api-version={self.api_version}")
# self.translate_endpoint = urllib.parse.urljoin(self.translate_endpoint, f"/translator/text/v3.0/translate?api-version={self.api_version}")
def translate(self, question, text):
headers = {
'Ocp-Apim-Subscription-Key': self.translate_key,
'Ocp-Apim-Subscription-Region': self.translate_region,
'Content-type': 'application/json; charset=utf-8'
}
params = urllib.parse.urlencode({})
body = [{
'text': text
}]
body_question = [{
'text':question
}]
request_q = requests.post(self.detect_endpoint, params=params, headers=headers, json=body_question)
response = request_q.json()
to_language = response[0]['language']
request = requests.post(self.detect_endpoint, params=params, headers=headers, json=body)
response = request.json()
from_language = response[0]['language']
if (from_language != to_language):
params = urllib.parse.urlencode({
'from': from_language,
'to': to_language
})
body = [{
'text': text
}]
request = requests.post(self.translate_endpoint, params=params, headers=headers, json=body)
response = request.json()
return response[0]['translations'][0]['text']
else:
return text
AzureTranslatorClient = AzureTranslatorClient()
text = AzureTranslatorClient.translate(question="How to repair a ship",text='您可以在船坞中修复您的船只。在船坞中选择需要修复的船只,然后点击“修复”按钮。修复需要时间和资源,具体取决于船只的损坏程度。')
print(text)
# month = "2022-07"
# n = str.replace(month,'-','_')
# print(n)
# sn = "1700719944"
# actimeTime = "2022-12-01 00:00:00"
# startTime = "2022-12-01 00:00:00"
# startTime = int(datetime.datetime.strptime(startTime,"%Y-%m-%d %H:%M:%S").timestamp())
# actimeTime = int(datetime.datetime.strptime(actimeTime,"%Y-%m-%d %H:%M:%S").timestamp())
# endTime = "2022-12-31 23:59:59"
# endTime = int(datetime.datetime.strptime(endTime,"%Y-%m-%d %H:%M:%S").timestamp())
# loginCount = 64
# loginTime = list(map(lambda x:datetime.datetime.fromtimestamp(x).strftime("%Y-%m-%d %H:%M:%S"),np.random.randint(max(actimeTime,startTime),endTime,size=loginCount)))
# sn = [f"{sn}"]*loginCount
# df = pandas.DataFrame((zip(sn, loginTime)),columns=["sn","login"])
# print(df)
# df.to_sql()
# def opListName(str):
# return urllib.parse.quote(str)
# str = "%E8%90%A5%E9%94%80%E8%AF%9D%E6%9C%AF8.txt"
# l = []
# l.append(str)
# l.append(str)
# str = urllib.parse.unquote(str)
# print(str)
# ln = list(map(opList,l))
# print(ln)