Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computation with strings 1 Day 2 - 8/31/16

Similar presentations


Presentation on theme: "Computation with strings 1 Day 2 - 8/31/16"— Presentation transcript:

1 Computation with strings 1 Day 2 - 8/31/16
LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

2 Course organization http://www.tulane.edu/~howard/NLP/
Is there anyone here that wasn't here on Monday? I have changed dates of last two quizzes in the Schedule of assignments. I have updated 2. An Introduction to Python. Please go over it at your leisure and familiarize yourself with the command-line interface. I have probably done as much work as I have time for on 3. Natural language processing. We have a lot to do in a short amount of time this semester, so treat the section on computational culture as a bit of inspiration for the final project. NLP, Prof. Howard, Tulane University 31-Aug-2016

3 Computer hygiene You must turn your computer off every now and then, so that it can clean itself. By the same token, you should close applications every now and then. NLP, Prof. Howard, Tulane University 31-Aug-2016

4 Installation of Python
Can any one NOT get Spyder to do this? NLP, Prof. Howard, Tulane University 31-Aug-2016

5 Test >>> 9312 Be sure to try the other arithmetic operators, subtraction (-), multiplication (*), and division (/). Does division work the way you expect? After you have tired of playing with math, play with some text: >>> word = 'msinairatnemhsilbatsesiditna' >>> 'anti' in word False >>> 'itna' in word True NLP, Prof. Howard, Tulane University 31-Aug-2016

6 §4. Computation with strings
What is a string? A string is a sequence of characters delimited between single or double quotes. NLP, Prof. Howard, Tulane University 31-Aug-2016

7 Examples >>> monty = 'Monty Python' >>> monty
>>> doublemonty = "Monty Python" >>> doublemonty >>> circus = 'Monty Python's Flying Circus' File "<stdin>", line 1 circus = 'Monty Python's Flying Circus' ^ LyntaxError: invalid syntax >>> circus = "Monty Python's Flying Circus" >>> circus "Monty Python's Flying Circus" >>> circus = 'Monty Python\'s Flying Circus' NLP, Prof. Howard, Tulane University 31-Aug-2016

8 The + and * operators >>> B = 'balloon' >>> B
>>> B+'s' >>> B+s >>> 'red '+B >>> 'red '+B+'s' >>> B*2 >>> B+'s'*2 >>> (B+'s')*2 >>> B-'n' >>> B+2 >>> B+'2' A new string can be formed by combination or concatenation of two strings with + or repeating a string a number of times with *. Unfortunately, a character cannot be deleted with –: NLP, Prof. Howard, Tulane University 31-Aug-2016

9 4.1.1. Operator precedence NLP, Prof. Howard, Tulane University
31-Aug-2016

10 4.1.2. Data type >>> type(B) >>> type(2)
Look at B in Variable Explorer. NLP, Prof. Howard, Tulane University 31-Aug-2016

11 4.2. Basic string methods Python supplies several methods that can be applied to strings to perform tasks. Lome of them are illustrated below. The input code is given, without the corresponding output. It is up to you to type them in to see what they do: >>> len(B) >>> len(B+'s') >>> len(B*2) >>> sorted(B) >>> len(sorted(B)) >>> set(B) >>> sorted(set(B)) >>> len(set(B)) NLP, Prof. Howard, Tulane University 31-Aug-2016

12 4.2.1. Nested or embedded operations
NLP, Prof. Howard, Tulane University 31-Aug-2016

13 Tokens vs. types set(B) produces the set of characters in string B.
One useful property of sets is that they do not contain duplicate elements. The process of removing repetitions performed by set() touches on a fundamental concept in language computation, that of the distinction between a token and a type. A representation in which repetitions are allowed is said to consist of tokens, while one in which there are no repetitions is said to consist of types. Thus set() converts the tokens of a string into types. There is one type of 'o' in 'balloon', but two tokens of 'o'. NLP, Prof. Howard, Tulane University 31-Aug-2016

14 4.2.3. Dot and method notation
The material aggregated to a method in parentheses is called its argument(s). In the examples above, the argument B can be thought of linguistically as the object of a noun: the length of B, the alphabetical sorting of B, the set of B. But what if two pieces of information are needed for a method to work, for instance, to count the number of o’s in otolaryngologist? To do so, Python allows for information to be prefixed to a method with a dot: >>> B.count('o') The example can be read as “in B, count the o’s”, with the argument being the substring to be counted, 'o', and the attribute being the string over which the count progresses, or more generally: attribute.method(argument) What can be attribute and argument varies from method to method and so has to be memorized. NLP, Prof. Howard, Tulane University 31-Aug-2016

15 How to clean up a string There is a group of methods for modifying the properties of a string, illustrated below. You can guess what they do from their names: >>> L = 'i lOvE yOu' >>> L >>> L.lower() >>> L.upper() >>> L.swapcase() >>> L.capitalize() >>> L.title() >>> L.replace('O','o') >>> L.strip('i') >>> L2 = ' '+L+' ' >>> L2 >>> L2.strip() NLP, Prof. Howard, Tulane University 31-Aug-2016

16 4.3. How to find your way around a string
>>> E = 'abcde' >>> E[0] >>> E[1] >>> E[4] >>> E[5] NLP, Prof. Howard, Tulane University 31-Aug-2016

17 0 = 1, Zero-based indexation
You probably thought that the first character in a string should be given the number 1, but Python actually gives it 0, and the second character gets 1. There are some advantages to this format which do not concern us here, but we will mention a real- world example. In Europe, the floors of buildings are numbered in such a way that the ground floor is considered the zeroth one, so that the first floor up from the ground is the first floor, though in the USA, it would called the second floor. NLP, Prof. Howard, Tulane University 31-Aug-2016

18 In a picture NLP, Prof. Howard, Tulane University 31-Aug-2016

19 What does a negative index mean?
NLP, Prof. Howard, Tulane University 31-Aug-2016

20 Slicing >>> E[2:5] >>> E[-6:-3]
NLP, Prof. Howard, Tulane University 31-Aug-2016

21 No beginning or end >>> E[2:] >>> E[-2:]
NLP, Prof. Howard, Tulane University 31-Aug-2016

22 A slice is a string >>> type(E[2:]) >>> E[:-1] + '!'
NLP, Prof. Howard, Tulane University 31-Aug-2016

23 4.3.1.4. Extended slicing >>> K = 'abcdefghijk'
NLP, Prof. Howard, Tulane University 31-Aug-2016

24 Format of a slice string[start:end:step]
NLP, Prof. Howard, Tulane University 31-Aug-2016

25 4.3.1.5. How to reverse a string >>> K[::-1]
NLP, Prof. Howard, Tulane University 31-Aug-2016

26 4.3.2. How to find an index given a character
NLP, Prof. Howard, Tulane University 31-Aug-2016

27 >>> D = 'abcdabc'
index() & rindex() find() & rfind() >>> D.index('d') >>> D.rindex('d') >>> D.index('a') >>> D.rindex('a') >>> D.find('d') >>> D.rfind('d') >>> D.find('a') >>> D.rfind('a') NLP, Prof. Howard, Tulane University 31-Aug-2016

28 index() or find() Where they differ lies in how they handle null responses: >>> D.find('z') -1 >>> D.index('z') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found NLP, Prof. Howard, Tulane University 31-Aug-2016

29 Find substrings >>> D.index('cda')
index() & rindex() find() & rfind() >>> D.index('cda') >>> D.index('abc') >>> D.find('cda') >>> D.find('abc') NLP, Prof. Howard, Tulane University 31-Aug-2016

30 4.3.2.1. How to limit search to a substring
index() & rindex() find() & rfind() >>> D.index('ab', 0, 3) >>> D.index('ab', 3) >>> D.find('ab', 0, 3) >>> D.find('ab', 3) NLP, Prof. Howard, Tulane University 31-Aug-2016

31 Format index/find(string, beginning, end)
NLP, Prof. Howard, Tulane University 31-Aug-2016

32 4.3.3. Operator iteration >>> L = 'i lOvE yOu'
>>> L[2:6].capitalize().upper() >>> L[-3:].capitalize().lower() >>> (L[:4].upper()+L[4:].lower()).swapcase() NLP, Prof. Howard, Tulane University 31-Aug-2016

33 4.2.5. Practice 1 What types are output by len(), sort(), set()?
Here are two real life strings to work with: >>> mail = >>> url = ' How would you strip out the user name and the server name from my address? Internet addresses start with the transfer protocol that the site uses. For web pages, this is usually the hypertext transfer protocol, http. How would you strip this information out to leave just the address of the book? Following up on (b), how would you extract just Tulane’s server address? NLP, Prof. Howard, Tulane University 31-Aug-2016

34 Practice 1, cont. 3) Write the code to perform the changes given below on these two strings: >>> S = 'ABCDEFGH' >>> s = 'abcdefgh' Make the first 3 characters of S lowercase. Make the last 4 characters of s uppercase. Create a string from the first 4 characters of S and the last 4 characters of s and then switch its case. Join both strings and find every even character. Join both strings and reverse the order of the characters. Retrieve the index of 'E' and 'h'. NLP, Prof. Howard, Tulane University 31-Aug-2016

35 Practice 1, cont. What is the longest sequence of operators that you can make? NLP, Prof. Howard, Tulane University 31-Aug-2016

36 Next time Practice 2 4.5. How to make a string longer than one line 4.6. Assignment and mutability 4.7. Date and time strings which has a practice. NLP, Prof. Howard, Tulane University 31-Aug-2016


Download ppt "Computation with strings 1 Day 2 - 8/31/16"

Similar presentations


Ads by Google