1
2
3
4 """Selector select suggested words in the list depending on various criteria.
5 """
6
7 from collections import OrderedDict
8
9
11 """The Selector class select the best suggested words among all.
12
13 The goal of the selector is to clean the suggested words list (contained in
14 a Prediction instance which should have been returned by a
15 L{Merger<mrgr>} L{merge()<mrgr.Merger.merge>} method and remove
16 the words which are too much (the configuration specify the excpected number
17 of suggested words).
18
19 G{classtree Selector}
20 """
21
22 - def __init__(self, config, contextMonitor):
23 """Selector creator.
24
25 @param config:
26 The configuration dictionary is used in order to retrieve the
27 Selector settings from the config file.
28 @type config: L{drvr.Configuration}
29 @param contextMonitor:
30 The ContextMonitor is used to check if a context change occure. In
31 which case the suggested words list must be cleared because
32 prediction fully depends on the context.
33 @type contextMonitor: L{ContextMonitor}
34 """
35 self.contextMonitor = contextMonitor
36 self.config = config
37 self.suggestions = self.config.getas('Selector', 'suggestions', 'int')
38 self.suggestedWords = []
39
41 """Select suggested words in the suggestions list.
42
43 Selecting suggested words consists in:
44 - List every suggestions words.
45 - Remove duplicate suggested words.
46 - Shorten the suggested words list so that it contains the desired
47 number og suggested words.
48
49 @param prediction:
50 The list of suggestions from which to carry out the selection.
51 @type prediction: L{Prediction}
52 """
53 words = [sugg.word for sugg in prediction]
54 if self.contextMonitor.context_change():
55 self.suggestedWords = []
56 self.rm_duplicate(words)
57 if len(words) > self.suggestions:
58 words = words[:self.suggestions]
59 self.suggestedWords += words
60 return words
61
63 """Remove duplicate words in the list and keep the original order.
64
65 @param words:
66 The words to filter.
67 @type words: list
68 """
69 self.suggestedWords = list(
70 OrderedDict.fromkeys(self.suggestedWords + words))
71