-
Notifications
You must be signed in to change notification settings - Fork 0
/
synonym_file_parsers.py
85 lines (69 loc) · 2.4 KB
/
synonym_file_parsers.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
84
85
"""
This module defines the parsers for the synonym file.
An abstract class is defined for the parser. This will allow us to
write parsers for other synonym input formats easily if needed in the
future.
"""
from abc import ABC, abstractmethod
class AbstractSynonymParser(ABC):
"""
An abstract base class for which defines a synonym file parser.
"""
@abstractmethod
def __init__(self, synonym_file):
"""
In a subclass method should instantiate the parser and use the
parse method (see below) to read the file and generate the
synonyms_dict class variable
This method may throw an IOError.
"""
pass
@abstractmethod
def parse(self, file_name):
"""
In a subclass this method should read the file given as the
argument file_name and generates a dict of synonyms.
This method may throw an IOError.
"""
pass
@abstractmethod
def get_synonyms(self):
"""
In a subclass this method should return the synonyms
dictionary generated by the parser.
@return a dictionary of strings which maps a synonym to the
first synonym in the list of synonyms as defined in the synonym file
"""
pass
class SpaceSeparatedSynonymFileParser(AbstractSynonymParser):
"""
A class which instantiates a parser for the space separated
synonym file format.
"""
def __init__(self, synonym_file):
"""
initializes a space seperated synonym_file parser
"""
self.synonym_dict = {}
self.parse(synonym_file)
def parse(self, file_name):
"""
Reads the file given as the argument file_name and generates a
dict of synonyms.
This method may throw a IOError Exception.
@return None
"""
with open(file_name) as file_handler:
for line in file_handler.readlines():
words = line.split()
synonym = words[0].lower()
for word in words:
self.synonym_dict[word.lower()] = synonym
return
def get_synonyms(self):
"""
This method returns the list of synonyms dict generated by the parser.
@return a dictionary of strings which maps a synonym to the
first synonym in the list of synonyms as defined in the synonym file
"""
return self.synonym_dict