Linguistics 158: Computer-aided methods in linguistics

John B. Lowe / Department of Linguistics
University of California, Berkeley - Spring 1997

Data structures and control logic

week /class: 7 / 13 : Lec Tu , 4 Mar 1997

Some"Metadiscussion" about programming

  1. Data: Variables and containers
  2. The data used by programs exists in a variety of different forms:

    Some container expressions in HyperCard

  3. Control logic
  4. Not every statement in a program is executed when it runs, and not every statement is executed once. It is necessary to state conditions under which a statement is executed: executing one set of statements may preclude executing another set of statements. Normally, sets of statements which are all to be executed together are organized into blocks. Often the grouping is explicitly indicated by some sort of enclosing text or symbols.

    statement block

    do
        statement 1
        statement 2
        statement 3
        ...
        statement n
    enddo
    
    The statements in a program which alter the order of execution of statements are called control statements and the use of them in a program is called flow control or control logic. conditional execution of statements

  5. Conditional execution: the general idea "if then else"
  6. if condition then
        statement block
    else
       statement block
    end if
    

    HyperCard example

    on mouseUp
      ask "Gender?"
      if it is "boy" then
        put "male"
      else if it is "girl" then
        put "female"
      else
        put "who knows!"
      end if
    end mouseUp
    

    PERL example

    if ($line =~ /$exp/) {
         print "$line matched: $& \n";
        }     else {
             print "$line (pattern not found.)\n";
        }
    

    
    

  7. Iteration
  8. Iteration (or "looping") is the process of doing the same process over and over again until some desired result is reached. You may wish to perform the same process on a number of different items (e.g. words in a file), or you may wish to perform the a process on a single item until a certain condition obtains (e.g. exchanging pairs in a list of items until the list is sorted). In TG, for example, iteration was handled via The Cycle, leading to a pernicious idea that language processing and computer processing might share important similarities.

    Usually, the syntax of a loop is something like:

    Looping: the general idea...

    set loop invariant
    loop while loop invariant is false
        do something
        reset loop invariant
    endloop
    

    PERL example: Read lines from a file and look for a pattern

    while ($line = ) {
              chop($line);
          if ($line =~ /$exp/) {
               print "$line matched: $& \n";
              }     else {
                   print "$line (pattern not found.)\n";
              }
    }
    

  9. Putting these all together: HyperCard examples
  10. Writing the contents of a HyperCard field a text file

    on ExportCorpus
      ask file "Name of file to write"
      if it is empty then exit ExportCorpus
      put it into fn
      open file fn
      write bg fld "Corpus" to file fn
      close file fn
    end ExportCorpus
    

    Loading a text file into a field (NB: 30000 character limit!)

    on ImportCorpus
      answer file "Name of file to load" of type text
      if it is empty then
        exit ImportCorpus
      else
        put it into fn
      end if  
      put 0 into RecCount
      open file fn
      read from file fn until return
      repeat while it is not empty
        if char 1 of it = "#" then
        else
          put cleanup(it) into line RecCount of bg fld "Corpus"
        end if
        add 1 to RecCount
        read from file fn until return
      end repeat
      close file fn
      answer RecCount && "lines loaded."
    end ImportCorpus
    

    Using Functions

    function cleanup str
    -- remove trailing blanks. Assumes string STR has a CR at the end of it
        put empty into last char of str
        repeat while the last char of str = " "
          put empty into the last char of str
        end repeat
        return str
    

    HyperCard example: A string searching function

    Returns the line number of the first matching line in a container
    function Search target,source
      put offset(return & target & return,return & source & return) into ofs
      return (the number of lines in char 1 to ofs of source)
    end Search
    

  11. A Palindrome Detector (NB: bug fixed!)
  12. on mouseUp
      -- get the possible palindrome
      put line 1 of cd fld 1 into p
      -- save a copy for later use
      put p into p2
      -- delete punctuation etc. which might mess us up
      repeat with i = the number of characters in p down to 1
        if " ,;.:'" contains char i of p then
          delete char i of p
        end if
      end repeat
      -- reverse the string
      put empty into p3
      repeat with i = 1 to the number of characters in p
        put char i of p before p3
      end repeat
      -- whew! hard part is over!
      answer p && p3
      if p3 = p then
        put p2 && "is a palindrome"
      else
        put p2 && "is NOT a palindrome"
      end if
    end mouseUp

    Homework 4 : Corpora (due: Mar. 13)
    Homework answers
    [Ling 158 Home Page | Linguistics 158 schedule]