Package tipy :: Module stpl
[hide private]
[frames] | no frames]

Source Code for Module tipy.stpl

 1  #!/usr/bin/env python3 
 2  # -*- coding: utf-8 -*- 
 3   
 4  """The Stoplist class holds a list of (undesired) words.""" 
 5   
 6   
7 -class StopList(object):
8 """Stoplist classes holding every words of the stoplist(s). 9 10 The program's predictors test every suggested words against the stoplist. If 11 a suggested word is a stoplist word then the suggested word is not added to 12 the suggested words list. 13 14 G{classtree StopList} 15 """ 16
17 - def __init__(self, stopListFile):
18 """Extract every words of the file and store them in a list. 19 20 The file must contains one word per line. The method dosen't lower any 21 word. 22 23 @param stopListFile: 24 Path to the stoplist file. 25 @type stopListFile: str 26 """ 27 self.stopListFile = stopListFile 28 self.words = [] 29 if self.stopListFile: 30 try: 31 with open(self.stopListFile) as f: 32 self.words = [x.strip('\n') for x in f.readlines()] 33 except IOError: 34 lg.error('Cannot open file "%s". Stop list won\'t be used' 35 % (self.stopListFile)) 36 self.size = len(self.words)
37
38 - def add_words(self, wordList):
39 """Add words to the stoplist. 40 41 @param wordList: 42 The words to add. 43 @type wordList: list 44 """ 45 for word in wordList: 46 self.add_word(word)
47
48 - def add_word(self, word):
49 """Add a word to the stoplist. 50 51 @param word: 52 The word to add. 53 @type word: str 54 """ 55 self.words.append(word) 56 self.size += 1
57
58 - def remove_words(self, wordList):
59 """Remove words from the stoplist. 60 61 @param wordList: 62 The words to remove. 63 @type wordList: list 64 """ 65 for word in wordList: 66 self.remove_word(word)
67
68 - def remove_word(self, word):
69 """Remove a word from the stoplist. 70 71 @param word: 72 The word to remove. 73 @type word: str 74 """ 75 try: 76 self.words.remove(word) 77 self.size -= 1 78 except ValueError: 79 print('WARNING: "' + word + '" is not in stop list')
80