Python For Linguists
Python For Linguists
Book Review
Python for Linguists
Michael Hammond
(University of Arizona)
Reviewed by
Benjamin Roth
University of Vienna
and
Michael Wiegand
Alpen-Adria-Universität Klagenfurt
Teaching programming skills is a hard task. It is even harder if one targets an audience
with no or little mathematical background. Although there are books on programming
that target such groups, they often fail to raise or maintain interest due to artificial
examples that lack reference to the professional issues that the audience typically face.
This book fills the gap by addressing linguistics, a profession and academic subject for
which basic knowledge of script programming is becoming more and more important.
The book Python for Linguists by Michael Hammond is an introductory Python course
targeted at linguists with no prior programming background. It succeeds previous
books for Perl (Hammond 2008) and Java (Hammond 2002) by the same author, and
reflects the current de facto prevalence of Python when it comes to adoption and
available packages for natural language processing.
We feel it necessary to clarify that the book aims at (general) linguists in the broad
sense rather than computational linguists. Its aim is to teach linguists the fundamental
concepts of programming using typical examples from linguistics. The book should not
be mistaken as a course for learning basic algorithms in computational linguistics. We
acknowledge that the author nowhere makes such a claim, however, given the thematic
proximity to computational linguistics, one should have the right expectation before
working with the book.
Chapters 1–5 lay the foundations of the Python programming language, introduc-
ing the most important language constructs but deferring object oriented programming
to a later part of the book. The focus in Chapters 1 and 2 covers the basic data types
(numbers, strings, dictionaries), with a particular emphasis on simple string operations,
and introduces some more advanced concepts such as mutability.
Chapters 3–5 introduce control structures, input–output operations, and modules.
The book goes at great length to visualize the program flow and the state of different
variables for different steps in a program execution, which is certainly very helpful for
learners with no prior programming experience. The book also guides the learner to
understand certain error types that frequently occur in computer programming (but
might be unintuitive for beginners). For example, when discussing function calls, much
care is devoted to pointing out the unintended consequences stemming from mutability
and side effects.
https://doi.org/10.1162/coli r 00400
1 https://docs.python.org/3/tutorial/inputoutput.html#tut-files.
2 https://www.python.org/dev/peps/pep-0008/#id36.
3 https://www.gutenberg.org/.
2
Book Reviews
parsing Web data and parallel processing. The elaborate example of corpus collection
nicely brings together many of the concepts introduced earlier in the book, and serves
as a convincing application for showcasing the usefulness of parallel processing.
The crawler contains a language guesser based on the most frequent Welsh words.
The simple frequency-based heuristic of the language guesser is the only part in the
book that shows the potential of quantitative methods for text processing (rather than
rule-based methods). Given the prevalence of statistical methods in computational lin-
guistics, it would have been desirable if the book spent more time on the opportunities
and pitfalls of using empirical methods for solving practical tasks.
In the last part of the book, Chapters 9–12, more attention is directed towards dif-
ferent programming paradigms that are possible within the Python language. In par-
ticular, object oriented programming (OOP), event-driven programming (and how to
use it for implementing graphical user interfaces), and the functional programming
capabilities of Python are presented.
Even though OOP has been used throughout the book implicitly (since fundamen-
tally everything has the status of an object in Python), it is only very late in the book that
the concepts behind OOP are explicitly discussed. It seems that the book treats OOP as
an advanced concept that would be too confusing to confront beginners with early on
(which might not be the case if OOP concepts are reduced to their main ideas, such as
grouping data and functionality together). In order to illustrate the logic behind inheritance,
the book uses examples from linguistic hierarchies, for example, syllabification trees
(a syllable has an onset and a rhyme, which are all subspans of a word with a spelling and
pronunciation), rather than the typical examples usually used for introducing OOP (e.g.,
a car is a vehicle and has wheels and a location).
A chapter on graphical user interfaces builds on the Tkinter framework, and the
most basic concepts (windows, buttons) are introduced. A small graphical demo of the
Porter stemmer covered earlier is built as an example for an user interface. Unfortu-
nately there is very little connection to any linguistic use case in this chapter, and user
interfaces would have been a great opportunity for showcasing some annotation prob-
lem (e.g., rating fluency vs. adequacy of sentences). One could argue that a more modern
(and more flexible) approach to user interfaces building on browser-based interaction
would have been more useful.
Finally, the Python-specific functional programming language constructs are dis-
cussed, and the undesirable consequences of non-functional programming (mutability,
side effects) as well as the advantages of functional programming (better control, paral-
lelization, conciseness) are highlighted. An Appendix gives a brief introduction to basic
processing with the natural language toolkit (NLTK) (Loper and Bird 2002), such as
using its corpora or pre-processing raw text.
General Observations. The book is clearly structured. The author carefully and con-
sistently arranged the different elements of the Python programming language to make
it as accessible as possible. Unlike many tutorials for Python available on the Web, this
book not just details the syntax of the programming language but it takes the time to
convey important programming skills, such as a divide-and-conquer approach to mod-
ularize code. Not at all places is the output of the sample code displayed. This may
sometimes slow down the process of understanding it. However, the complete code of
the book is made publicly available so that readers may test it on their particular Python
installation.
Each chapter concludes with a set of exercises. We consider them highly useful in
further deepening the subject matter presented in the chapters. The exercises mostly
range from simple questions investigating particular details of the concepts presented
3
Computational Linguistics Volume 47, Number 1
in the chapter, for example, by modifying the existing examples, up to writing smaller
programs from scratch solving particular linguistic tasks.
The linguistic examples chosen may occasionally look a bit construed. Yet, overall
they are the main asset of this book and they will be more interesting to the target au-
dience than the examples found in other existing books.
After reading this book, the reader will have a solid grasp of the Python program-
ming language. It should suffice for solving typical daily tasks for linguistics, such as
restructuring files or computing low-level statistics (e.g., collecting word frequencies).
The knowledge presented in the book may also enable the reader to study more ad-
vanced topics related to Python, for example, computational linguistic algorithms, data
science, or machine learning.
Some readers may be surprised to see only few external libraries discussed in the
book. However, this may be on purpose and be in line with the didactic concept pursued
by the author. The aim of this book is to teach basic programming skills, namely, how
to understand and structure code for data processing. This can better be conveyed by
showing complete solutions to specific tasks rather than showing how to call a partic-
ular Python library. Novices are more likely to acquire a deeper understanding of how
programs are written by explaining a programming solution from scratch. Of course,
we should also bear in mind that many of the external libraries currently available
for Python may be short-lived and not available or maintained in few years times.
Nonetheless, we would have appreciated a statement telling the reader to consider
publicly available libraries in practice.
Summary. The main goal of Python for Linguists is to teach basic programming
skills to linguists that do not have any prior background in computer science. By using
linguistically motivated examples throughout, the book does a great job making the ma-
terial covered relevant to the target group. The structure of the book is well thought-out,
and ensures that prerequisites are covered before moving to more advanced topics. This
and the exercises that come with each chapter would make the book a great companion
for a foundational programming course targeted at linguists.
References
Hammond, Michael. 2002. Programming for Loper, Edward and Steven Bird. 2002. NLTK:
Linguists: Java Technology for Language the natural language toolkit.
Researchers. Wiley Online Library. Porter, Martin F. 1980. An algorithm for
Hammond, Michael. 2008. Programming for suffix stripping. Program, 14(3):
Linguists: Perl for Language Researchers. 130–137.
John Wiley & Sons.
Benjamin Roth is a joint professor of Computer Science and Philology at the University of Vienna,
Austria. His research interests are the extraction of knowledge from text with statistical methods
and knowledge-supervised learning. His e-mail address is [email protected].
Michael Wiegand is a professor of Computational Linguistics at the Digital Age Research
Center, Alpen-Adria-Universität Klagenfurt, Austria. His research interests include abusive
language detection, sentiment analysis, and information extraction. His e-mail address is
[email protected].