/
unit_disambiguator.py
36 lines (34 loc) · 1.67 KB
/
unit_disambiguator.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
from CQE.unit_classifier.train_classifier_bert import ambigious_units
from pathlib import Path
TOPDIR = Path(__file__).parent.parent.parent
import spacy
import operator
import os
def get_project_root() -> Path:
return Path(__file__).parent.parent
#######
#contains code for the disambiguator class, which is combination of bert classifiers
#given a surface form the class returns a correct classifer output using the .cats from the spacy transformer
#models should be located in data/units/unit_models/ for this class to work correctly
######
class unit_disambiguator():
def __init__(self):
self.models = {}
path = get_project_root()
for key, values in ambigious_units.items():
if key in ["C", "B", "P"]:
file_name = os.path.join(path, "unit_models/train_BIG" + key + ".spacy/model-best")
elif key=="¥":
file_name = os.path.join(path, "unit_models/train_yen.spacy/model-best")
elif key=="′":
file_name = os.path.join(path, "unit_models/train_ascii'.spacy/model-best")
elif key=="″":
file_name = os.path.join(path, "unit_models/train_ascii_doublequote.spacy/model-best")
elif key=="\"":
file_name = os.path.join(path, "unit_models/train_doublequote.spacy/model-best")
else:
file_name = os.path.join(path, "unit_models/train_" + key + ".spacy/model-best")
self.models[key] = spacy.load(file_name)
def disambiguate(self, sentence, surface_form):
probabilities = self.models[surface_form](sentence).cats
return max(probabilities.items(), key=operator.itemgetter(1))[0]