0% found this document useful (0 votes)
10 views

Tutorial 8

This document provides an overview of topics covered in a tutorial, including feedback on an exercise, a review of list comprehension, functions, scope, and the NLTK, SpaCy, and JSON libraries. It also includes a quiz.

Uploaded by

Richard Salnikov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Tutorial 8

This document provides an overview of topics covered in a tutorial, including feedback on an exercise, a review of list comprehension, functions, scope, and the NLTK, SpaCy, and JSON libraries. It also includes a quiz.

Uploaded by

Richard Salnikov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PCL 1 - Tutorial 8

Luc, Isabelle, Lucas, Maritina


WELCOME ΤΙ
ΕΝ
Ν
ΤΡ ΟΕΙ
ΩΕ Σ Ο
ΙΚ
ΡΕ ΤΙ ΔΕ
ΑΣ Ν
?
Overview

1. Exercise 3 Feedback

2. Lecture Material Review:


- List Comprehension
- Functions
- Scope
- NLTK/Spacy/Json

1. Quizizz!
List Comprehension
Purpose: Simplifies code for creating new lists by applying an expression to each element in an iterable.

Basic Syntax: Conditional Logic:


• [expression for item in iterable] • Syntax with Condition:
• Example: [x*2 for x in range(5)] [expression for item in iterable if condition]

What does this do? • Example: [x for x in range(10) if x % 2 == 0]

& this?

Advantages: Common Use Cases:


• Conciseness: Reduces the number of lines of code. • Creating lists from other lists or iterables.
• Readability: Easier to understand and write. • Applying a function to each element in an iterable.
• Performance: Often faster than traditional loops. • Filtering elements based on a condition.
Functions
What are Functions? Lines of Code in Script vs Functions
• Functions are reusable blocks of code that • Script Code: Written at the top level of a file,
perform a specific task. executed in sequence, useful for linear
execution and script flow.
• They help in breaking down complex
problems into simpler, manageable parts. • Functions: Encapsulated code blocks, can
be called multiple times from anywhere in the
• Functions can take inputs (arguments), script, ideal for repetitive tasks and modular
process them, and return outputs. coding.

Best Practices
• Functions should do one thing and do it well.
• Use descriptive names for functions and parameters.
• Keep functions short and readable.
Functions Cheat Sheet Creating Functions
• Syntax: Use the def keyword, followed by the function name and
Basic Function Structure parentheses ().
• Parameters: Optional variables passed inside the parentheses.
def function_name(parameters): They receive the values (arguments) passed into the function.
# Function body
return result • Body: The code block within the function that performs the task.
• Return Statement: Optionally returns a value to the caller.

Types of Functions
• Regular Functions: Defined using def. Calling Functions
• Anonymous Functions (Lambdas): Short, one-line • Call a function by its name followed by parentheses. Include
functions defined with the lambda keyword. arguments if the function requires them.

Scope
• Local Scope: Variables created inside a function. function_name(arguments)
• Global Scope: Variables defined outside any
function.

Return Value
Built-in Functions
• Common examples: print(), len(), range(), type(), • Use return to send a value back from the function. If return is not
input() used, the function returns None.
Functions: POLYMORPHISM
Polymorphism in programming refers to the ability of a function to process objects differently based on their
class or data type. The term "polymorphism" comes from Greek, meaning "many forms." In simple terms, it
allows functions to use entities of different types at different times.

def greet(input):
if isinstance(input, str):
# If the input is a string, greet by name
return f"Hello, {input}!"
elif isinstance(input, int) and input > 0: This function handles
# If the input is an integer, repeat the greeting cases where the input
return "Hello! " * input is a string or a
else: positive integer!
return "Invalid input!" That's polymorphism.
See! IT'S GREEK.

# Example
print(greet("Alice")) # Output: Hello, Alice!
print(greet(3)) # Output: Hello! Hello! Hello!
print(greet(-1)) # Output: Invalid input!
Build_A_Function
Objective: Fill in the missing parts of the Python function below to make it
work correctly.

def ___(text, times): Options:


___ = "" • range
for i in ___(times): • +=
result ___ text • concatenate strings
return ___ • result
Build_A_Function SOLUTION
Objective: Fill in the missing parts of the Python function below to make it work correctly.

Options:
def concatenate_strings(text, times):
• range
result = ""
for i in range(times): • +=
result += text • concatenate_strings
return result • result
https://realpython.com/lessons/navigate-namespaces-oveview/
NAMESPACES: Variable Scope
• Local Scope: if you use a variable (like x)
inside a function, Python first checks if it's
defined within that function.
• Global Scope: if the variable isn't in the
function, Python then looks for it outside the
function, in the overall file or program.

• Enclosing Scope: if the variable isn't in the


local or global scope, but the function is inside
another function, Python checks in the outer
function.

• Built-in Scope: if Python can't find the variable


in any of these places, it checks if it's a built-in
feature of Python itself.

USE PYTHON TUTOR TO


VISUALIZE SCOPE BEHAVIOUR!
NLTK (Natural Language Toolkit) See Jupyter Notebook on Olat! nltk_json_tutorial_notebook.ipynb

What is NLTK?
• A leading platform for building Python programs to work with human
language data.
• Comprehensive library with easy-to-use interfaces.

Key Features
• Text Processing Libraries
• Extensive collection of text processing libraries for
classification, tokenization, stemming, tagging, parsing, and
semantic reasoning.
• Corpora and Resources
• Includes a rich set of diverse text corpora and lexical
resources.

https://www.codecademy.com/learn/dscp-natural-language-processing/modules/dscp-text-preprocessing/cheatsheet
SpaCy
Key Features:
• Efficient Text Processing: Fast and streamlined
What is SpaCy?
handling of large volumes of text.
• Open-Source NLP Library: Advanced and efficient
for processing text in Python. • Pre-Trained Statistical Models: Supports multiple
languages for various NLP tasks.
• Real-World Use: Ideal for information extraction and • Named Entity Recognition: Powerful tool for identifying
natural language understanding tasks entities like names and places.
• Part-of-Speech Tagging: Accurate grammatical tagging
of words in a sentence.
• Dependency Parsing: Building dependency trees to
understand sentence structure.
• Extensibility and Customization: Easy to customize and
extend for specific needs.
• Integration with Python Ecosystem: Seamlessly works
with other Python libraries.

https://spacy.io/usage/spacy-101
JSON
JSON (JavaScript Object Notation) is a lightweight,
plain text format used for data interchange
between computers, and is language-independent.

Usage
• Text-based: JSON's text format is ideal for annotating and
storing linguistic data, including NER and sentiment analysis
results. {
• Lightweight: Its minimal data size makes JSON practical for "tokenized_text": ["I", "am", "learning", "NLP"],
"parsed_text": [
processing and sharing large NLP datasets.
{"token": "I", "lemma": "I", "pos": "PRON"},
{"token": "am", "lemma": "be", "pos": "AUX"},
{"token": "learning", "lemma": "learn", "pos": "VERB"},
JSON's structure is composed of two universally understood {"token": "NLP", "lemma": "NLP", "pos": "PROPN"}
data structures: ]
}
• Objects: JSON uses key-value pairs (like dictionaries!) for
easy readability and alignment with common coding patterns.
• Arrays: JSON arrays (like lists) allow for natural indexing and
iteration, simplifying data manipulation.
How to create a dictionary and convert it to JSON

import json The indent parameter in JSON formatting


controls the indentation level for better
readability. Experiment with different values to
# Step 1: Create a Python dictionary see its impact on the JSON string's
my_dict = { appearance. Remember to print the output to
"name": "Alice", observe the changes.
"age": 25,
"is_student": True,
"courses": ["Biology", "Chemistry", "Physics"],
"profile": {
"university": "University of Wonderland", It's as easy as it gets!
All you need to do is create a
"year": 3 dictionary, the json library does
} the rest!
}

# Step 2: Convert the dictionary to a JSON string


json_string = json.dumps(my_dict, indent=4)
IT'S QUIZZIZZ TIME!
(No) Questions?

Or just --> click here


https://docs.google.com/forms/d/e/1FAIpQLScv5cl2glS3nk-
Please fill out this form too --> uowjJqkGgC-1lxcCZCXzYc46lnp5oMt6Qjw/viewform

You might also like