Next / Previous / Contents / Shipman's homepage

9.8. KwicIndex.__addRef(): Add one reference

kwic.py
# - - -   K w i c I n d e x . _ _ a d d R e f

    def __addRef(self, s, start, end, userData):
        '''Add one reference, unless it's in the stop list.

          [ (s is a unicode string) and
            (start and end bracket a nonempty string in s) ->
                let
                   word == s[start:end]
                   prefix == s[:start].strip()
                   suffix == s[end:].strip()
                in ->
                  word is a unicode string ->
                    if word is in self.__stopSet ->
                      I
                    else if self.__skip has a KwicWord instance for
                    word ->
                      that instance  +:=  a KwicRef instance
                          with prefix=(prefix), word=(word),
                          suffix=(suffix), and userData=(userData)
                    else ->
                      self.__skip  :=  a new KwicWord instance 
                          containing a new KwicRef instance
                          with prefix=(prefix), word=(word),
                          suffix=(suffix), and userData=(userData) ]
        '''

First we extract the keyword. If the uppercased word is in the stop list, just return to the caller. Otherwise extract the prefix and suffix strings.

kwic.py
        #-- 1 --
        word = s[start:end]
        upWord = word.upper()

        #-- 2 --
        if upWord in self.__stopSet:
            return
        else:
            prefix = s[:start].strip()
            suffix = s[end:].strip()

        #-- 3 --
        # [ if self.__skip has an entry for (upWord) ->
        #     kwicWord  :=  the corresponding value
        #   else ->
        #     self.__skip  :=  a new KwicWord instance for (word)
        #     kwicWord  :=  that instance ]
        try:
            kwicWord = self.__skip.match(upWord)
        except KeyError:
            kwicWord = KwicWord(word)
            self.__skip.insert(kwicWord)

        #-- 4 --
        # [ kwicWord  +:=  a new KwicRef with prefix=(prefix),
        #       word=(word), suffix=(suffix), ]
        kwicWord.add ( prefix, word, suffix, userData )