cv_parser / utils.py
kilachei's picture
Upload 11 files
01cf742 verified
raw
history blame contribute delete
No virus
3.57 kB
from imports import *
import unicodedata
dict_map = {
"òa": "oà",
"Òa": "Oà",
"ÒA": "OÀ",
"óa": "oá",
"Óa": "Oá",
"ÓA": "OÁ",
"ỏa": "oả",
"Ỏa": "Oả",
"ỎA": "OẢ",
"õa": "oã",
"Õa": "Oã",
"ÕA": "OÃ",
"ọa": "oạ",
"Ọa": "Oạ",
"ỌA": "OẠ",
"òe": "oè",
"Òe": "Oè",
"ÒE": "OÈ",
"óe": "oé",
"Óe": "Oé",
"ÓE": "OÉ",
"ỏe": "oẻ",
"Ỏe": "Oẻ",
"ỎE": "OẺ",
"õe": "oẽ",
"Õe": "Oẽ",
"ÕE": "OẼ",
"ọe": "oẹ",
"Ọe": "Oẹ",
"ỌE": "OẸ",
"ùy": "uỳ",
"Ùy": "Uỳ",
"ÙY": "UỲ",
"úy": "uý",
"Úy": "Uý",
"ÚY": "UÝ",
"ủy": "uỷ",
"Ủy": "Uỷ",
"ỦY": "UỶ",
"ũy": "uỹ",
"Ũy": "Uỹ",
"ŨY": "UỸ",
"ụy": "uỵ",
"Ụy": "Uỵ",
"ỤY": "UỴ",
}
### Normalize functions ###
def replace_all(text, dict_map=dict_map):
for i, j in dict_map.items():
text = unicodedata.normalize('NFC',str(text)).replace(i, j)
return text
def normalize(text, segment=True):
text = replace_all(text, dict_map)
if segment:
text = text.split(".")
text = ". ".join([underthesea.word_tokenize(i, format="text") for i in text])
return text
def text_preprocess(document):
punc = [i for i in ["\"", "-", ".", ":"]]#string.punctuation.replace(",","")]
stopword = [" thì ", " được ", " có ", " là "]
acronyms = {" wfh": " làm việc tại nhà ", " ot": " làm tăng ca ", " team": " nhóm ", " pm": " quản lý dự án ", " flexible": " linh động ",
" office": " văn phòng ", " feedback": " phản hồi ", " cty": " công ty ", " hr": " tuyển dụng ", " effective": " hiệu quả ",
" suggest": " gợi ý ", " hong": " không ", " ko": " không ", " vp": " văn phòng ", " plan ": " kế hoạch ", " planning": " lên kế hoạch ",
" family": " gia đình ", " leaders": " trưởng nhóm ", " leader": " trưởng nhóm ", ",": " , "}
document = re.sub(r"\n"," . ", document)
document = re.sub(r"\t"," ", document)
document = re.sub(r"\r","", document)
for p in punc:
document = document.replace(p," ")
for acr in acronyms:
tmp = [acr, acr.upper(), acr[0].upper()+acr[1:]]
for j in tmp:
document = re.sub(j, acronyms[acr], document)
#document = re.sub(j, acr.upper(), document)
for sw in stopword:
document = re.sub(sw, " ", document)
document = re.sub(" ", " ", document)
document = re.sub(" ", " ", document)
try:
document = document.split(".")
document = ". ".join([underthesea.word_tokenize(i, format="text") for i in document])
except:
pass
return document.lower()
### Compute metrics for multiclass classification problem
def compute_metrics(pred):
labels = pred.label_ids
preds = pred.predictions.argmax(-1)
f1 = f1_score(labels, preds, average="weighted")
acc = accuracy_score(labels, preds)
return {"accuracy": acc, "f1": f1}
### Make multilabel result from Ner result
# mb and cls_class just a dictionary map id to class name, see train.py
def convert2cls(data, mb, cls_class):
data = list(set(data))
try:
data.remove(20)
except:
pass
for i, num in enumerate(data):
if num>=10:
data[i] -= 10
data[i] = cls_class[data[i]]
data = mb.transform([data])[0]
return list(data)