-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWordDictionary.py
More file actions
38 lines (32 loc) · 1011 Bytes
/
WordDictionary.py
File metadata and controls
38 lines (32 loc) · 1011 Bytes
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
# coding:utf-8
import csv
import os
import codecs
class WordDictionary:
def __init__(self):
self.words = {}
def add_word(self, word):
if not word in self.words:
self.words[word] = len(self.words)
def get_index(self, word):
if word in self.words:
return self.words[word]
else:
return None
def get_size(self):
return len(self.words)
def save(self, path, name):
if not os.path.exists(path):
os.makedirs(path)
file = codecs.open(path + name, 'w+', encoding='utf-8')
wr = csv.writer(file)
for key in self.words.keys():
wr.writerow([key, self.words[key]])
file.close()
def load(self, path, name):
self.words.clear()
file = codecs.open(path + name, 'r', encoding='utf-8')
rd = csv.reader(file)
for strs in rd:
self.words[strs[0]] = int(strs[1])
file.close()