1.1 --- a/tests/Dict.py Thu Sep 29 17:28:37 2005 +0000
1.2 +++ b/tests/Dict.py Thu Sep 29 17:30:53 2005 +0000
1.3 @@ -3,17 +3,19 @@
1.4 "A simple file indexer."
1.5
1.6 import codecs
1.7 +import time
1.8
1.9 class Parser:
1.10 - def __init__(self, dict_location, encoding=None):
1.11 - self.dict_location = dict_location
1.12 + def __init__(self, filenames, encoding=None, delay=None):
1.13 + self.filenames = filenames
1.14 self.encoding = encoding
1.15 + self.delay = delay
1.16
1.17 - def _get_file_content(self):
1.18 + def _get_file_content(self, filename):
1.19 if self.encoding is None:
1.20 - f = open(self.dict_location)
1.21 + f = open(filename)
1.22 else:
1.23 - f = codecs.open(self.dict_location, encoding=self.encoding)
1.24 + f = codecs.open(filename, encoding=self.encoding)
1.25 s = f.read()
1.26 f.close()
1.27 return s
1.28 @@ -22,15 +24,21 @@
1.29
1.30 "Send word entries from the file."
1.31
1.32 - tokens = self._get_file_content().split()
1.33 - index = {}
1.34 + for filename in self.filenames:
1.35 + tokens = self._get_file_content(filename).split()
1.36 + index = {}
1.37
1.38 - words = []
1.39 - for token in tokens:
1.40 - token = self._strip(token)
1.41 - if token not in words:
1.42 - channel.send((token, self.dict_location))
1.43 - words.append(token)
1.44 + words = []
1.45 + for token in tokens:
1.46 + token = self._strip(token)
1.47 + if token not in words:
1.48 + channel.send((token, filename))
1.49 + words.append(token)
1.50 +
1.51 + # Introduce a delay to simulate hard work.
1.52 +
1.53 + if self.delay:
1.54 + time.sleep(self.delay)
1.55
1.56 def _strip(self, token):
1.57
1.58 @@ -56,9 +64,9 @@
1.59
1.60 def add_entry(self, entry):
1.61
1.62 - "Add the given word 'entry' (token, dict_location) to the index."
1.63 + "Add the given word 'entry' (token, filename) to the index."
1.64
1.65 - token, dict_location = entry
1.66 + token, filename = entry
1.67
1.68 if not token:
1.69 return
1.70 @@ -71,7 +79,7 @@
1.71
1.72 if not words.has_key(token):
1.73 words[token] = []
1.74 - words[token].append(dict_location)
1.75 + words[token].append(filename)
1.76
1.77 class Searcher:
1.78 def __init__(self, index):