1 / 33

LING 408/508: Computational Techniques for Linguists

LING 408/508: Computational Techniques for Linguists. Lecture 2 8/22/2012. Survey. Please fill out the survey and hand it in today, if you haven’t done so already. You don’t need to list every single course you’ve ever taken. Outline. Running IDLE Overview of computer programming

brock
Download Presentation

LING 408/508: Computational Techniques for Linguists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LING 408/508: Computational Techniques for Linguists Lecture 2 8/22/2012

  2. Survey • Please fill out the survey and hand it in today, if you haven’t done so already. • You don’t need to list every single course you’ve ever taken.

  3. Outline • Running IDLE • Overview of computer programming • Programming languages and computer hardware • Python datatypes • Assignment operator and references • Short assignment #1

  4. Outline • Running IDLE • Overview of computer programming • Programming languages and computer hardware • Python datatypes • Assignment operator and references • Short assignment #1

  5. Programming involves: • Choosing a computational problem • Finding a solution in terms of abstract representations and operations • Implementation in a particular programming language

  6. 1. Formulate computational problem • Problem to be solved, with input and output • Example 1: multiplication • Input: two numbers x and y • Output: a number z, which is the result of multiplying x and y together • Example 2: weather prediction • Input: today’s temperature, today’s humidity, time of year, geographic location, etc. • Output: a temperature and quantity of precipitation predicted for tomorrow • Develop a simplified model for complex real-world problems

  7. 2. Abstract representations and operations • Decide on relevant objects / states / characteristics / actions of the problem • Formulate abstract representations of those objects / states • Formulate algorithms that operate upon those representations to produce some result • Complex problems can be modeled by breaking it down into small, interacting components

  8. Example: chess game • What do we need to represent? • What operations are applied to those representations?

  9. Example: chess game • What do we need to represent? • chess board • pieces • configuration of pieces (at a particular moment in time) • starting configuration • whose turn it is • end state: checkmate / draw • whether the current player is in check • etc. • What actions take place upon those representations? • Legal moves of pieces • pawn, rook, pawn becoming king, castling, check, etc. • Result of moving pieces • Whether the configuration of pieces is a an end state • etc.

  10. 3. Implementation in programming language • Choose a programming language • Write the program in terms of the constructs available in the language • Program = algorithm implemented in a particular programming language

  11. Example: multiplication • Write the program in terms of the constructs available in the language • Multiply 3 by 5 in Python: 3 * 5 • It’s easy to write a program for this because the representations and operations correspond exactly to primitive constructs in Python: integers and the multiplication operator

  12. Example: program to play chess • Not so straightforward… • For example, we want to represent a chess board. But the concept of a “chess board” is not directly available in the Python language (unlike integers). • Need compound data types • Begin with primitive data types, combine them to produce higher-level representations • Can produce arbitrarily complex models

  13. Outline • Running IDLE • Overview of computer programming • Programming languages and computer hardware • Python datatypes • Assignment operator and references • Short assignment #1

  14. Primitive data types in Python • Integers: • 3, 8, 0, -2, 100 • Floating-point numbers • 3.14159, 0.0001, -.101010101, 2.34e+18 • Booleans (binary-valued) • True, False • “None” • Value is None

  15. Compound data types in Python • Strings • 'hello', "GOODBYE" • (note: in C, a string is an array of characters; Python doesn’t have characters; a single character is a 1-element string) • Lists • [1,2,3,4,5], ['how', 'are', 'you'] • Elements are indexed: element at index 0 of [1,2,3,4,5] is 1 • Tuples • (1, 2, 3, 'a', 'b')

  16. Compound data types in Python • Sets • {1,2,3,4,5} {1,1,2,2,3,3,4,4} # these are same • Sets do not store duplicates, and set elements are notindexed • Dictionaries • {'a':1, 'b':2, 'c':3} • Map ‘a’ to 1, map ‘b’ to 2, map ‘c’ to 3 • Example application: represent the frequencies of letters in a text • Classes • Create your own data type

  17. Operators • Operators are applied to data types to produce new results • Common integer operators: + addition - subtraction * multiplication // division / integer division % modulus ** exponentiation

  18. Addition, subtraction, multiplication >>> 3 + 4 7 >>> 10 - 2 8 >>> 5 * 3 15 >>> 3 * (2 + 1) # parentheses for grouping 9

  19. Exponentiation >>> 2**3 # 2 to third power = 2*2*2 = 8 8 >>> 2**4 # 2 to fourth power = 16 16 >>> 16**.5 # same as square root of 16 4

  20. Integer division and remainder • In elementary school, you learned that: 7 / 2 = 3 r 1 • In Python: >>> 7 // 2 3 >>> 7 % 2 1

  21. Floating-point division >>> 7 // 2 3 >>> 7 / 2 # result is a float 3.5

  22. Complex arithmetic expressions • Apply operator to operands that may themselves be complex 2 3 2 / 3 1 + (2 / 3) 7 * (1 + (2 / 3)) 7 * (1 + (2 / 3)) – 18 ( 7 * (1 + (2 / 3)) – 18 ) % 4 (( 7 * (1 + (2 / 3)) – 18 ) % 4)**3

  23. Outline • Running IDLE • Overview of computer programming • Programming languages and computer hardware • Python datatypes • Assignment operator and references • Short assignment #1

  24. Assignment operator • Let’s say you want to calculate the result of 1 + (2 * (3 + ( 4 * ( 5 + 6 )))) • You want to re-use the result in subsequent computation • It will be cumbersome to repeatedly put this long expression in your code multiple times • Solution: assignment of value to variable >>> a = 1 + (2 * (3 + ( 4 * ( 5 + 6 )))) >>> a # value is retrieved from variable 95

  25. Assignment operator • The assignment operator assigns a value to a variable >>> x = 3 >>> x 3 >>> y = 7 >>> x + y # look up values for x and y 10 >>> x * y 21 >>> x = x + 1 # modify value of variable >>> x 4

  26. Variables are stored as references • What happens when we write x = 3 ? • Create an integer 3and store it in memory • Create a name x • xis associated with a reference to the memory location storing 3 • So, when we say that the value of xis 3, we mean that xnow refers to the integer 3 Type: Integer Data: 3 Variable name: x Variable name: x Ref: <address1> memory name list

  27. Modification of immutabledatatypes(int, string, float, bool, tuple) • x = x + 1 • The reference of name x is looked up. • The value at that reference is retrieved. • Calculate 3+1, producing 4,which is assigned to a fresh memory location with a new reference. • The name x is changed to point to this new reference. • The old data 3 is garbage collected if no name refers to it. • Immutable: change what a variable refers to, rather than change its value Type: Integer Data: 3 Variable name: x Ref: <address1> Type: Integer Data: 4

  28. Shortcut operators >>> a = 3 >>> a = a + 1 >>> a 4 >>> a += 1 # same as a = a + 1 >>> a 5 >>> a *= 3 # same as a = a * 3 >>> a 15

  29. Swap values of variables >>> x = 3 >>> y = 4 • Want to swap values: want x to have value of 4, y to have value of 3 >>> x = y # this doesn’t work: >>> y = x # now x is 4, y is 4

  30. Swap values of variables • Assign to a temporary variable: >>> x = 3 >>> y = 4 >>> temp = x # save value of x: temp is 3 >>> x = y # gets value of y: x is now 3 >>> y = temp # y gets value stored in temp • Alternatively, use multiple assignment: >>> x = 3 >>> y = 4 >>> x, y = y, x # now x is 4, y is 3 >>> (x, y) = (y, x) # parentheses are optional

  31. Outline • Running IDLE • Overview of computer programming • Programming languages and computer hardware • Python datatypes • Assignment operator and references • Short assignment #1

  32. Due by Friday 8/24 • Make 1,000 from eight 8’s, combined together in one arithmetic expression, through any combination of integer operators and parentheses • Example: (8888 – 888) / 8 = 1000 • Instructions: • Think of two more solutions to this problem • Create a python script file that, when executed, prints your formula, and evaluates and prints the result • Represent your formula as a string to print it: print('5+5') • Turn in a screenshot of script file and result in Python shell • Turn in at beginning of class, or place under my office door

  33. Next class: functions

More Related