SlideShare a Scribd company logo
Knowledge Representation
in
Digital Humanities
Antonio Jiménez Mavillard
Department of Modern Languages and Literatures
Western University
Lecture 6
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 6
Domain Modelling
and
Complex Object Representation
in Python
1. More complex data types
2. Object-oriented programming
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard5
Chapter 6
1 More complex data types
1.1 Lists
1.2 Tuples
1.3 Dictionaries
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard7
More complex data types
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard12
Lists
* Indexes
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard13
In [1]: l = [1, 'abc', [], True]
In [2]: l
Out[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 Humanities
Antonio Jiménez Mavillard14
In [1]: l = [1, 'abc', [], True]
In [2]: l
Out[2]: [1, 'abc', [], True]
In [3]: 
Lists
* Mutability
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard15
In [3]: l[1] = 3.1416
In [4]: l
Out[4]: [1, 3.1416, [], True]
In [5]: l[2:4] = [False, 'xyz']
In [6]: l
Out[6]: [1, 3.1416, False, 'xyz']
In [7]: l.append(2)
In [8]: l
Out[8]: [1, 3.1416, False, 'xyz', 2]
Lists
* Mutability
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard16
In [9]: l.insert(2, [1, 2, 3])
In [10]: l
Out[10]: [1, 3.1416, [1, 2, 3], False, 'xyz', 2]
In [11]: l.remove(False)
In [12]: l
Out[12]: [1, 3.1416, [1, 2, 3], 'xyz', 2]
In [13]: del l[1]
In [14]: l
Out[14]: [1, [1, 2, 3], 'xyz', 2]
Lists
* Mutability
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard17
In [15]: l.extend([True, 7.3])
In [16]: l
Out[16]: [1, [1, 2, 3], 'xyz', 2, True, 7.3]
In [17]: x = l.pop(3)
In [18]: l
Out[18]: [1, [1, 2, 3], 'xyz', True, 7.3]
In [19]: x
Out[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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard19
Lists
* Exercise 1 (solution)
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard22
Lists
* Some functions and operators
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard23
In [1]: l1 = ['a', 'b', 'c']
In [2]: l2 = ['d', 'e', 'f']
In [3]: l3 = l1 + l2
In [4]: l3
Out[4]: ['a', 'b', 'c', 'd', 'e', 'f']
In [5]: l4 = l1 * 3
In [5]: l4
Out[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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard25
Lists
* Exercise 2 (solution)
(Consider the case n=0)
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard28
References
Downey, Allen. “Chapter 10: Lists.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print.
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard34
Dictionaries
* Access
· All items
- Syntax: dictionary.items()
- Example: d.items()
- It returns a list of tuples
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard36
Dictionaries
* Access
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard37
In [1]: d = {1: 'a', 2: 'b', 3: 'c'}
In [2]: d
Out[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 Humanities
Antonio Jiménez Mavillard38
Dictionaries
* Modifying dictionaries
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard39
In [7]: d[1] = 'x'
In [8]: d
Out[8]: {1: 'x', 2: 'b', 3: 'c'}
In [9]: d[4] = 'd'
In [10]: d
Out[10]: {1: 'x', 2: 'b', 3: 'c', 4: 'd'}
In [11]: 
Dictionaries
* Deleting items
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard40
In [11]: x = d.popitem()
In [12]: x
Out[12]: (1, 'x')
In [13]: d
Out[13]: {2: 'b', 3: 'c', 4: 'd'}
In [14]: y = d.pop(3)
In [15]: y
Out[15]: 'c'
In [16]: d
Out[16]: {2: 'b', 4: 'd'}
Dictionaries
* Deleting items
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard41
In [17]: del d[2]
In [18]: d
Out[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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard43
Dictionaries
* Some functions and operators
· The operator in checks if a key is
contained in a dictionary
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard45
Dictionaries
* Exercise 3 (solution)
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard47
Dictionaries
* Exercise 4
· Example:
Knowledge Representation in Digital Humanities
Antonio Jiménez Mavillard48
Dictionaries
* Exercise 4 (solution)
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard50
Object-oriented programming
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard62
Modelling domains
* Exercise 5 (solution)
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard67
Assignment
* Assignment 6: The library
· Project
+ Read the description of the library in
the attached file
Knowledge Representation in Digital Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio 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 Humanities
Antonio Jiménez Mavillard71

More Related Content

Similar to Lecture06

Lecture09
Lecture09Lecture09
Lecture09
mavillard
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
lokeshgoud13
 
Lecture04
Lecture04Lecture04
Lecture04
mavillard
 
Datatypes in Python.pdf
Datatypes in Python.pdfDatatypes in Python.pdf
Datatypes in Python.pdf
king931283
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
gjcross
 
Python
PythonPython
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
AnitaDevi158873
 
Python data type
Python data typePython data type
Python data type
Jaya Kumari
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
ssuser8e50d8
 
BFNP –FunctionalProgram-mingNielsHallenbergT.docx
BFNP –FunctionalProgram-mingNielsHallenbergT.docxBFNP –FunctionalProgram-mingNielsHallenbergT.docx
BFNP –FunctionalProgram-mingNielsHallenbergT.docx
AASTHA76
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdf
MdAshik35
 
mooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthonmooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthon
garvitbisht27
 
ppt_pspp.pdf
ppt_pspp.pdfppt_pspp.pdf
ppt_pspp.pdf
ShereenAhmedMohamed
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
Koteswari Kasireddy
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
Eelco Visser
 
Brixon Library Technology Initiative
Brixon Library Technology InitiativeBrixon Library Technology Initiative
Brixon Library Technology Initiative
Basil Bibi
 
02 Python Data Structure.pptx
02 Python Data Structure.pptx02 Python Data Structure.pptx
02 Python Data Structure.pptx
ssuser88c564
 
Project presentation PPT.pdf this is help for student who doing this complier...
Project presentation PPT.pdf this is help for student who doing this complier...Project presentation PPT.pdf this is help for student who doing this complier...
Project presentation PPT.pdf this is help for student who doing this complier...
AmitSingh395981
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
mohitesoham12
 
Python standard data types
Python standard data typesPython standard data types
Python standard data types
Learnbay Datascience
 

Similar to Lecture06 (20)

Lecture09
Lecture09Lecture09
Lecture09
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
 
Lecture04
Lecture04Lecture04
Lecture04
 
Datatypes in Python.pdf
Datatypes in Python.pdfDatatypes in Python.pdf
Datatypes in Python.pdf
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
 
Python
PythonPython
Python
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
Python data type
Python data typePython data type
Python data type
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
BFNP –FunctionalProgram-mingNielsHallenbergT.docx
BFNP –FunctionalProgram-mingNielsHallenbergT.docxBFNP –FunctionalProgram-mingNielsHallenbergT.docx
BFNP –FunctionalProgram-mingNielsHallenbergT.docx
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdf
 
mooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthonmooc_presentataion_mayankmanral on the subject puthon
mooc_presentataion_mayankmanral on the subject puthon
 
ppt_pspp.pdf
ppt_pspp.pdfppt_pspp.pdf
ppt_pspp.pdf
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
 
Brixon Library Technology Initiative
Brixon Library Technology InitiativeBrixon Library Technology Initiative
Brixon Library Technology Initiative
 
02 Python Data Structure.pptx
02 Python Data Structure.pptx02 Python Data Structure.pptx
02 Python Data Structure.pptx
 
Project presentation PPT.pdf this is help for student who doing this complier...
Project presentation PPT.pdf this is help for student who doing this complier...Project presentation PPT.pdf this is help for student who doing this complier...
Project presentation PPT.pdf this is help for student who doing this complier...
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Python standard data types
Python standard data typesPython standard data types
Python standard data types
 

Recently uploaded

System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
saastr
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
GDSC PJATK
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
flufftailshop
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 

Recently uploaded (20)

System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 

Lecture06

  • 1. Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard Department of Modern Languages and Literatures Western University
  • 2. Lecture 6 Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard * Contents: 1. Why this lecture? 2. Discussion 3. Chapter 6 4. Assignment 5. Bibliography 2
  • 3. Why this lecture? Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard * This lecture... · formalizes the modelling of real-world domains · goes in depth into the representation of complex objects 3
  • 4. Last assignment discussion Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard * Time to... · consolidate ideas and concepts dealt in the readings · discuss issues arised in the specific solutions to the projects 4
  • 5. Chapter 6 Domain Modelling and Complex Object Representation in Python 1. More complex data types 2. Object-oriented programming Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard5
  • 6. Chapter 6 1 More complex data types 1.1 Lists 1.2 Tuples 1.3 Dictionaries Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard6
  • 7. 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 Humanities Antonio Jiménez Mavillard7
  • 8. More complex data types Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard8
  • 9. Lists * Debugging · Syntax errors + not closing [] · Logic errors + accessing to a non-existing element - index out of range Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard9
  • 10. 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 Humanities Antonio Jiménez Mavillard10
  • 11. 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 Humanities Antonio Jiménez Mavillard11
  • 12. 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 Humanities Antonio Jiménez Mavillard12
  • 13. Lists * Indexes Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard13 In [1]: l = [1, 'abc', [], True] In [2]: l Out[2]: [1, 'abc', [], True] In [3]: l[0] Out[3]: 1 In [4]: l[1:3] Out[4]: ['abc', []] In [5]: 
  • 14. Lists * Mutability · Lists are mutable (can be modified) Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard14 In [1]: l = [1, 'abc', [], True] In [2]: l Out[2]: [1, 'abc', [], True] In [3]: 
  • 15. Lists * Mutability Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard15 In [3]: l[1] = 3.1416 In [4]: l Out[4]: [1, 3.1416, [], True] In [5]: l[2:4] = [False, 'xyz'] In [6]: l Out[6]: [1, 3.1416, False, 'xyz'] In [7]: l.append(2) In [8]: l Out[8]: [1, 3.1416, False, 'xyz', 2]
  • 16. Lists * Mutability Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard16 In [9]: l.insert(2, [1, 2, 3]) In [10]: l Out[10]: [1, 3.1416, [1, 2, 3], False, 'xyz', 2] In [11]: l.remove(False) In [12]: l Out[12]: [1, 3.1416, [1, 2, 3], 'xyz', 2] In [13]: del l[1] In [14]: l Out[14]: [1, [1, 2, 3], 'xyz', 2]
  • 17. Lists * Mutability Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard17 In [15]: l.extend([True, 7.3]) In [16]: l Out[16]: [1, [1, 2, 3], 'xyz', 2, True, 7.3] In [17]: x = l.pop(3) In [18]: l Out[18]: [1, [1, 2, 3], 'xyz', True, 7.3] In [19]: x Out[19]: 2 In [20]: 
  • 18. 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 Humanities Antonio Jiménez Mavillard18
  • 19. 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 Humanities Antonio Jiménez Mavillard19
  • 20. Lists * Exercise 1 (solution) Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard20 def invert(l):     result = []     for elem in l:         result.insert(0, elem)     return result
  • 21. Lists * Some functions and operators · The function range generates a list of ordered integers Knowledge Representation in Digital Humanities Antonio 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]:
  • 22. 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 Humanities Antonio Jiménez Mavillard22
  • 23. Lists * Some functions and operators Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard23 In [1]: l1 = ['a', 'b', 'c'] In [2]: l2 = ['d', 'e', 'f'] In [3]: l3 = l1 + l2 In [4]: l3 Out[4]: ['a', 'b', 'c', 'd', 'e', 'f'] In [5]: l4 = l1 * 3 In [5]: l4 Out[5]: ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] In [6]: 
  • 24. Lists * Some functions and operators · The operator in checks if a value is contained in a list Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard24
  • 25. 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 Humanities Antonio Jiménez Mavillard25
  • 26. Lists * Exercise 2 (solution) (Consider the case n=0) Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard26 def repeat(l, n):     result = []     i = 1     while i <= n:         result = result + l     return result
  • 27. 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 Humanities Antonio Jiménez Mavillard27
  • 28. 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 Humanities Antonio Jiménez Mavillard28
  • 29. References Downey, Allen. “Chapter 10: Lists.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print. Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard29
  • 30. 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 Humanities Antonio Jiménez Mavillard30
  • 31. 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 Humanities Antonio Jiménez Mavillard31
  • 32. 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 Humanities Antonio Jiménez Mavillard32
  • 33. 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 Humanities Antonio Jiménez Mavillard33
  • 34. Dictionaries * Access · As a whole - Example: d · Its values one at a time (lookup) - Syntax: dictionary[key] - Example: d[1] Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard34
  • 35. Dictionaries * Access · All items - Syntax: dictionary.items() - Example: d.items() - It returns a list of tuples Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard35
  • 36. Dictionaries * Access · Only (all) keys - Syntax: dictionary.keys() - Example: d.items() · Only (all) keys - Syntax: dictionary.keys() - Example: d.items() Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard36
  • 37. Dictionaries * Access Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard37 In [1]: d = {1: 'a', 2: 'b', 3: 'c'} In [2]: d Out[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']
  • 38. 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 Humanities Antonio Jiménez Mavillard38
  • 39. Dictionaries * Modifying dictionaries Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard39 In [7]: d[1] = 'x' In [8]: d Out[8]: {1: 'x', 2: 'b', 3: 'c'} In [9]: d[4] = 'd' In [10]: d Out[10]: {1: 'x', 2: 'b', 3: 'c', 4: 'd'} In [11]: 
  • 40. Dictionaries * Deleting items Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard40 In [11]: x = d.popitem() In [12]: x Out[12]: (1, 'x') In [13]: d Out[13]: {2: 'b', 3: 'c', 4: 'd'} In [14]: y = d.pop(3) In [15]: y Out[15]: 'c' In [16]: d Out[16]: {2: 'b', 4: 'd'}
  • 41. Dictionaries * Deleting items Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard41 In [17]: del d[2] In [18]: d Out[18]: {4: 'd'} In [19]: 
  • 42. 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 Humanities Antonio Jiménez Mavillard42
  • 43. 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 Humanities Antonio Jiménez Mavillard43
  • 44. Dictionaries * Some functions and operators · The operator in checks if a key is contained in a dictionary Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard44
  • 45. Dictionaries * Exercise 3 · Write a function called histogram that receives a string and returns the frequency of each letter Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard45
  • 46. Dictionaries * Exercise 3 (solution) Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard46 def histogram(word):     d = {}     for letter in word:         if letter in d:             d[letter] += 1         else:             d[letter] = 1     return d
  • 47. 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 Humanities Antonio Jiménez Mavillard47
  • 48. Dictionaries * Exercise 4 · Example: Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard48
  • 49. Dictionaries * Exercise 4 (solution) Knowledge Representation in Digital Humanities Antonio 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
  • 50. References Downey, Allen. “Chapter 11: Dictionaries.” Think Python. Sebastopol, CA: O’Reilly, 2012. Print. Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard50
  • 51. Object-oriented programming Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard51
  • 52. 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 Humanities Antonio Jiménez Mavillard52
  • 53. General ideas * Debugging · Logic errors + accessing to a non-existing element - attribute not found - attribute not initialized - method not found Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard53
  • 54. 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 Humanities Antonio Jiménez Mavillard54
  • 55. 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 Humanities Antonio Jiménez Mavillard55
  • 56. 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 Humanities Antonio Jiménez Mavillard56
  • 57. Attributes and methods * Attributes · Data · Syntax: object.attribute · Take specific values for each object · Examples: base, height (Rectangle) Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard57
  • 58. 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 Humanities Antonio Jiménez Mavillard58
  • 59. 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 Humanities Antonio Jiménez Mavillard59
  • 60. Modelling domains * Exercise 5 · Write the method birthday for the class Author that increases by one the author's age Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard60
  • 61. 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 Humanities Antonio Jiménez Mavillard61
  • 62. 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 Humanities Antonio Jiménez Mavillard62
  • 63. Modelling domains * Exercise 5 (solution) Knowledge Representation in Digital Humanities Antonio 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
  • 64. Modelling domains * Exercise 5 (solution) Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard64     def read(self, title):         book = self.bibliography[title]         print book.text          def __repr__(self):         return self.name
  • 65. Modelling domains * Exercise 5 (solution) Knowledge Representation in Digital Humanities Antonio 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
  • 66. 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 Humanities Antonio Jiménez Mavillard66
  • 67. 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 Humanities Antonio Jiménez Mavillard67
  • 68. Assignment * Assignment 6: The library · Project + Read the description of the library in the attached file Knowledge Representation in Digital Humanities Antonio Jiménez Mavillard68
  • 69. 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 Humanities Antonio Jiménez Mavillard69
  • 70. 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 Humanities Antonio Jiménez Mavillard70
  • 71. 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 Humanities Antonio Jiménez Mavillard71