-
Notifications
You must be signed in to change notification settings - Fork 2
/
pipeline.py
280 lines (222 loc) · 9.28 KB
/
pipeline.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
"""pipeline logic and utils"""
import os
import sys
import logging
import uuid
import re
import json
import yaml
import jieba
from keybert import KeyBERT
from src import utils
from src import resources
from src import db
from src import programming_languages as prog_lang
from src import preprocessing as preprocess
from src import search_engine
# config should contain:
# - resources
# - jieba_dict
# - jieba_stop_words
# - puntuation_mark
# - programming_languages
# - models
# - keybert_model
#
# optional
# - output
# - pipeline_info
def load_pipeline_config(base_dir: str, config_path: str) -> dict:
"""
load pipeline config;
all resources in the config should be relative to base_dir (nlp root dir)
"""
# load raw config
with open(config_path, 'r', encoding='utf-8') as infile:
try:
raw_config = yaml.safe_load(infile)
except yaml.YAMLError as yaml_e:
logging.error(yaml_e)
sys.exit(1)
# relative path -> absolute path
for res_key in raw_config['resources']:
res_val = raw_config['resources'][res_key]
raw_config['resources'][res_key] = os.path.join(base_dir, res_val)
for out_key in raw_config['output']:
out_val = raw_config['output'][out_key]
raw_config['output'][out_key] = os.path.join(base_dir, out_val)
return raw_config
def init_pipeline_resources(config: dict) -> dict:
"""
load pipeline resource and resturn resource dict;
depends on the config load by load_pipeline_config
"""
# init jieba tokenizer
jieba_tokenizer = jieba.Tokenizer(dictionary=config['resources']['jieba_dict'])
# load stop word set
stop_word_file_list = [
config['resources']['jieba_stop_words'],
config['resources']['puntuation_mark'],
config['resources']['programming_languages']
]
stop_word_set = set()
for path in stop_word_file_list:
s_set = resources.load_stop_word_set(path)
stop_word_set |= s_set
# load keyword extraction model
logging.info("loading model %s...", config['models']['keybert_model'])
keybert_model = KeyBERT(model="paraphrase-multilingual-MiniLM-L12-v2")
# load programming language synonym json and build syn_map and inv_syn_map
prog_lang_syn_dict, prog_lang_inv_syn_dict = resources.load_syn_map(
config['resources']['programming_languages']
)
# build programming language matching regex pattern
prog_lang_re_pattern = prog_lang.build_programming_language_matching_regex_pattern(
prog_lang_syn_dict
)
# result
result_resources = {
"jieba_tokenizer": jieba_tokenizer,
"stop_word_set": stop_word_set,
"keybert_model": keybert_model,
"prog_lang_syn_dict": prog_lang_syn_dict,
"prog_lang_inv_syn_dict": prog_lang_inv_syn_dict,
"prog_lang_re_pattern": prog_lang_re_pattern
}
return result_resources
def pipeline_info_to_db_update_info(pipeline_info: dict) -> dict:
"""get db update json from pipeline result"""
db_update_info = {
"programming_languages": pipeline_info['programming_languages'],
"keywords_unigram": pipeline_info['extracted_keywords'],
"keywords_bigram": pipeline_info['extracted_keywords_phrases']
}
return db_update_info
def pipeline_info_to_docs_search_indexing_info(pipeline_info: dict) -> dict:
"""ms indexing json for search from pipeline result"""
hashtags = []
tags_string = re.sub(r'[\[\"\]]', '', pipeline_info['raw_tags_string'])
for h_tag in tags_string.split(','):
hashtags.append(h_tag.strip())
ms_indexing_info = {
'uuid': str(uuid.uuid4()),
'href': pipeline_info['href'],
'title': pipeline_info['title'],
'raw_hl_content': pipeline_info['content_text'],
'word_seg_processed_content': pipeline_info['word_seg_processed_content_text'],
'keywords': pipeline_info['extracted_keywords'],
'hashtags': hashtags,
'genre': pipeline_info['genre'],
'published_at': pipeline_info['published_at'],
'published_at_unix': utils.date_str_to_unix_time(
pipeline_info['published_at'],
"%Y-%m-%d %H:%M:%S"
),
'author_href': pipeline_info['author_href'],
'author_name': pipeline_info['author_name'].strip(),
'series_href': pipeline_info['series_href'],
'series_name': pipeline_info['series_name'],
'series_num': pipeline_info['series_num'],
'reading_time': utils.count_chinese_tech_article_reading_time(
pipeline_info['content_text'],
pipeline_info['content_code_info']
)
}
return ms_indexing_info
def pipeline_info_to_keywords_search_indexing_info_list(pipeline_info: dict) -> list:
"""ms indexing json for auto fill from pipeline result"""
ms_indexing_info_list = []
for kwp in pipeline_info['extracted_keywords_phrases']:
ms_indexing_info = {
'uuid': str(uuid.uuid4()),
'phrase': kwp
}
ms_indexing_info_list.append(ms_indexing_info)
return ms_indexing_info_list
def pipeline(config: dict, params: dict):
"""run nlp pipeline, update db and index search engine"""
logging.basicConfig(encoding='utf-8', level=params.get('loglevel', logging.INFO))
# load and init resources using config
pipeline_resources = init_pipeline_resources(config)
# pipeline process by batches
for batch_i, (head, rear) in enumerate(params['backlog_index_list']):
# load raw data from db
articles = db.get_articles(
api_server_url=params['api_server_url'],
offset=head,
limit=rear-head
)
# nlp pipeline
pipeline_info_list = []
for articles_i, pipeline_info in enumerate(articles):
if (articles_i % 10) == 0:
logging.info(
"Process %d, Batch: %d / %d, Task: %d / %d",
params.get('process_i', 0),
batch_i,
len(params['backlog_index_list']),
articles_i,
(rear-head)
)
# extract content text from html
extracted_info = preprocess.extract_content(
pipeline_info['content_html']
)
pipeline_info.update(extracted_info)
# extract code block and programming languages from content
extracted_info = preprocess.extract_code(
pipeline_info['content_html'],
pipeline_info['content_text'],
pipeline_resources['prog_lang_inv_syn_dict'],
pipeline_resources['prog_lang_re_pattern']
)
pipeline_info.update(extracted_info)
# preprocess content
extracted_info = preprocess.process_content_text(
pipeline_info['content_text'],
pipeline_resources['jieba_tokenizer'],
pipeline_resources['stop_word_set']
)
pipeline_info.update(extracted_info)
# keyword extraction
extracted_info = preprocess.keyword_extraction(
pipeline_resources['keybert_model'],
pipeline_info['word_seg_processed_content_text']
)
pipeline_info.update(extracted_info)
# append result
pipeline_info_list.append(pipeline_info)
# dump output
pipeline_info_out_path = config.get('output', {}).get('pipeline_info', None)
if pipeline_info_out_path:
pipeline_info_str_list = []
for pipeline_info in pipeline_info_list:
pipeline_info_str_list.append(
json.dumps(pipeline_info, ensure_ascii=False)
)
with open(pipeline_info_out_path, 'a') as out_file:
out_file.write('\n'.join(pipeline_info_str_list)+'\n')
# update db
for pipeline_info in pipeline_info_list:
db_update_info = pipeline_info_to_db_update_info(pipeline_info)
db.update_articles(
api_server_url=params['api_server_url'],
data=db_update_info,
primary_key_val=pipeline_info['href'],
)
# index search engine
docs_search_info_list = []
kws_search_info_list = []
for pipeline_info in pipeline_info_list:
docs_search_info_list.append(
pipeline_info_to_docs_search_indexing_info(pipeline_info)
)
kws_search_info_list += pipeline_info_to_keywords_search_indexing_info_list(
pipeline_info
)
search_engine.bulk_indexing_docs_search(
params['api_server_url'], docs_search_info_list
)
search_engine.bulk_index_keywords_search(
params['api_server_url'], kws_search_info_list
)