File size: 3,571 Bytes
632f4d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d38a62
 
632f4d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d38a62
 
632f4d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)