-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2021-2-22-biology.py
304 lines (263 loc) · 8.33 KB
/
2021-2-22-biology.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import os
import pymongo
file_ch_dir = "D:/monetwaredata/ch"
file_en_dir = "D:/monetwaredata/en"
en_new_file = "D:/monetwaredata/en_new.txt"
files = os.listdir(file_ch_dir)
print(files)
files_dir = []
for file in files:
files_dir.append(file_ch_dir + '/' + file)
print(file_ch_dir + '/' + file)
file_name = files_dir[0]
file = open(file_name, 'r', encoding='utf-8')
line = file.readline()
while line:
line = file.readline()
def get_valid_src_database_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "SrcDatabase-来源库":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_title_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Title-题名":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_author_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Author-作者":
authors = []
authors_all = words[1].strip().split(';')
for author in authors_all:
if author != '':
authors.append(author)
return authors
else:
return []
else:
return []
def get_valid_organ_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Organ-单位":
organs = []
organs_all = words[1].strip().split(';')
for organ in organs_all:
if organ != '':
organs.append(organ)
return organs
else:
return []
else:
return []
def get_valid_source_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Source-文献来源":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_keyword_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Keyword-关键词":
keywords = []
keywords_all = words[1].strip().split(';;')
if len(keywords_all) == 1 and ";" in keywords_all[0]:
keywords_en_all = keywords_all[0].strip().split(';')
for keyword in keywords_en_all:
if keyword != '':
keywords.append(keyword)
return keywords
else:
for key_word in keywords_all:
if key_word != '':
keywords.append(key_word)
return keywords
else:
return []
else:
return []
def get_valid_summary_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Summary-摘要":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_pub_time_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "PubTime-发表时间":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_FirstDuty_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "FirstDuty-第一责任人":
organs = []
organs_all = words[1].strip().split(';')
for organ in organs_all:
if organ != '':
organs.append(organ)
return organs
else:
return []
else:
return []
def get_valid_year_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Year-年":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_period_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Period-期":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_pageCount_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "PageCount-页码":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_CLC_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "CLC-中图分类号":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_volume_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Volume-卷":
return words[1].strip()
else:
return ""
else:
return ""
def get_valid_Fund_data(target_string):
if ':' in target_string:
words = target_string.split(':')
if words[0] == "Fund-基金":
organs = []
organs_all = words[1].strip().split(';;')
for organ in organs_all:
if organ != '':
organs.append(organ)
return organs
else:
return []
else:
return []
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
file = open(en_new_file, 'r', encoding='utf-8')
lines = file.readlines()
position = []
# print(lines)
for i in range(len(lines)):
if is_number(lines[i].split(':')[0]):
position.append(i)
position.append(len(lines))
ch_biology = []
for i in range(len(position)-1):
start = position[i]
end = position[i + 1]
ch_dictionary = {}
for j in range(start + 1, end):
# print(lines[j].split(':'))
target = lines[j].split(':')[0]
ch_dictionary["type"] = "english"
if target == "SrcDatabase-来源库":
ch_dictionary["SrcDataBase"] = get_valid_src_database_data(lines[j])
if target == "Title-题名":
ch_dictionary["title"] = get_valid_title_data(lines[j])
if target == "Author-作者":
ch_dictionary["author"] = get_valid_author_data(lines[j])
if target == "Organ-单位":
ch_dictionary["organ"] = get_valid_organ_data(lines[j])
if target == "Source-文献来源":
ch_dictionary["source"] = get_valid_source_data(lines[j])
if target == "Keyword-关键词":
ch_dictionary["keyword"] = get_valid_keyword_data(lines[j])
if target == "Summary-摘要":
ch_dictionary["summary"] = get_valid_summary_data(lines[j])
if target == "PubTime-发表时间":
ch_dictionary["PubTime"] = get_valid_pub_time_data(lines[j])
if target == "FirstDuty-第一责任人":
ch_dictionary["FirstDuty"] = get_valid_FirstDuty_data(lines[j])
if target == "Fund-基金":
ch_dictionary["Fund"] = get_valid_Fund_data(lines[j])
if target == "Year-年":
ch_dictionary["Year"] = get_valid_year_data(lines[j])
if target == "Volume-卷":
ch_dictionary["volume"] = get_valid_volume_data(lines[j])
if target == "Period-期":
ch_dictionary["period"] = get_valid_period_data(lines[j])
if target == "PageCount-页码":
ch_dictionary["pageCount"] = get_valid_pageCount_data(lines[j])
if target == "CLC-中图分类号":
ch_dictionary["CLC-中国分类号"] = get_valid_CLC_data(lines[j])
ch_biology.append(ch_dictionary)
print(ch_biology)
client = pymongo.MongoClient("mongodb://192.168.1.142:27017/admin?connectTimeoutMS=10000&authSource=admin")
db = client["biology"]
collection = db["biology_data"]
collection.insert_many(ch_biology)
# while line:
# if ':' in line:
# target_value = line.split(':')
# target = target_value[0]
# if is_number(target):
#
#
#
#
#
# line = file.readline()
#
# a = get_valid_keyword_data("Keyword-关键词: Diatoms;;Silicon;;Photoluminescence;;Biosensing")
# print(a)