File size: 1,469 Bytes
c837b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import mojimoji
import pandas as pd
from rapidfuzz import fuzz, process


class EntityDictionary:

    def __init__(self, path):
        self.df = pd.read_csv(path)

    def get_candidates_list(self):
        return self.df.iloc[:, 0].to_list()

    def get_normalization_list(self):
        return self.df.iloc[:, 2].to_list()

    def get_normalized_term(self, term):
        return self.df[self.df.iloc[:, 0] == term].iloc[:, 2].item()


class DiseaseDict(EntityDictionary):

    def __init__(self):
        super().__init__('dictionaries/disease_dict.csv')


class DrugDict(EntityDictionary):

    def __init__(self):
        super().__init__('dictionaries/drug_dict.csv')


class EntityNormalizer:

    def __init__(self, database: EntityDictionary, matching_method=fuzz.ratio, matching_threshold=0):
        self.database = database
        self.matching_method = matching_method
        self.matching_threshold = matching_threshold
        self.candidates = [mojimoji.han_to_zen(x) for x in self.database.get_candidates_list()]

    def normalize(self, term):
        term = mojimoji.han_to_zen(term)
        preferred_candidate = process.extractOne(term, self.candidates, scorer=self.matching_method)
        score = preferred_candidate[1]

        if score > self.matching_threshold:
            ret = self.database.get_normalized_term(preferred_candidate[0])
            return ('' if pd.isna(ret) else ret), score
        else:
            return '', score