-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
238 lines (177 loc) · 6.55 KB
/
utils.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
import re
from os import path
import pandas as pd
from bs4 import BeautifulSoup
import json
import requests
from clients.postgres_client import PostgresClient
from settings import SARO_SKILL, SARO_PREFIXES, QUERY_EXECUTOR_URL, INDEX
def my_add(x, y):
"""
This functions adds x and y args
Args:
x: x parameter
y: y parameter
Returns:
x+y
Examples:
>>> my_add(1,2)
"""
return x + y
def create_meta_value(value):
"""
This function is used to to transform the value output of Dobie to meta data value
:param value: provided Dobie value
:return: metadata value
"""
extracted_value = value.replace("+", "plus")
split_value = extracted_value.split(" ")
if len(split_value) > 1:
processed_value = list(map(lambda word: word.title() if word != split_value[0] else word, split_value))
value_data = "".join(processed_value)
else:
value_data = extracted_value
meta_value = "".join(list(map(lambda char: "dot" if char == "." and char == value_data[0] else char, value_data)))
return meta_value
def parse_features(annotation):
"""
This function is used to to extract features from a provided annotation object
:param annotation: provided annotation object
:return: extracted features
"""
saro_skill = {}
features = annotation.select('Feature')
for feature in features:
name = feature.Name.text.capitalize()
value = feature.Value.text.capitalize()
if name == 'String':
value = value.lower()
meta_value = create_meta_value(value)
saro_skill["meta_value"] = meta_value
if name != "Frequencyofmention":
saro_skill[name] = value
features_dict = dict(filter(lambda element: element[1] != 'External', saro_skill.items()))
return features_dict
def parse_dobie_response(xml_response):
"""
The following function is used to get DOBIE responses and parses annotated tools
Args:
xml_response: DOBIE response
Returns:
extracted_skills: list of dict
>>> [{'string': 'CustomerSupport', 'frequencyOfMention': '1', 'kind': 'topic'}, {'string': 'Security', 'frequencyOfMention': '1', 'kind': 'topic'}]
Examples:
>>> parse_dobie_response(
'''<Annotation Id="0" Type="Split" StartNode="42" EndNode="42">
<Feature>
<Name className="java.lang.String">kind</Name>
<Value className="java.lang.String">external</Value>
</Feature>
</Annotation>'''
)
"""
soup = BeautifulSoup(xml_response, "xml")
annotations = soup.find_all('Annotation')
extracted_skills = []
for annotation in annotations:
features_dict = parse_features(annotation)
if features_dict:
extracted_skills.append(SARO_SKILL.format(**features_dict))
if extracted_skills:
extracted_saro_data = SARO_PREFIXES + "".join(extracted_skills)
else:
extracted_saro_data = []
return extracted_saro_data
def split_camel_case(input_string):
"""
This function is used to transform camel case words to more words
Args:
input_string: camel case string
Returns: Extracted words from camel case
"""
if len(input_string) > 5:
splitted = re.sub('([A-Z][a-z]+)', r' \1', re.sub('([A-Z]+)', r' \1', input_string)).split()
joined_string = " ".join(splitted)
return joined_string
else:
return input_string
def extract_raw_features(annotation):
"""
This function is used to extract raw features from annotations
:param annotation: annotation object
:return: features in dictionary
"""
features = annotation.select('Feature')
annotation_features = {}
for feature in features:
feature_name = feature.Name.text
feature_value = feature.Value.text
annotation_features[feature_name] = feature_value
features_dict = dict(filter(lambda element: element[1] != 'external', annotation_features.items()))
return features_dict
def handle_raw_annotation(dobie_output, job_name):
"""
This function is used to extract dobie annotation and return list of extracted skills
:param dobie_output: dobie response
:param job_name: provided job_name
:return: list of extracted skills
"""
postgres_client = PostgresClient()
soup = BeautifulSoup(dobie_output, "xml")
annotations = soup.find_all('Annotation')
extracted_skills = []
for annotation in annotations:
features_dict = extract_raw_features(annotation)
if features_dict and 'type' not in features_dict.keys():
features_dict['string'] = split_camel_case(features_dict['string'])
postgres_client.upsert_new_skill(
job_name=job_name,
skill=features_dict['string'],
frequencyOfMention=features_dict['frequencyOfMention'],
kind=features_dict['kind']
)
extracted_skills.append(features_dict)
return extracted_skills
def save_extracted_skills(skills, filename):
"""
This function is used to get extracted skills and save them to a csv
:param skills: provided skills
:param filename: given file name
:return:
"""
file_name = '{}.csv'.format(filename)
skills_df = pd.DataFrame(skills)
skills_df['frequencyOfMention'] = pd.to_numeric(skills_df['frequencyOfMention'])
sorted_skills = skills_df.sort_values(by='frequencyOfMention', ascending=False)
check_if_file_exists = path.isfile(file_name)
if check_if_file_exists:
sorted_skills.to_csv(file_name, mode='a', header=False)
else:
sorted_skills.to_csv(file_name)
def query_creator(job_attributes, key):
"""
This function is used to query Analyzer for job posts
:param job_attributes: provided job attributes
:param key: provided key
:return: returned job post ids
"""
data = {
"query": "bool_query",
"index": INDEX,
"min_score": job_attributes['min_score'],
"_source": ["id"],
"should": [
{"multi_match": {
"query": query,
"fields": ["title", "requirements"],
"type": "phrase",
"slop": 2}
} for query in job_attributes['queries']
]
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(url=QUERY_EXECUTOR_URL, headers=headers, data=json.dumps(data))
job_post_ids = [res['_source']['id'] for res in response.json()]
return job_post_ids