home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / freq.ex < prev    next >
Text File  |  1994-01-20  |  3KB  |  109 lines

  1.     -- Count frequencies of words in standard input.
  2.     -- Uses a binary tree to speed lookups and produce 
  3.     -- an alphabetic listing.
  4.  
  5.     -- usage: ex freq < file1 > file2
  6.  
  7. constant EOF = -1
  8. constant TRUE = 1
  9.  
  10. constant STANDARD_IN = 0,
  11.      STANDARD_OUT = 1
  12.  
  13. constant NAME = 1,
  14.      COUNT = 2,
  15.      LEFT = 3,
  16.      RIGHT = 4
  17.  
  18. without type_check
  19.  
  20. type subtree(sequence t)
  21. -- A detailed recursive type check.
  22. -- If you choose to always run with type checking
  23. -- you might prefer a simpler, faster check.
  24. -- e.g. replace "subtree" by "sequence" below
  25.     if length(t) = 0 then
  26.     return TRUE 
  27.     else
  28.         return length(t) = 4 and 
  29.            sequence(t[NAME]) and
  30.            integer(t[COUNT]) and
  31.            subtree(t[LEFT]) and 
  32.            subtree(t[RIGHT])
  33.     end if
  34. end type
  35.  
  36. function next_word()
  37. -- read standard input to get next alphabetic string 
  38.     integer c
  39.     sequence word
  40.  
  41.     word = ""
  42.     while TRUE do
  43.     c = getc(STANDARD_IN)
  44.     if (c >= 'A' and c <= 'Z') or
  45.        (c >= 'a' and c <= 'z') then
  46.             word = word & c
  47.     elsif length(word) > 0 then
  48.         return word
  49.     elsif c = EOF then
  50.         return 0
  51.     end if
  52.     end while
  53. end function
  54.  
  55. function insert(subtree node, sequence word)
  56. -- Insert new word into tree or increment existing word count by 1.
  57. -- Return modified node.
  58. -- This is quite fast since internally Euphoria is copying
  59. -- pointers to subtrees, *not* all the data as it might appear. 
  60.     integer x
  61.  
  62.     if length(node) = 0 then
  63.     -- couldn't find it - create new node
  64.     node = {word, 1, {}, {}}
  65.     else 
  66.     x = compare(word, node[NAME])
  67.     if x = 0 then
  68.         node[COUNT] = node[COUNT] + 1         -- found it, increment count 
  69.     elsif x < 0 then
  70.         node[LEFT] = insert(node[LEFT], word)  -- insert it on the left
  71.     else
  72.         node[RIGHT] = insert(node[RIGHT], word)-- insert it on the right
  73.     end if
  74.     end if
  75.     return node
  76. end function
  77.  
  78. procedure printTree(subtree node)
  79. -- recursively print tree information
  80. -- in alphabetical order
  81.     if length(node) = 0 then
  82.     return
  83.     end if
  84.     printTree(node[LEFT])
  85.     printf(STANDARD_OUT, "%s:%d\n", {node[NAME], node[COUNT]})
  86.     printTree(node[RIGHT])
  87. end procedure
  88.  
  89. -- root of the whole tree
  90. subtree root 
  91. root = {}
  92.  
  93. procedure word_count()
  94. -- build a binary tree containing words in standard input
  95.     object word
  96.  
  97.     while TRUE do 
  98.     word = next_word() 
  99.     if atom(word) then
  100.         exit
  101.     end if
  102.     root = insert(root, word)
  103.     end while
  104.     printTree(root)
  105. end procedure
  106.  
  107. word_count() 
  108.  
  109.