Commit b3cb1b87 authored by Fred Drake's avatar Fred Drake

Coding convention update: avoid use of "__" prefix for instance vars.

parent 9db492f1
...@@ -24,45 +24,45 @@ class Lexicon: ...@@ -24,45 +24,45 @@ class Lexicon:
__implements__ = ILexicon __implements__ = ILexicon
def __init__(self, *pipeline): def __init__(self, *pipeline):
self.__wids = OIBTree() # word -> wid self._wids = OIBTree() # word -> wid
self.__words = IOBTree() # wid -> word self._words = IOBTree() # wid -> word
# XXX we're reserving wid 0, but that might be yagni # XXX we're reserving wid 0, but that might be yagni
self.__nextwid = 1 self._nextwid = 1
self.__pipeline = pipeline self._pipeline = pipeline
def length(self): def length(self):
"""Return the number of unique terms in the lexicon.""" """Return the number of unique terms in the lexicon."""
return self.__nextwid - 1 return self._nextwid - 1
def words(self): def words(self):
return self.__wids.keys() return self._wids.keys()
def wids(self): def wids(self):
return self.__words.keys() return self._words.keys()
def items(self): def items(self):
return self.__wids.items() return self._wids.items()
def sourceToWordIds(self, text): def sourceToWordIds(self, text):
last = _text2list(text) last = _text2list(text)
for element in self.__pipeline: for element in self._pipeline:
last = element.process(last) last = element.process(last)
return map(self._getWordIdCreate, last) return map(self._getWordIdCreate, last)
def termToWordIds(self, text): def termToWordIds(self, text):
last = _text2list(text) last = _text2list(text)
for element in self.__pipeline: for element in self._pipeline:
last = element.process(last) last = element.process(last)
wids = [] wids = []
for word in last: for word in last:
wid = self.__wids.get(word) wid = self._wids.get(word)
if wid is not None: if wid is not None:
wids.append(wid) wids.append(wid)
return wids return wids
def get_word(self, wid): def get_word(self, wid):
"""Return the word for the given word id""" """Return the word for the given word id"""
return self.__words[wid] return self._words[wid]
def globToWordIds(self, pattern): def globToWordIds(self, pattern):
if not re.match("^\w+\*$", pattern): if not re.match("^\w+\*$", pattern):
...@@ -71,27 +71,27 @@ class Lexicon: ...@@ -71,27 +71,27 @@ class Lexicon:
assert pattern.endswith("*") assert pattern.endswith("*")
prefix = pattern[:-1] prefix = pattern[:-1]
assert prefix and not prefix.endswith("*") assert prefix and not prefix.endswith("*")
keys = self.__wids.keys(prefix) # Keys starting at prefix keys = self._wids.keys(prefix) # Keys starting at prefix
wids = [] wids = []
words = [] words = []
for key in keys: for key in keys:
if not key.startswith(prefix): if not key.startswith(prefix):
break break
wids.append(self.__wids[key]) wids.append(self._wids[key])
words.append(key) words.append(key)
return wids return wids
def _getWordIdCreate(self, word): def _getWordIdCreate(self, word):
wid = self.__wids.get(word) wid = self._wids.get(word)
if wid is None: if wid is None:
wid = self.__new_wid() wid = self._new_wid()
self.__wids[word] = wid self._wids[word] = wid
self.__words[wid] = word self._words[wid] = word
return wid return wid
def __new_wid(self): def _new_wid(self):
wid = self.__nextwid wid = self._nextwid
self.__nextwid += 1 self._nextwid += 1
return wid return wid
def _text2list(text): def _text2list(text):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment