-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastrodict.py
80 lines (77 loc) · 2.73 KB
/
astrodict.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
import re
import asyncio
import pandas as pd
async def get_astro_trans(content):
content = re.split(r'\s+', content.strip())
searchnum = 10
searchway = 'mohu'
if content[-1].isdigit():
searchnum = int(content[-1])
if content[-2] == 'pr':
searchway = 'jingque'
searchkey = content[:-2]
else:
searchkey = content[:-1]
elif content[-1] == 'pr':
searchway = 'jingque'
searchkey = content[:-1]
else:
searchkey = content
zhPattern = re.compile(r'^[\u4E00-\u9FFF]+$')
match = zhPattern.search(content[0])
if match:
direction = 'zh2en'
else:
direction = 'en2zh'
if direction == 'zh2en':
cedict = pd.read_csv('./astrodict/astrodict_191103ce.txt', sep='\t', header=None)
cedict.columns = ['C', 'E']
if searchway == 'mohu':
for i in searchkey:
pattern = r'('+i+')'
cedict = cedict[cedict['C'].str.contains(pattern)]
cedict.reset_index(inplace=True)
dictastro = {}
if len(cedict) < searchnum:
searchnum = len(cedict)
string = ''
for i in range(searchnum):
string += cedict['C'][i] +':' + cedict['E'][i] +'\n'
else:
i = ''.join(searchkey)
cedict = cedict.loc[cedict['C'] == i]
cedict.reset_index(inplace=True)
dictastro = {}
if len(cedict) < searchnum:
searchnum = len(cedict)
string = ''
for i in range(searchnum):
string += cedict['C'][i] +':' + cedict['E'][i] +'\n'
else:
ecdict = pd.read_csv('./astrodict/astrodict_191103ec.txt', sep='\t', header=None)
ecdict.columns = ['E', 'C']
if searchway == 'mohu':
for i in searchkey:
pattern = r'('+i+')'
ecdict = ecdict[ecdict['E'].str.contains(pattern)]
ecdict.reset_index(inplace=True)
dictastro = {}
if len(ecdict) < searchnum:
searchnum = len(ecdict)
string = ''
for i in range(searchnum):
string += ecdict['E'][i] +':' + ecdict['C'][i] +'\n'
else:
i = ''.join(searchkey)
ecdict = ecdict.loc[ecdict['E'] == i]
ecdict.reset_index(inplace=True)
dictastro = {}
if len(ecdict) < searchnum:
searchnum = len(ecdict)
string = ''
for i in range(searchnum):
string += ecdict['E'][i] +':' + ecdict['C'][i] +'\n'
if string:
return string[:-1]
else:
return '天文学词典中暂时还没收录相关词条。'