110 likes | 233 Vues
In this lecture, we'll cover key Perl programming concepts, specifically focusing on arrays and hashes, their initialization, and how to manipulate data structures. We'll discuss the importance of human language processing versus machine models, and explore examples from our reading on compositional semantics, including the practical application of idioms. Homework assignments due next Wednesday will include detecting repeated words in sentences and calculating word frequencies. Ensure you're prepared for Perl Week with the provided resources and examples.
E N D
LING/C SC/PSYC 438/538 Lecture 3 8/30 Sandiway Fong
Administrivia • Homework out today • change: due next Wednesday midnight • (Monday being Labor Day) • Reading reminder • Chapter 2 of JM.. we’ll be using Perl here
Quiz from Lecture 1 • Discussion (Quiz not handed back) • Is the word spoilsport an example of compositional semantics or not? Explain your answer. idioms contrasted with compositional e.g. spoilsmanvs. blackboard • Re: human language processing vs. machine models of language Did the authors cite any examples of this in chapter 1?
Perl Week • Tutorial contd… • http://perldoc.perl.org/perlintro.html • from arrays onwards …. • Philosophy: Natural Language Principles in Perl • If a language is designed so that you can ``learn as you go'', then the expectation is that everyone is learning, and that's okay. • http://www.wall.org/~larry/natural.html
Perl Week • Notes on arrays and hashes • Arrays are indexed from 0,1,2,3… • Hashes are like arrays with user-defined indexing (aka associative array or hash table) • Initialization (round brackets and commas) • @a = (“zero”, “one”, “two”, “three”, “four”); • %h = (“zero”, 0, “one”, 1, “two”, 2, “three”, 3, “four”, 4); (key/value pairs) • Access (square brackets vs. curly braces) • $a[1] “one” • $h{zero} 0 • Output • print @a zeroonetwothreefour • print “@a” zero one two three four • print %h three3one1zero0two2four4 (note: different order) • print “%h” %h
Perl Week • Conditionals • if ( @a < 10 ) { print “Small array\n” } else {print “Big array\n” } • Note: @a here is a scalar = size of array • unless (@a > 10) { print “@a\n” } • Note: if size of array a is ≤ 10, it prints the contents of array a • Looping • %fruits = ("apple", "green", "orange", "orange", "lemon", "yellow"); • foreach $fruit (keys %fruits) { print $fruit, " => ", $fruits{$fruit}, "\n” } gives output: • lemon => yellow • apple => green • orange => orange • Note: apparently here • keys %fruits = (“lemon” “apple” “orange”) is an array
Perl Week • Example: • the following program prints Equal! • == is the numeric equality comparison operator • my $one_string = “1”; • my $one_number = 1; • if ($one_string == $one_number) { • print “Equal!\n” • } else { • print “Different!\n” • } • Example: • the following program prints 3 is my number • . is the string concatenation operator • my @a = (one, two, three); • my $string = @a . “ is my number”; • print “$string\n”; Perl features implicit coercion of data types
Lecture 3 Homework • Due nextWednesday(see lecture 1 for the rules on submissions) • One file only total please! • Question 1: 438 and 538 (7 points) • Given • @sentence1 = (I, saw, the, the, cat, on, the, mat); • @sentence2 = (the, cat, sat, on, the, mat); • Write a simple Perl program which detects repeated words (many spell checker/grammar programs have this capability) • It should print a message stating the repeated word and its position if one exists • e.g. word 3 “the” is repeated in the case of sentence1 • No repeated words found in the case of sentence2 • note: output multiple messages if there are multiple repeated words • Hint: use a loop • Submit your Perl code and show examples of your program working
Lecture 3 Homework • Question 2: 438 and 538 (3 points) • Describe what would it take to stop a repeated word program from flagging legitimate examples of repeated words in a sentence • (No spell checker/grammar program that I know has this capability) • Examples of legitimately repeated words: • I wish that that question had an answer • Because he had had too many beers already, he skipped the Friday office happy hour
Lecture 3 Homework • Question 3: 538 (10 points), (438 extra credit) • Write a simple Perl program that outputs word frequencies for a sentence • E.g. given • @sentence1 = (I, saw, the, cat, on, the, mat, by, the, saw, table); • output a summary that looks something like: • the occurs 4 times • saw occurs twice • I, car, mat, on, by, table occurs once only • Hint: build a hash keyed by word with value frequency • Submit your Perl code and show examples of your program working