\documentclass{beamer}             % for slides
% \documentclass[handout]{beamer}    % for handout
\input{beamer}

\title{Programming Fundamentals and Python}

\author{Steven Bird \and Ewan Klein \and Edward Loper}
\institute{
  University of Melbourne, AUSTRALIA
  \and
  University of Edinburgh, UK
  \and
  University of Pennsylvania, USA
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section{Introduction: Programming Basics}

\begin{frame}
  \titlepage
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
  \frametitle{Introduction}
  \begin{itemize}
    \item non-technical overview
    \item many working program fragments
    \item try them for yourself as we go along
    \item many online tutorials (see \url{www.python.org})
    \item Textbook: Zelle, John (2004) \textit{Python Programming: An Introduction
        to Computer Science}
  \end{itemize}
\end{frame}

\subsection{Lists}

\begin{frame}[fragile]
  \frametitle{Defining Lists}
  \small

\begin{itemize}
\item list: ordered sequence of items
\item item: string, number, complex object (e.g. a list)
\item list representation: comma separated items: \verb|['John', 14, 'Sep', 1984]|
\item list initialization:

\begin{verbatim}
    >>> a = ['colourless', 'green', 'ideas']
\end{verbatim}

\item sets the value of variable \texttt{a}
\item to see the its value, do: \texttt{print a}
\item in interactive mode, just type the variable name:

\begin{verbatim}
  >>> a
  ['colourless', 'green', 'ideas']
\end{verbatim}
\end{itemize}
\end{frame}


\begin{frame}
  \frametitle{Simple List Operations}
  \small

\begin{enumerate}
\item length: \texttt{len()}
\item indexing: \texttt{a[0]}, \texttt{a[1]}
\item indexing from right: \texttt{a[-1]}
\item slices: \texttt{a[1:3]}, \texttt{a[-2:]}
\item concatenation: \texttt{b = a + ['sleep', 'furiously']}
\item sorting: \texttt{b.sort()}
\item reversing: \texttt{b.reverse()}
\item iteration: \texttt{for item in a:}
\item all the above applies to strings as well
\item double indexing: \texttt{b[2][1]}
\item finding index: \texttt{b.index('green')}
\end{enumerate}
\end{frame}

\subsection{Strings}

\begin{frame}
  \frametitle{Simple String Operations}
  \small

\begin{enumerate}
\item joining: \texttt{c = ' '.join(b)}
\item splitting: \texttt{c.split('r')}
\item lambda expressions: \texttt{lambda x: len(x)}
\item maps: \texttt{map(lambda x: len(x), b)}
\item list comprehensions: \texttt{[(x, len(x)) for x in b]}
\item getting help: \texttt{help(list)}, \texttt{help(str)}
\end{enumerate}
\end{frame}

\subsection{Dictionaries}

\begin{frame}[fragile]
  \frametitle{Dictionaries}
  \small

\begin{itemize}
\item accessing items by their names, e.g. dictionary
\item defining entries:

\begin{verbatim}
  >>> d = {}
  >>> d['colourless'] = 'adj'
  >>> d['furiously'] = 'adv'
  >>> d['ideas'] = 'n'
\end{verbatim}

\item accessing:

\begin{verbatim}
  >>> d.keys()
  ['furiously', 'colourless', 'ideas']
  >>> d['ideas']
  'n'
  >>> d
  {'furiously': 'adv', 'colourless': 'adj', 'ideas': 'n'}
\end{verbatim}
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{Dictionaries: Iteration}

\begin{verbatim}
  >>> for w in d:
  ...    print "%s [%s]," % (w, d[w]),
  furiously [adv], colourless [adj], ideas [n],
\end{verbatim}

\begin{itemize}
\item rule of thumb: dictionary entries are like variable names
\item \textit{create} them by assigning to them\\
  \verb|x = 2| (variable), \verb|d['x'] = 2| (dictionary entry)
\item \textit{access} them by reference\\
  \verb|print x| (variable), \verb|print d['x']| (dictionary entry)
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{Dictionaries: Example: Counting Word Occurrences}
\scriptsize

\begin{verbatim}
  >>> import nltk
  >>> count = {}
  >>> for word in nltk.corpus.gutenberg.words('shakespeare-macbeth'):
  ...     word = word.lower()
  ...     if word not in count:
  ...         count[word] = 0
  ...     count[word] += 1
\end{verbatim}

Now inspect the dictionary:

\begin{verbatim}
  >>> print count['scotland']
  12
  >>> frequencies = [(freq, word) for (word, freq) in count.items()]
  >>> frequencies.sort()
  >>> frequencies.reverse()
  >>> print frequencies[:20]
  [(1986, ','), (1245, '.'), (692, 'the'), (654, "'"), (567, 'and'), (482, ':'), (399, 'to'), (365, 'of'), (360, 'i'), (255, 'a'), (246, 'that'), (242, '?'), (224, 'd'), (218, 'you'), (213, 'in'), (207, 'my'), (198, 'is'), (170, 'not'), (165, 'it'), (156, 'with')]
\end{verbatim}
\end{frame}

\section{Regular Expressions}

\subsection{Introduction}

\begin{frame}
\frametitle{Regular Expressions}
\begin{itemize}
\item string matching
\item substitution
\item patterns, classes
\item Python's regular expression module: \texttt{re}
\item NLTK's utility function: \texttt{re\_show}
\end{itemize}
\end{frame}

\subsection{Matching and Substituting}

\begin{frame}[fragile]
\frametitle{Loading module, Matching}

\begin{itemize}
\item Set up:

{\small
\begin{verbatim}
  >>> import nltk, re
  >>> sent = "colourless green ideas sleep furiously"
\end{verbatim}}

\item Matching:

{\small
\begin{verbatim}
  >>> nltk.re_show('l', sent)
  co{l}our{l}ess green ideas s{l}eep furious{l}y
  >>> nltk.re_show('green', sent)
  colourless {green} ideas sleep furiously
\end{verbatim}}
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{Substitutions}

\begin{itemize}
\item E.g. replace all instances of \texttt{l} with \texttt{s}.
\item Creates an output string (doesn't modify input)

\begin{verbatim}
  >>> re.sub('l', 's', sent)
  'cosoursess green ideas sseep furioussy'
\end{verbatim}

\item Work on substrings (NB not words)

\begin{verbatim}
  >>> re.sub('green', 'red', sent)
  'colourless red ideas sleep furiously'
\end{verbatim}
\end{itemize}
\end{frame}

\subsection{More Complex Patterns}

\begin{frame}[fragile]
\frametitle{More Complex Patterns}

\begin{itemize}
\item Disjunction:

{\small
\begin{verbatim}
  >>> nltk.re_show('(green|sleep)', sent)
  colourless {green} ideas {sleep} furiously
  >>> re.findall('(green|sleep)', sent)
  ['green', 'sleep']
\end{verbatim}}

\item Character classes, e.g. non-vowels followed by vowels:

{\small
\begin{verbatim}
  >>> nltk.re_show('[^aeiou][aeiou]', sent)
  {co}{lo}ur{le}ss g{re}en{ i}{de}as s{le}ep {fu}{ri}ously
  >>> re.findall('[^aeiou][aeiou]', sent)
  ['co', 'lo', 'le', 're', ' i', 'de', 'le', 'fu', 'ri']
\end{verbatim}}
\end{itemize}
\end{frame}

\subsection{Structured Results}

\begin{frame}[fragile]
\frametitle{Structured Results}
\begin{itemize}
\item Select a sub-part to be returned
\item e.g. non-vowel characters which appear before a vowel:

{\small
\begin{verbatim}
  >>> re.findall('([^aeiou])[aeiou]', sent)
  ['c', 'l', 'l', 'r', ' ', 'd', 'l', 'f', 'r']
\end{verbatim}}

\item generate \textit{tuples}, for later tabulation

{\small
\begin{verbatim}
  >>> re.findall('([^aeiou])([aeiou])', sent)
  [('c', 'o'), ('l', 'o'), ('l', 'e'), ('r', 'e'), (' ', 'i'), ('d', 'e'), ('l', 'e'), ('f', 'u'), ('r', 'i')]
\end{verbatim}}
\end{itemize}
\end{frame}

\section{Accessing Data and Programs}

\subsection{Accessing Files}

\begin{frame}[fragile]
\frametitle{Accessing Files and the Web}
\begin{itemize}
\item accessing local files (create \texttt{corpus.txt} first)

{\small
\begin{verbatim}
  >>> print open('corpus.txt').read() 
  Hello world.  This is a test file.
\end{verbatim}}

\item Accessing URLs on the Web:

{\small
\begin{verbatim}
  >>> from urllib import urlopen
  >>> page = urlopen("http://news.bbc.co.uk/").read()
  >>> text = nltk.clean_html(page)
  >>> print text[:60]
   BBC NEWS | News Front Page News Sport Weather World Service
\end{verbatim}}
\end{itemize}
\end{frame}

\subsection{Accessing NLTK}

\begin{frame}[fragile]
\frametitle{Accessing NLTK}

\begin{itemize}
\item modules: classes, functions
\item data structures, algorithms
\item importing, e.g. \texttt{import nltk}

{\small
\begin{verbatim}
  >>> from nltk import utilities
  >>> utilities.re_show('green', s)
  colourless {green} ideas sleep furiously
\end{verbatim}}
\end{itemize}
\end{frame}

\subsection{Accessing Corpora}

\begin{frame}[fragile]
\frametitle{Texts from Project Gutenberg}

{\small
\begin{verbatim}
  >>> nltk.corpus.gutenberg.items
  ['austen-emma', 'austen-persuasion', 'austen-sense', 'bible-kjv', 'blake-poems', 'blake-songs', 'chesterton-ball', 'chesterton-brown', 'chesterton-thursday', 'milton-paradise', 'shakespeare-caesar', 'shakespeare-hamlet', 'shakespeare-macbeth', 'whitman-leaves']
  >>> count = 0
  >>> for word in nltk.corpus.gutenberg.words('whitman-leaves'):
  ...     count += 1
  >>> print count
  154873
\end{verbatim}}
\end{frame}

\begin{frame}[fragile]
\frametitle{Brown Corpus}
\scriptsize

\begin{verbatim}
  >>> nltk.corpus.brown.items
  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r']
  >>> print nltk.corpus.brown.words('a')
  ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an', 'investigation', 'of', "Atlanta's", 'recent', 'primary', 'election', 'produced', '``', 'no', 'evidence', "''", 'that', 'any', 'irregularities', 'took', 'place', '.']
  >>> print nltk.corpus.brown.tagged_sents('a')
  [('The', 'at'), ('Fulton', 'np-tl'), ('County', 'nn-tl'), ('Grand', 'jj-tl'), ('Jury', 'nn-tl'), ('said', 'vbd'), ('Friday', 'nr'), ('an', 'at'), ('investigation', 'nn'), ('of', 'in'), ("Atlanta's", 'np$'), ('recent', 'jj'), ('primary', 'nn'), ('election', 'nn'), ('produced', 'vbd'), ('``', '``'), ('no', 'at'), ('evidence', 'nn'), ("''", "''"), ('that', 'cs'), ('any', 'dti'), ('irregularities', 'nns'), ('took', 'vbd'), ('place', 'nn'), ('.', '.')]
\end{verbatim}
\end{frame}


\begin{frame}[fragile]
\frametitle{Penn Treebank}
\scriptsize

\begin{verbatim}
  >>> print nltk.corpus.treebank.parsed_sents('wsj_0001')[0]
  (S:
    (NP-SBJ:
      (NP: (NNP: 'Pierre') (NNP: 'Vinken'))
      (,: ',')
      (ADJP: (NP: (CD: '61') (NNS: 'years')) (JJ: 'old'))
      (,: ','))
    (VP:
      (MD: 'will')
      (VP:
        (VB: 'join')
        (NP: (DT: 'the') (NN: 'board'))
        (PP-CLR:
          (IN: 'as')
          (NP: (DT: 'a') (JJ: 'nonexecutive') (NN: 'director')))
        (NP-TMP: (NNP: 'Nov.') (CD: '29'))))
    (.: '.'))
\end{verbatim}
\end{frame}

% \section{Miscellaneous Python Features}

% \subsection{Character Encoding}

% \begin{frame}
% \begin{itemize}

% \end{itemize}
% \end{frame}



\end{document}
