-
Notifications
You must be signed in to change notification settings - Fork 1
/
add.py
42 lines (28 loc) · 1.18 KB
/
add.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
import os, shutil, string
from sets import Set
LocalDictionary = os.path.join(os.getenv("HOME"), 'Library/Spelling/LocalDictionary')
HostedDictionaryPath = os.path.join(os.path.dirname(__file__), 'dictionaries')
# Make initial backup of old file
if not os.path.exists(LocalDictionary + '.backup'):
shutil.copyfile(LocalDictionary, LocalDictionary + '.backup')
# Read LocalDictionary
d = Set(map(string.strip, open(LocalDictionary).readlines()))
length = len(d)
# Add words
for fileName in os.listdir(HostedDictionaryPath):
if os.path.isfile(os.path.join(HostedDictionaryPath, fileName)) and not fileName.startswith('.'):
print 'Reading %s' % (fileName)
# Read new words
newWords = Set(map(string.strip, open(os.path.join(HostedDictionaryPath, fileName)).readlines()))
# Remove comments and empty lines
newWords = [x for x in newWords if not x.startswith('#')]
newWords = [x for x in newWords if x != '']
# Update set
d.update(Set(newWords))
# Sort alphabetically
d = sorted(d)
# Save back
LocalDictionaryFile = open(LocalDictionary, 'w')
LocalDictionaryFile.write("\n".join(d))
LocalDictionaryFile.close()
print 'Added %s words to your dictionary.' % (len(d) - length)