-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_data.py
More file actions
48 lines (37 loc) · 1.23 KB
/
Copy pathinsert_data.py
File metadata and controls
48 lines (37 loc) · 1.23 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
import json
import time
from tqdm import tqdm
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'amcl.settings')
django.setup()
from annotation.models import Article
from annotation.utils.translate import BDT
# 插入数据
with open('samples.json', 'r', encoding='utf-8') as f:
target_articles = json.load(f)
db_articles = []
for article in target_articles:
db_articles.append(Article(**article, version=1))
Article.objects.bulk_create(db_articles)
# 利用百度翻译,翻译英文摘要
translator = BDT()
for article in tqdm(Article.objects.all()):
if article.transl_sentences is None:
try:
article.transl_sentences = json.dumps(
translator.translate('\n'.join(json.loads(article.sentences))), ensure_ascii=False)
article.save()
# 百度翻译API有请求频率限制
time.sleep(1)
except Exception as e:
print(e)
translator = BDT()
if article.transl_abstract is None:
try:
article.transl_abstract = translator.translate(article.abstract)[0]
article.save()
time.sleep(1)
except Exception as e:
print(e)
translator = BDT()