COMPUTATION WITH STRINGS 1 DAY 2 - 8/27/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

Character and String definitions, algorithms, library functions Characters and Strings.
NLTK & Python Day 4 LING Computational Linguistics Harry Howard Tulane University.
Strings and regular expressions Day 10 LING Computational Linguistics Harry Howard Tulane University.
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Chapter 2: Variables, Operations, and Strings CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
NLTK & BASIC TEXT STATS DAY /08/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
COMPUTATION WITH STRINGS 4 DAY 5 - 9/05/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
ON-LINE DOCUMENTS 3 DAY /17/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
UNICODE DAY /22/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
CS0004: Introduction to Programming Variables – Strings.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Structured programming 3 Day 33 LING Computational Linguistics Harry Howard Tulane University.
COMPUTATION WITH STRINGS 2 DAY 2 - 8/29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Strings CS303E: Elements of Computers and Programming.
TWITTER DAY /07/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
WEB TEXT DAY /14/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
REGULAR EXPRESSIONS 4 DAY 9 - 9/15/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
REGULAR EXPRESSIONS 2 DAY 7 - 9/10/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
NLTK & Python Day 6 LING Computational Linguistics Harry Howard Tulane University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
REGULAR EXPRESSIONS 1 DAY 6 - 9/08/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
ON-LINE DOCUMENTS DAY /13/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
CONTROL 2 DAY /26/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
COMPUTATION WITH STRINGS 3 DAY 4 - 9/03/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Review: A Computational View Programming language common concepts: 1. sequence of instructions -> order of operations important 2. conditional structures.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CONTROL 3 DAY /29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
GCSE COMPUTER SCIENCE Practical Programming using Python
String and Lists Dr. José M. Reyes Álamo.
Lists 1 Day /17/14 LING 3820 & 6820 Natural Language Processing
Topic: Python Lists – Part 1
Chapter 6 JavaScript: Introduction to Scripting
Lists 2 Day /19/14 LING 3820 & 6820 Natural Language Processing
Flat text Day 6 - 9/12/16 LING 3820 & 6820 Natural Language Processing
Computation with strings 2 Day 3 - 9/02/16
Variables, Expressions, and IO
Introduction to Scripting
Computation with strings 3 Day 4 - 9/07/16
Computation with strings 1 Day 2 - 8/31/16
Methods – on strings and other things
Regular expressions 2 Day /23/16
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Control 3 Day /05/16 LING 3820 & 6820 Natural Language Processing
NLP 2 Day /07/16 LING 3820 & 6820 Natural Language Processing
CS 100: Roadmap to Computing
Regular expressions 3 Day /26/16
Python Primer 1: Types and Operators
Methods – on strings and other things
15-110: Principles of Computing
JavaScript: Objects.
Computation with strings 4 Day 5 - 9/09/16
CS 100: Roadmap to Computing
Control 1 Day /30/16 LING 3820 & 6820 Natural Language Processing
Presentation transcript:

COMPUTATION WITH STRINGS 1 DAY 2 - 8/27/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University

Course organization 27-Aug-2014NLP, Prof. Howard, Tulane University 2   The syllabus is coming.   Is there anyone here that wasn't here on Monday?

Can anyoone NOT get Spyder to do this? Installation of Python 27-Aug NLP, Prof. Howard, Tulane University

Test >>>  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 27-Aug-2014NLP, Prof. Howard, Tulane University 4

A string is a sequence of characters delimited between single or double quotes.string §3. Computation with strings 27-Aug NLP, Prof. Howard, Tulane University

Examples 1. >>> monty = 'Monty Python' 2. >>> monty 3. 'Monty Python' 4. >>> doublemonty = "Monty Python" 5. >>> doublemonty 6. 'Monty Python' 7. >>> circus = 'Monty Python's Flying Circus' 8. File " ", line 1 circus = 'Monty Python's Flying Circus' 9. ^ SyntaxError: invalid syntax 10. >>> circus = "Monty Python's Flying Circus" 11. >>> circus 12. "Monty Python's Flying Circus" 13. >>> circus = 'Monty Python\'s Flying Circus' 14. >>> circus 15. "Monty Python's Flying Circus" 27-Aug-2014NLP, Prof. Howard, Tulane University 6

The + and * operators  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 –:concatenation 1. >>> S = 'balloon' 2. >>> S+'!' 3. >>> S+! 4. >>> 'M'+S 5. >>> S*2 6. >>> S+'!'*2 7. >>> (S+'!')*2 8. >>> S-'n' 9. >>> S >>> S+'2' 27-Aug-2014NLP, Prof. Howard, Tulane University 7

Some string methods  Python supplies several methods that can be applied to strings to perform tasks. Some 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: 1. >>> len(S) 2. >>> len(S+'!') 3. >>> len(S*2) 4. >>> sorted(S) 5. >>> len(sorted(S)) 6. >>> set(S) 7. >>> sorted(set(S)) 8. >>> len(set(S)) 27-Aug-2014NLP, Prof. Howard, Tulane University 8

Tokens vs. types  set(S) produces the set of characters in the string.  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.tokentype  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'. 27-Aug-2014NLP, Prof. Howard, Tulane University 9

Method notation  The material aggregated to a method in parentheses is called its argument(s).  In the examples above, the argument S can be thought of linguistically as the object of a noun: the length of S, the alphabetical sorting of S, the set of S. 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: >>> S.count('o')  The example can be read as “in S, 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. 27-Aug-2014NLP, Prof. Howard, Tulane University 10

Cleaning 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: >>> S = 'i lOvE yOu' >>> S >>> S.lower() >>> S.upper() >>> S.swapcase() >>> S.capitalize() >>> S.title() >>> S.replace('O','o') >>> S.strip('i') >>> S2 = ' '+S+' ' >>> S2 >>> S2.strip() 27-Aug-2014NLP, Prof. Howard, Tulane University 11

3.3. Finding your way around a string I will try to send you some practice for what we have done today. Next time