-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocabulari.py
83 lines (65 loc) · 2.83 KB
/
vocabulari.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
# -*- coding: utf-8 -*-
#+--------------------------------------------------------------------------+#
# Importem mòduls #
#+--------------------------------------------------------------------------+#
from abc import ABC, abstractmethod
import numpy as np
import cv2
import pickle
#+--------------------------------------------------------------------------+#
# Definim les classes #
#+--------------------------------------------------------------------------+#
class Vocabulary(ABC):
def __init__(self, vocabulary):
self._vocabulary = vocabulary
@abstractmethod
def read(self, vocabulary_file):
raise NotImplementedError
@property
def vocabulary(self):
try:
return self._vocabulary
except:
raise AssertionError("ERROR: Vocabulary no llegit!")
class Txt_Vocabulary(Vocabulary):
def __init__(self):
super().__init__(vocabulary = np.array([]))
def read(self, vocabulary_file):
with open(vocabulary_file, "r") as vocabulary_file:
for paraula in vocabulary_file:
self._vocabulary = np.append(self._vocabulary, paraula[:-1])
class Img_Vocabulary(Vocabulary):
def __init__(self):
super().__init__(vocabulary = None)
def read(self, vocabulary_file):
sift = cv2.SIFT_create()
matcher = cv2.FlannBasedMatcher()
with open(vocabulary_file, 'rb') as fitxer:
vocabulary = pickle.load(fitxer)
bow_extractor = cv2.BOWImgDescriptorExtractor(sift, matcher)
bow_extractor.setVocabulary(vocabulary)
self._vocabulary = bow_extractor
class Tfidf_Vocabulary(Vocabulary):
def __init__(self, vocabulary_normal):
super().__init__(vocabulary = np.array([]))
self._vocabulary_normal = vocabulary_normal
self._vocabulary_dict = {}
def read(self, tipus, vocabulary_file):
if tipus == "text":
with open(vocabulary_file, "r") as vocabulary_file:
for i in vocabulary_file:
try:
self._vocabulary_dict[(i.split()[0])] = float(i.split()[1])
self._vocabulary = np.append(self._vocabulary, float(i.split()[1]))
except ValueError:
continue
else:
with open(vocabulary_file, "r") as vocabulary_file:
for paraula in vocabulary_file:
self._vocabulary = np.append(self._vocabulary,float(paraula))
@property
def vocabulary_normal(self):
return self._vocabulary_normal
@property
def vocabulary_dict(self):
return self._vocabulary_dict