Lecture06

71
Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard Department of Modern Languages and Literatures Western University

Transcript of Lecture06

Page 1: Lecture06

Knowledge Representationin

Digital HumanitiesAntonio Jiménez Mavillard

Department of Modern Languages and LiteraturesWestern University

Page 2: Lecture06

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

Page 3: Lecture06

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

Page 4: Lecture06

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

Page 5: Lecture06

Chapter 6Domain Modelling

andComplex Object Representation

in Python

1. More complex data types2. Object-oriented programming

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard5

Page 6: Lecture06

Chapter 6

1 More complex data types 1.1 Lists 1.2 Tuples 1.3 Dictionaries

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard6

Page 7: Lecture06

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

Page 8: Lecture06

More complex data types

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard8

Page 9: Lecture06

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

Page 10: Lecture06

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

Page 11: Lecture06

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

Page 12: Lecture06

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

Page 13: Lecture06

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]: 

Page 14: Lecture06

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]: 

Page 15: Lecture06

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]

Page 16: Lecture06

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]

Page 17: Lecture06

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]: 

Page 18: Lecture06

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

Page 19: Lecture06

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

Page 20: Lecture06

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

Page 21: Lecture06

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]:

Page 22: Lecture06

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

Page 23: Lecture06

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]: 

Page 24: Lecture06

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

Page 25: Lecture06

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

Page 26: Lecture06

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

Page 27: Lecture06

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

Page 28: Lecture06

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

Page 29: Lecture06

References

Downey, Allen. “Chapter 10: Lists.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard29

Page 30: Lecture06

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

Page 31: Lecture06

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

Page 32: Lecture06

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

Page 33: Lecture06

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

Page 34: Lecture06

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

Page 35: Lecture06

Dictionaries

* Access · All items - Syntax: dictionary.items() - Example: d.items() - It returns a list of tuples

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard35

Page 36: Lecture06

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

Page 37: Lecture06

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']

Page 38: Lecture06

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

Page 39: Lecture06

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]: 

Page 40: Lecture06

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'}

Page 41: Lecture06

Dictionaries

* Deleting items

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard41

In [17]: del d[2]

In [18]: dOut[18]: {4: 'd'}

In [19]: 

Page 42: Lecture06

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

Page 43: Lecture06

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

Page 44: Lecture06

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

Page 45: Lecture06

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

Page 46: Lecture06

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

Page 47: Lecture06

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

Page 48: Lecture06

Dictionaries

* Exercise 4 · Example:

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard48

Page 49: Lecture06

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

Page 50: Lecture06

References

Downey, Allen. “Chapter 11: Dictionaries.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard50

Page 51: Lecture06

Object-oriented programming

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard51

Page 52: Lecture06

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

Page 53: Lecture06

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

Page 54: Lecture06

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

Page 55: Lecture06

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

Page 56: Lecture06

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

Page 57: Lecture06

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

Page 58: Lecture06

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

Page 59: Lecture06

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

Page 60: Lecture06

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

Page 61: Lecture06

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

Page 62: Lecture06

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

Page 63: Lecture06

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

Page 64: Lecture06

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

Page 65: Lecture06

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

Page 66: Lecture06

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

Page 67: Lecture06

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

Page 68: Lecture06

Assignment

* Assignment 6: The library · Project + Read the description of the library in the attached file

Knowledge Representation in Digital HumanitiesAntonio Jiménez Mavillard68

Page 69: Lecture06

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

Page 70: Lecture06

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

Page 71: Lecture06

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