Next / Previous / Contents / Shipman's homepage

12. kwictest: A small test driver

Here is a script that demonstrates KWIC indexing. When you run it, you will note that the words “Mae” and “Driving” occur multiple times, and the occurrences differ in their capitalization. In the resulting report, the headings for each new word use whatever case was first encountered in the text, but the reference entries always show the case that was used in that reference.

kwictest
#!/usr/bin/env python
#================================================================
# kwictest: Test driver for kwic.py
#
# Do not edit this file.  It is automatically extracted from the
# documentation at this location:
#   http://www.nmt.edu/~shipman/soft/kwic/
#----------------------------------------------------------------

# - - - - -   I m p o r t s

import sys
from kwic import KwicIndex

# - - - - -   M a n i f e s t   c o n s t a n t s

TEST_LIST = [
  "Driving Miss Daisy",
  "Daisy MAE is driving",
  "For Mae West amongst the former hundred sixty" ]

# - - -   m a i n

def main():
    '''Main program.
    '''
    kwic = KwicIndex()
    print "=== Input lines:"
    for line in TEST_LIST:
        print line
        kwic.index(line)
    print

    for kw in kwic.genWords():
        wordReport(kw)

    print "=== Only entries starting with D:"
    for kw in kwic.genWords("D"):
        if kw.word[0].upper() == 'D':
            wordReport(kw)
        else:
            break

# - - -   w o r d R e p o r t

def wordReport(word):
    '''Report on one KwicWord.
    '''
    print "=== %s" % word.word
    for ref in word.genRefs():
        print ref
    print

# - - - - -   E p i l o g u e

if __name__ == '__main__':
    main()