Post on 11-May-2015
Knowledge Representationin
Digital HumanitiesAntonio Jiménez Mavillard
Department of Modern Languages and LiteraturesWestern University
Lecture 6
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard
* Contents: 1. Why this lecture? 2. Discussion 3. Chapter 6 4. Assignment 5. Bibliography
2
Why this lecture?
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard
* This lecture... · formalizes the modelling of real-world domains · goes in depth into the representation of complex objects
3
Last assignment discussion
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard
* Time to... · consolidate ideas and concepts dealt in the readings · discuss issues arised in the specific solutions to the projects
4
Chapter 6Domain Modelling
andComplex Object Representation
in Python
1. More complex data types2. Object-oriented programming
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard5
Chapter 6
1 More complex data types 1.1 Lists 1.2 Tuples 1.3 Dictionaries
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard6
Chapter 6
2 Object-oriented programming 2.1 General ideas 2.2 Classes and objects 2.3 Attributes and methods 2.4 Modelling domains
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard7
More complex data types
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard8
Lists
* Debugging · Syntax errors + not closing [] · Logic errors + accessing to a non-existing element - index out of range
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard9
Lists
* Debugging · Semantic errors + not accessing the first and/or last element + not considering the empty list, [] + modifying a list inside a function
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard10
Lists
* list · Type for lists · Examples: [1, 2, 3], ['a', 'b', 'c'], [1, 'abc', [], True] · A list is a sequence of values
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard11
Lists* Indexes · Same index system as strings · Access: + A whole + One element at a time + A slice · Range: 0 .. list's length - 1 · The index [-1] accesses the last element
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard12
Lists
* Indexes
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard13
In [1]: l = [1, 'abc', [], True]
In [2]: lOut[2]: [1, 'abc', [], True]
In [3]: l[0]Out[3]: 1
In [4]: l[1:3]Out[4]: ['abc', []]
In [5]:
Lists
* Mutability · Lists are mutable (can be modified)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard14
In [1]: l = [1, 'abc', [], True]
In [2]: lOut[2]: [1, 'abc', [], True]
In [3]:
Lists
* Mutability
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard15
In [3]: l[1] = 3.1416
In [4]: lOut[4]: [1, 3.1416, [], True]
In [5]: l[2:4] = [False, 'xyz']
In [6]: lOut[6]: [1, 3.1416, False, 'xyz']
In [7]: l.append(2)
In [8]: lOut[8]: [1, 3.1416, False, 'xyz', 2]
Lists
* Mutability
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard16
In [9]: l.insert(2, [1, 2, 3])
In [10]: lOut[10]: [1, 3.1416, [1, 2, 3], False, 'xyz', 2]
In [11]: l.remove(False)
In [12]: lOut[12]: [1, 3.1416, [1, 2, 3], 'xyz', 2]
In [13]: del l[1]
In [14]: lOut[14]: [1, [1, 2, 3], 'xyz', 2]
Lists
* Mutability
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard17
In [15]: l.extend([True, 7.3])
In [16]: lOut[16]: [1, [1, 2, 3], 'xyz', 2, True, 7.3]
In [17]: x = l.pop(3)
In [18]: lOut[18]: [1, [1, 2, 3], 'xyz', True, 7.3]
In [19]: xOut[19]: 2
In [20]:
Lists* Some functions and operators · index(x): returns the (first) index of the element x · count(x): returns the number of ocurrences of x · sort(): orders the elements of the list · reverse(): inverts the elemens of the list
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard18
Lists
* Exercise 1 · Write a function called invert that returns the reverse of a list (do not use the function reverse of lists)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard19
Lists
* Exercise 1 (solution)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard20
def invert(l): result = [] for elem in l: result.insert(0, elem) return result
Lists
* Some functions and operators · The function range generates a list of ordered integers
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard21
In [1]: range(10)Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [2]: range(2, 8)Out[2]: [2, 3, 4, 5, 6, 7]
In [3]: range(0, 10, 3)Out[3]: [0, 3, 6, 9]
In [4]:
Lists* Some functions and operators · +: concatenates lists · *: repeats a list a given number of times · [:]: slices a list - General syntax: l[n:m] - l[n:] ≡ l[n:len(l)] - l[:m] ≡ l[0:m] - l[:] ≡ l[0:len(l)] ≡ l
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard22
Lists
* Some functions and operators
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard23
In [1]: l1 = ['a', 'b', 'c']
In [2]: l2 = ['d', 'e', 'f']
In [3]: l3 = l1 + l2
In [4]: l3Out[4]: ['a', 'b', 'c', 'd', 'e', 'f']
In [5]: l4 = l1 * 3
In [5]: l4Out[5]: ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
In [6]:
Lists
* Some functions and operators · The operator in checks if a value is contained in a list
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard24
Lists
* Exercise 2 · Write a function called repeat that returns the result of repeating a list a number of times (do not use the operator * of lists)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard25
Lists
* Exercise 2 (solution)
(Consider the case n=0)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard26
def repeat(l, n): result = [] i = 1 while i <= n: result = result + l return result
Lists
* Lists as arguments/parameters of functions · The parameter is a reference to the list · Modifying the paremeter (list inside the function) implies modifying the argument (list passed to the function) · To avoid this, make a copy of the list with [:]
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard27
Lists* Lists vs strings · Lists are mutable · Strings are inmutable · A string is a sequence of characters · A list is a sequence of values · A list of characters is not a string · The function list converts a string to a list
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard28
References
Downey, Allen. “Chapter 10: Lists.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard29
Tuples
* tuple · Type for tuples · Examples: (1, 2, 3), ('a', 'b', 'c'), (1, 'abc', [], True) · A tuple is an inmutable list
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard30
Dictionaries* Debugging · Syntax errors + not closing {} · Logic errors + accessing to a non-existing element - key not found · Semantic errors + modifying a dictionary inside a function
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard31
Dictionaries* dict · Type for dictionaries · A dictionary is a kind of list that establishes a mapping between a set of indices (called keys) and a set of values · Keys can be any type (not exclusively integer) · Each pair key-value is called item
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard32
Dictionaries
* dict · Examples: {1:'a', 2:'b', 3:'c'}, {'one':'uno', 'two':'dos', 'three':'tres', 'four':'cuatro', 'five':'cinco', 'six':'seis', 'seven':'siete', 'eight':'ocho', 'nine':'nueve', 'ten':'diez',}
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard33
Dictionaries
* Access · As a whole - Example: d · Its values one at a time (lookup) - Syntax: dictionary[key] - Example: d[1]
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard34
Dictionaries
* Access · All items - Syntax: dictionary.items() - Example: d.items() - It returns a list of tuples
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard35
Dictionaries
* Access · Only (all) keys - Syntax: dictionary.keys() - Example: d.items() · Only (all) keys - Syntax: dictionary.keys() - Example: d.items()
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard36
Dictionaries* Access
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard37
In [1]: d = {1: 'a', 2: 'b', 3: 'c'}
In [2]: dOut[2]: {1: 'a', 2: 'b', 3: 'c'}
In [3]: d[1]Out[3]: 'a'
In [4]: d.items()Out[4]: [(1, 'a'), (2, 'b'), (3, 'c')]
In [5]: d.keys()Out[5]: [1, 2, 3]
In [6]: d.values()Out[6]: ['a', 'b', 'c']
Dictionaries
* Modifying dictionaries · Modifying existing item - Syntax: dictionary[key] = new_value - Example: d[1] = 'x' · Adding new item - Syntax: dictionary[new_key] = value - Example: d[4] = 'd'
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard38
Dictionaries
* Modifying dictionaries
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard39
In [7]: d[1] = 'x'
In [8]: dOut[8]: {1: 'x', 2: 'b', 3: 'c'}
In [9]: d[4] = 'd'
In [10]: dOut[10]: {1: 'x', 2: 'b', 3: 'c', 4: 'd'}
In [11]:
Dictionaries* Deleting items
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard40
In [11]: x = d.popitem()
In [12]: xOut[12]: (1, 'x')
In [13]: dOut[13]: {2: 'b', 3: 'c', 4: 'd'}
In [14]: y = d.pop(3)
In [15]: yOut[15]: 'c'
In [16]: dOut[16]: {2: 'b', 4: 'd'}
Dictionaries
* Deleting items
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard41
In [17]: del d[2]
In [18]: dOut[18]: {4: 'd'}
In [19]:
Dictionaries
* Some functions and operators · update(d): receives a dictionary d and - if d contains keys included in the dictionary, this function updates the values of the dictionary with the values of d
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard42
Dictionaries
* Some functions and operators · update(d): receives a dictionary d and - if d contains keys not included in the dictionary, the new items (pairs key-value) are added to the dictionary
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard43
Dictionaries
* Some functions and operators · The operator in checks if a key is contained in a dictionary
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard44
Dictionaries
* Exercise 3 · Write a function called histogram that receives a string and returns the frequency of each letter
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard45
Dictionaries
* Exercise 3 (solution)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard46
def histogram(word): d = {} for letter in word: if letter in d: d[letter] += 1 else: d[letter] = 1 return d
Dictionaries
* Exercise 4 · Write a function called invert_histogram that receives an histogram and returns the inverted histogram, where the keys are the frequencies and the values are lists of the letters that have that frequency
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard47
Dictionaries
* Exercise 4 · Example:
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard48
Dictionaries
* Exercise 4 (solution)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard49
def invert_histogram(h): inverse = {} for key in h: value = h[key] if value in inverted_h: inverse[value].append(key) else: inverse[value] = [key] return inverse
References
Downey, Allen. “Chapter 11: Dictionaries.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard50
Object-oriented programming
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard51
General ideas
* Programs made up of object definitions and functions that operate on them* Objects correspond to concepts in the real world* Functions correspond to the ways real-world objects interact
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard52
General ideas
* Debugging · Logic errors + accessing to a non-existing element - attribute not found - attribute not initialized - method not found
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard53
Classes and objects* Classes · A class is the representation of an idea or a concept · A class is a user-defined type · Examples: Author, Book · Syntax: class ClassName(SuperclassNames): attributes and methods
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard54
Classes and objects* Objects · An object is an instance of the class · An object is a concrete element that belongs to a class of objects · Examples: William Shakespeare (Author), Romeo and Juliet (Book) · Syntax: object_name = ClassName(arguments)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard55
Attributes and methods
* A class is defined by features that are common to all objects that belong to the class* Those features are: · Attributes · Methods
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard56
Attributes and methods
* Attributes · Data · Syntax: object.attribute · Take specific values for each object · Examples: base, height (Rectangle)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard57
Attributes and methods
* Methods · Functions that operate with data · Syntax: object.method(arguments) · Get different results for each object · Examples: calculateArea()
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard58
Modelling domains
* Exercise 5 · In Literature, authors write novels, poems, short stories... Let us call them books in general · Model the classes Author and Book (abstract relevant data in both cases)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard59
Modelling domains
* Exercise 5 · Write the method birthday for the class Author that increases by one the author's age
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard60
Modelling domains
* Exercise 5 · Write the method write for the class Author that receives a title and a text and add a new Book with this author, title and text to his/her bibliography
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard61
Modelling domains
* Exercise 5 · Write the method read for the class Author that receives a title, searches the book in his/her bibliography and prints its text · Add as many attributes as needed
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard62
Modelling domains
* Exercise 5 (solution)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard63
class Author: def __init__(self, name, age, bibliography={}): self.name = name self.age = age self.bibliography = bibliography def birthday(self): self.age += 1 def write(self, title, text): new_book = Book(title, self, text) self.bibliography[title] = new_book
Modelling domains
* Exercise 5 (solution)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard64
def read(self, title): book = self.bibliography[title] print book.text def __repr__(self): return self.name
Modelling domains
* Exercise 5 (solution)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard65
class Book: def __init__(self, title, author, text): self.title = title self.author = author self.text = text def __repr__(self): return self.title
References
Downey, Allen. “Chapter 15: Classes and Objects.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.
Downey, Allen. “Chapter 16: Classes and Functions.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.
Downey, Allen. “Chapter 17: Classes and Methods.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard66
Assignment* Assignment 6: The library · Readings + Data Structure Selection (Think Python) + An Introduction to OOP Using Python (A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences)
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard67
Assignment
* Assignment 6: The library · Project + Read the description of the library in the attached file
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard68
Assignment
* Assignment 6: The library · Project + Model the scenario described by defining classes and using suitable data types + Try the solution with the test provided
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard69
References
Downey, Allen. “Chapter 13: Case Study: Data Structure Selection.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.
Lin, Johnny Wei-Bing. “Chapter 7: An Introduction to OOP Using Python: Part I—Basic Principles and Syntax.” A Hands-on
Introduction to Using Python in the Atmospheric and Oceanic Sciences. San Francisco: Creative Commons, 2012.
Print.
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard70
Bibliography
Downey, Allen. Think Python. Sebastopol, CA: O’Reilly, 2012. Print.
Lin, Johnny Wei-Bing. A Hands-on Introduction to Using Python in the Atmospheric and Oceanic Sciences. San Francisco:
Creative Commons, 2012. Print.
Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard71