AI
Practical File
sj
INDEX
• Introduction to PROLOG
• Simple Facts in PROLOG
• a. Ram likes mango.
• b. Seema is a girl.
• c. Bill likes Cindy.
• d. Rose is red.
• e. John owns gold.
• Predicates in PROLOG
• a. Convert Centigrade to Fahrenheit
• b. Check if a Temperature is Below Freezing
• Breadth-First Search Traversal Program
• Water Jug Problem Implementation
• Remove Punctuations from a String
• Sort Sentence in Alphabetical Order
• Hangman Game in Python (Single Implementation)
• Tic-Tac-Toe Game Implementation
• Remove Stop Words using NLTK
• Stemming Implementation using NLTK
• POS Tagging using NLTK
• Lemmatization using NLTK
• Text Classification using NLTK
Experiment 1: Introduction to PROLOG
Aim
To understand the basic concepts and syntax of PROLOG programming language.
Theory
PROLOG (Programming in Logic) is a declarative programming language used in artificial
intelligence and computational linguistics. It's based on first-order predicate logic and uses
facts, rules, and queries as its fundamental elements.
Basic PROLOG Concepts
• Facts: Simple statements that express relationships
• Rules: Logical implications that define relationships
• Queries: Questions asked to the PROLOG system
• Variables: Start with uppercase letters or underscore
• Constants: Start with lowercase letters or are numbers
PROLOG (Programming in Logic) is a logic programming language associated with artificial
intelligence and computational linguistics. It is particularly known for its proficiency in
pattern matching, tree-based data structuring, and automatic backtracking, making it excellent
for solving problems in AI and symbolic reasoning.
Developed in the early 1970s, it is especially designed for tasks that involve symbolic
reasoning and pattern matching, which are core components of artificial intelligence and
computational linguistics. One of PROLOG's key features is its declarative nature; instead of
specifying how to compute something, you define relationships and the desired outcomes,
allowing the PROLOG interpreter to determine how to reach those outcomes.
Pattern Matching: PROLOG excels at pattern matching due to its use of unification, a
process that compares and matches data structures. This capability is particularly useful in
natural language processing, where identifying and matching patterns in text is crucial.
Tree-based Data Structuring: PROLOG inherently supports tree-based data structures,
making it highly efficient for parsing and managing hierarchical data. This is beneficial in
fields like computational linguistics, where syntactic structures of languages are often
represented as trees.
Automatic Backtracking: One of the most powerful features of PROLOG is its automatic
backtracking. When searching for solutions to a query, PROLOG explores all possible paths.
If it encounters a dead end, it backtracks and tries alternative paths until it finds a solution or
exhausts all possibilities. This feature is invaluable for solving complex problems that require
exploring many potential solutions, such as puzzles and games.
Inference Engine: At its core, PROLOG uses an inference engine based on the resolution
principle, allowing it to deduce new information from known facts and rules. This deductive
reasoning capability is what makes PROLOG powerful in applications like expert systems,
where it can derive conclusions from a set of given facts and logical rules.
Applications in AI: PROLOG's strengths in logical reasoning and pattern matching make it
suitable for a variety of AI applications, including natural language understanding,
knowledge representation, automated theorem proving, and intelligent systems that mimic
human decision-making processes.
Experiment 2: PROLOG Facts
Aim
To write simple facts in PROLOG for given statements.
Theory
Facts in PROLOG are basic assertions about relationships between objects. They end with a
period and use lowercase letters for constants. In PROLOG, facts represent basic assertions
about the world, expressed in a form of predicates. For example, likes(ram, mango). states
that Ram likes mango, encapsulating this relationship in a structured form that PROLOG can
query and manipulate.
Code
% Fa c t s r e pr e s e nt i ng gi ve n s t a t e me nt s
l i ke s ( r a m, ma ngo) .
i s _gi r l ( s e e ma ) .
l i ke s ( bi l l , c i ndy) .
i s _c ol or ( r os e , r e d) .
owns ( j ohn, gol d) .
% Que r i e s
?- l i ke s ( r a m, ma ngo) .
?- i s _gi r l ( s e e ma ) .
?- l i ke s ( bi l l , c i ndy) .
?- i s _c ol or ( r os e , r e d) .
?- owns ( j ohn, gol d) .
Output
? - l i ke s ( r a m, ma ngo) .
t r ue .
? - i s _gi r l ( s e e ma ) .
t r ue .
Experiment 3: Temperature Conversion in PROLOG
Aim
To implement temperature conversion and freezing point checking in PROLOG.
Theory
Predicates in PROLOG define relationships between objects and are used to query
information. An example is converting temperatures from centigrade to Fahrenheit, which
can be expressed in a predicate that takes centigrade input and provides the Fahrenheit
output based on the mathematical formula.
Code
% Te mpe r a t ur e c onve r s i on pr e di c a t e
c e l s i us _t o_f a hr e nhe i t ( C, F) : -
F i s ( C * 9/ 5) + 32.
% Fr e e z i ng poi nt c he c k pr e di c a t e
i s _f r e e z i ng( C) : -
C =< 0.
% Exa mpl e que r i e s
? - c e l s i us _t o_f a hr e nhe i t ( 100, F) .
? - i s _f r e e z i ng( - 5) .
Output
? - c e l s i us _t o_f a hr e nhe i t ( 100, F) .
F = 212.
? - i s _f r e e z i ng( - 5) .
t r ue .
Experiment 4: Breadth First Search Implementation
Aim
To implement Breadth First Search traversal algorithm in Python.
Theory
BFS is a graph traversal algorithm that explores all vertices at the present depth before
moving to vertices at the next depth level. Breadth-First Search (BFS) is a graph traversal
algorithm that explores vertices level by level, starting from the root node and progressing
outwards, ensuring that all nodes at the present depth are visited before moving on to nodes at
the next depth level. It uses a queue to keep track of the next location to explore.
Code
f r om c ol l e c t i ons i mpor t de f a ul t di c t , de que
c l a s s Gr a ph:
de f __i ni t __( s e l f ) :
s e l f . gr a ph = de f a ul t di c t ( l i s t )
de f a dd_e dge ( s e l f , u, v) :
s e l f . gr a ph[ u] . a ppe nd( v)
de f bf s ( s e l f , s t a r t ) :
vi s i t e d = s e t ( )
que ue = de que ( [ s t a r t ] )
vi s i t e d. a dd( s t a r t )
whi l e que ue :
ve r t e x = que ue . popl e f t ( )
pr i nt ( ve r t e x, e nd=" " )
f or ne i ghbor i n s e l f . gr a ph[ ve r t e x] :
i f ne i ghbor not i n vi s i t e d:
vi s i t e d. a dd( ne i ghbor )
que ue . a ppe nd( ne i ghbor )
# Exa mpl e us a ge
i f __na me __ == " __ma i n__" :
g = Gr a ph( )
g. a dd_e dge ( 0, 1)
g. a dd_e dge ( 0, 2)
g. a dd_e dge ( 1, 2)
g. a dd_e dge ( 2, 0)
g. a dd_e dge ( 2, 3)
g. a dd_e dge ( 3, 3)
pr i nt ( " BFS s t a r t i ng f r om ve r t e x 2: " )
g. bf s ( 2)
Output
BFS s t a r t i ng f r om ve r t e x 2:
2 0 3 1
Experiment 5: Water Jug Problem
Aim
To implement the Water Jug Problem using Python.
Theory
The Water Jug Problem is a classic problem where we need to measure a specific amount of
water using two jugs of different capacities. The Water Jug Problem is a classic example of a
problem that can be solved using state space search techniques. The goal is to measure out a
specific amount of water using jugs with different capacities, applying operations like filling,
emptying, and pouring water between jugs to reach the target measurement.
Code
de f wa t e r _j ug_pr obl e m( j ug1_c a p, j ug2_c a p, t a r ge t ) :
vi s i t e d = s e t ( )
de f s ol ve ( j ug1, j ug2) :
i f ( j ug1, j ug2) i n vi s i t e d:
r e t ur n Fa l s e
vi s i t e d. a dd( ( j ug1, j ug2) )
pr i nt ( f " J ug1: { j ug1} , J ug2: { j ug2} " )
i f j ug1 == t a r ge t or j ug2 == t a r ge t :
r e t ur n Tr ue
# Fi l l j ug1
i f s ol ve ( j ug1_c a p, j ug2) :
r e t ur n Tr ue
# Fi l l j ug2
i f s ol ve ( j ug1, j ug2_c a p) :
r e t ur n Tr ue
# Empt y j ug1
i f s ol ve ( 0, j ug2) :
r e t ur n Tr ue
# Empt y j ug2
i f s ol ve ( j ug1, 0) :
r e t ur n Tr ue
# Pour f r om j ug1 t o j ug2
a mount = mi n( j ug1, j ug2_c a p - j ug2)
i f s ol ve ( j ug1 - a mount , j ug2 + a mount ) :
r e t ur n Tr ue
# Pour f r om j ug2 t o j ug1
a mount = mi n( j ug2, j ug1_c a p - j ug1)
i f s ol ve ( j ug1 + a mount , j ug2 - a mount ) :
r e t ur n Tr ue
r e t ur n Fa l s e
r e t ur n s ol ve ( 0, 0)
# Exa mpl e us a ge
j ug1_c a pa c i t y = 4
j ug2_c a pa c i t y = 3
t a r ge t _a mount = 2
pr i nt ( f " Fi ndi ng s ol ut i on f or t a r ge t a mount : { t a r ge t _a mount } " )
wa t e r _j ug_pr obl e m( j ug1_c a pa c i t y, j ug2_c a pa c i t y, t a r ge t _a mount )
Output
Fi ndi ng s ol ut i on f or t a r ge t a mount : 2
J ug1: 0, J ug2: 0
J ug1: 4, J ug2: 0
J ug1: 1, J ug2: 3
J ug1: 1, J ug2: 0
J ug1: 0, J ug2: 1
J ug1: 4, J ug2: 1
J ug1: 2, J ug2: 3
Sol ut i on f ound!
Experiment 6: String Punctuation Removal
Aim
To implement a program that removes punctuation from a given string.
Theory
Removing punctuation from a string is a common text processing task in natural language
processing (NLP). It involves stripping out any punctuation marks from the text to make it
easier to analyze and process, often performed using regular expressions to identify and
remove these characters.
Code
i mpor t s t r i ng
de f r e move _punc t ua t i on( t e xt ) :
# Cr e a t e t r a ns l a t i on t a bl e
t r a ns l a t or = s t r . ma ke t r a ns ( " " , " " , s t r i ng. punc t ua t i on)
# Re move punc t ua t i on us i ng t r a ns l a t i on t a bl e
r e t ur n t e xt . t r a ns l a t e ( t r a ns l a t or )
# Exa mpl e us a ge
t e xt = " He l l o, Wor l d! How a r e you? Thi s i s a t e s t . . . s t r i ng. "
c l e a ne d_t e xt = r e move _punc t ua t i on( t e xt )
pr i nt ( f " Or i gi na l t e xt : { t e xt } " )
pr i nt ( f " Cl e a ne d t e xt : { c l e a ne d_t e xt } " )
Output
Or i gi na l t e xt : He l l o, Wor l d! How a r e you? Thi s i s a t e s t . . . s t r i ng.
Cl e a ne d t e xt : He l l o Wor l d How a r e you Thi s i s a t e s t s t r i ng
Experiment 7: Sentence Sorting
Aim
To implement a program that sorts words in a sentence in alphabetical order.
Theory
Sorting a sentence in alphabetical order involves breaking the sentence into individual words,
sorting these words lexicographically, and then reconstructing the sentence from the sorted
list. This process helps in organizing text data for various applications such as indexing and
search.
Code
de f s or t _s e nt e nc e ( s e nt e nc e ) :
# Spl i t s e nt e nc e i nt o wor ds a nd s or t
wor ds = s e nt e nc e . l owe r ( ) . s pl i t ( )
s or t e d_wor ds = s or t e d( wor ds )
r e t ur n " " . j oi n( s or t e d_wor ds )
# Exa mpl e us a ge
s e nt e nc e = " Pyt hon i s a gr e a t pr ogr a mmi ng l a ngua ge "
s or t e d_s e nt e nc e = s or t _s e nt e nc e ( s e nt e nc e )
pr i nt ( f " Or i gi na l s e nt e nc e : { s e nt e nc e } " )
pr i nt ( f " Sor t e d s e nt e nc e : { s or t e d_s e nt e nc e } " )
Output
Or i gi na l s e nt e nc e : Pyt hon i s a gr e a t pr ogr a mmi ng l a ngua ge
Sor t e d s e nt e nc e : a gr e a t i s l a ngua ge pr ogr a mmi ng pyt hon
Experiment 8: Hangman Game
Aim
To implement the Hangman word guessing game in Python.
Theory
The Hangman game is a simple word-guessing game where the player tries to guess a hidden
word by suggesting letters within a certain number of attempts. Python can be used to
implement this game by using loops and conditionals to check the guessed letters and track
the player's progress.
Code
i mpor t r a ndom
de f ha ngma n( ) :
wor ds = [ ' pyt hon' , ' pr ogr a mmi ng' , ' c omput e r ' , ' a l gor i t hm' , ' da t a ba s e ' ]
wor d = r a ndom. c hoi c e ( wor ds )
gue s s e d_l e t t e r s = s e t ( )
t r i es = 6
whi l e t r i e s > 0:
di s pl a y = ' ' . j oi n( l e t t e r i f l e t t e r i n gue s s e d_l e t t e r s e l s e ' _' f or
l e t t e r i n wor d)
pr i nt ( f " \ nWor d: { di s pl a y} " )
pr i nt ( f " Tr i e s l e f t : { t r i e s } " )
i f ' _' not i n di s pl a y:
pr i nt ( " \ nCongr a t ul a t i ons ! You won! " )
r e t ur n
gue s s = i nput ( " Gue s s a l e t t e r : " ) . l owe r ( )
i f gue s s i n gue s s e d_l e t t e r s :
pr i nt ( " You a l r e a dy gue s s e d t ha t l e t t e r ! " )
c ont i nue
gue s s e d_l e t t e r s . a dd( gue s s )
i f gue s s not i n wor d:
t r i es - = 1
pr i nt ( " Wr ong gue s s ! " )
pr i nt ( f " \ nGa me Ove r ! The wor d wa s : { wor d} " )
# St a r t ga me
ha ngma n( )
Output
Wor d: ______
Tr i e s l ef t : 6
Gue s s a l et t er : p
Wor d: p_____
Tr i e s l ef t : 6
Gue s s a l et t er : y
Wor d: py____
Tr i e s l ef t : 6
...
Experiment 9: Stop Words Removal using NLTK
Aim
To implement stop words removal from a given text using NLTK.
Theory
Stop words are common words that are filtered out before processing natural language data.
NLTK provides a set of stop words in multiple languages.
Code
i mpor t nl t k
f r om nl t k. c or pus i mpor t s t opwor ds
f r om nl t k. t oke ni z e i mpor t wor d_t oke ni z e
de f r e move _s t opwor ds ( t e xt ) :
# Downl oa d r e qui r e d NLTK da t a
nl t k. downl oa d( ' s t opwor ds ' )
nl t k. downl oa d( ' punkt ' )
# Toke ni z e t e xt
t oke ns = wor d_t oke ni z e ( t e xt )
# Ge t Engl i s h s t op wor ds
s t op_wor ds = s e t ( s t opwor ds . wor ds ( ' e ngl i s h' ) )
# Re move s t op wor ds
f i l t e r e d_t e xt = [ wor d f or wor d i n t oke ns i f wor d. l owe r ( ) not i n
s t op_wor ds ]
r e t ur n ' ' . j oi n( f i l t e r e d_t e xt )
# Exa mpl e us a ge
t e xt = " Thi s i s a s a mpl e s e nt e nc e s howi ng s t op wor ds r e mova l us i ng NLTK"
f i l t er ed = r e move _s t opwor ds ( t e xt )
pr i nt ( f " Or i gi na l t e xt : { t e xt } " )
pr i nt ( f " Fi l t e r e d t e xt : { f i l t e r e d} " )
Output
Or i gi na l t e xt : Thi s i s a s a mpl e s e nt e nc e s howi ng s t op wor ds r e mova l us i ng
NLTK
Fi l t e r e d t e xt : s a mpl e s e nt e nc e s howi ng s t op wor ds r e mova l us i ng NLTK
Experiment 10: Tic-Tac-Toe Game Implementation
Aim
To implement a two-player Tic-Tac-Toe game in Python with a simple command-line
interface.
Theory
Tic-Tac-Toe is a game where two players take turns marking spaces on a 3×3 grid. The
player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row
wins the game. Tic-Tac-Toe is a classic two-player game where the players alternately mark
spaces in a 3x3 grid with Xs and Os. The objective is to place three of their marks in a
horizontal, vertical, or diagonal row. Implementing this game in Python involves creating a
grid, managing player turns, and checking for win conditions.
Key concepts:
• 2D array representation of the game board
• Turn-based gameplay
• Win condition checking
• Input validation
• Game state management
Code
Cl a s s Ti c Ta c Toe
de f __i ni t __( s e l f ) :
# I ni t i a l i z e e mpt y boa r d
s e l f . boa r d = [ ' ' f or _ i n r a nge ( 9) ]
s e l f . c ur r e nt _pl a ye r = ' X'
de f ma ke _move ( s e l f , pos i t i on) :
i f s e l f . i s _va l i d_move ( pos i t i on) :
s e l f . boa r d[ pos i t i on] = s e l f . c ur r e nt _pl a ye r
r e t ur n Tr ue
r e t ur n Fa l s e
de f i s _va l i d_move ( s e l f , pos i t i on) :
r e t ur n 0 <= pos i t i on <= 8 a nd s e l f . boa r d[ pos i t i on] == ' '
de f c he c k_wi nne r ( s e l f ) :
# Che c k r ows
f or i i n r a nge ( 0, 9, 3) :
i f s e l f . boa r d[ i ] == s e l f . boa r d[ i +1] == s e l f . boa r d[ i +2] ! = ' ' :
r e t ur n s e l f . boa r d[ i ]
# Che c k c ol umns
f or i i n r a nge ( 3) :
i f s e l f . boa r d[ i ] == s e l f . boa r d[ i +3] == s e l f . boa r d[ i +6] ! = ' ' :
r e t ur n s e l f . boa r d[ i ]
# Che c k di a gona l s
i f s e l f . boa r d[ 0] == s e l f . boa r d[ 4] == s e l f . boa r d[ 8] ! = ' ' :
r e t ur n s e l f . boa r d[ 0]
i f s e l f . boa r d[ 2] == s e l f . boa r d[ 4] == s e l f . boa r d[ 6] ! = ' ' :
r e t ur n s e l f . boa r d[ 2]
r e t ur n None
de f i s _boa r d_f ul l ( s e l f ) :
r e t ur n ' ' not i n s e l f . boa r d
de f s wi t c h_pl a ye r ( s e l f ) :
s e l f . c ur r e nt _pl a ye r = ' O' i f s e l f . c ur r e nt _pl a ye r == ' X' e l s e ' X'
de f di s pl a y_boa r d( s e l f ) :
pr i nt ( " \ nCur r e nt Boa r d: " )
f or i i n r a nge ( 0, 9, 3) :
pr i nt ( f " { s e l f . boa r d[ i ] } | { s e l f . boa r d[ i +1] } |
{ s e l f . boa r d[ i +2] } " )
i f i < 6:
pr i nt ( " - - - - - - - - - - - " )
de f pl a y_ga me ( ) :
ga me = Ti c Ta c Toe ( )
pr i nt ( " We l c ome t o Ti c - Ta c - Toe ! " )
pr i nt ( " Pos i t i ons a r e numbe r e d 0- 8, l e f t t o r i ght , t op t o bot t om" )
whi l e Tr ue :
ga me . di s pl a y_boa r d( )
pr i nt ( f " \ nPl a ye r { ga me . c ur r e nt _pl a ye r } ' s t ur n" )
t r y:
pos i t i on = i nt ( i nput ( " Ent e r pos i t i on ( 0- 8) : " ) )
i f not ga me . ma ke _move ( pos i t i on) :
pr i nt ( " I nva l i d move ! Tr y a ga i n. " )
c ont i nue
e xc e pt Va l ue Er r or :
pr i nt ( " Pl e a s e e nt e r a va l i d numbe r ! " )
c ont i nue
wi nne r = ga me . c he c k_wi nne r ( )
i f wi nne r :
ga me . di s pl a y_boa r d( )
pr i nt ( f " \ nPl a ye r { wi nne r } wi ns ! " )
br e a k
i f ga me . i s _boa r d_f ul l ( ) :
ga me . di s pl a y_boa r d( )
pr i nt ( " \ nI t ' s a t i e ! " )
br e a k
ga me . s wi t c h_pl a ye r ( )
i f __na me __ == " __ma i n__" :
pl a y_ga me ( )
Output
We l c ome t o Ti c - Ta c - Toe !
Pos i t i ons a r e numbe r e d 0- 8, l e f t t o r i ght , t op t o bot t om
Cur r e nt Boa r d:
| |
-----------
| |
-----------
| |
Pl a ye r X' s t ur n
Ent e r pos i t i on ( 0- 8) : 4
Cur r e nt Boa r d:
| |
-----------
| X |
-----------
| |
Pl a ye r O' s t ur n
Ent e r pos i t i on ( 0- 8) : 0
Cur r e nt Boa r d:
O | |
-----------
| X |
-----------
| |
Pl a ye r X' s t ur n
Ent e r pos i t i on ( 0- 8) : 8
Cur r e nt Boa r d:
O | |
-----------
| X |
-----------
| | X
Experiment 11: Stemming using NLTK
Aim
To implement stemming for given words using NLTK.
Theory
Stemming is the process of reducing words to their word stem or root form. Stemming is the
process of reducing words to their base or root form. NLTK provides stemming algorithms
that remove morphological affixes from words, converting them to their root forms. This is
useful in NLP tasks to treat different forms of a word as the same term, enhancing text
normalization
Code
f r om nl t k. s t e m i mpor t Por t e r St e mme r
f r om nl t k. t oke ni z e i mpor t wor d_t oke ni z e
de f pe r f or m_s t e mmi ng( t e xt ) :
# I ni t i a l i z e s t e mme r
ps = Por t e r St e mme r ( )
# Toke ni z e t e xt
wor ds = wor d_t oke ni z e ( t e xt )
# Pe r f or m s t e mmi ng
s t e mme d_wor ds = [ ps . s t e m( wor d) f or wor d i n wor ds ]
r e t ur n ' ' . j oi n( s t e mme d_wor ds )
# Exa mpl e us a ge
t e xt = " r unni ng r unne r r uns r a n"
s t e mme d_t e xt = pe r f or m_s t e mmi ng( t e xt )
pr i nt ( f " Or i gi na l t e xt : { t e xt } " )
pr i nt ( f " St e mme d t e xt : { s t e mme d_t e xt } " )
Output
Or i gi na l t e xt : r unni ng r unne r r uns r a n
St e mme d t e xt : r un r unne r r un r a n
Experiment 12: POS Tagging using NLTK
Aim
To implement Part of Speech (POS) tagging for given sentences using NLTK.
TheoryPOS tagging is the process of marking up words in a text with their corresponding
part of speech. Part-of-speech (POS) tagging is the process of labeling each word in a text
with its appropriate part of speech, such as noun, verb, adjective, etc. NLTK offers tools to
perform POS tagging, helping in the syntactic analysis of text which is critical for
understanding language structure and meaning.
Code
i mpor t nl t k
f r om nl t k i mpor t pos _t a g
f r om nl t k. t oke ni z e i mpor t wor d_t oke ni z e
de f pos _t a ggi ng( t e xt ) :
# Downl oa d r e qui r e d da t a
nl t k. downl oa d( ' a ve r a ge d_pe r c e pt r on_t a gge r ' )
nl t k. downl oa d( ' punkt ' )
# Toke ni z e a nd t a g t e xt
t oke ns = wor d_t oke ni z e ( t e xt )
t a gge d = pos _t a g( t oke ns )
r e t ur n t a gge d
# Exa mpl e us a ge
t e xt = " The qui c k br own f ox j umps ove r t he l a z y dog"
t a gge d_t e xt = pos _t a ggi ng( t e xt )
pr i nt ( f " Or i gi na l t e xt : { t e xt } " )
pr i nt ( " Ta gge d t e xt : " )
f or wor d, t a g i n t a gge d_t e xt :
pr i nt ( f " { wor d} : { t a g} " )
Output
Or i gi na l t e xt : The qui c k br own f ox j umps ove r t he l a z y dog
Ta gge d t e xt :
The : DT
qui c k: J J
br own: J J
f ox: NN
j umps : VBZ
ove r : I N
t he : DT
l a z y: J J
dog: NN
Experiment 13: Lemmatization using NLTK
Aim
To implement lemmatization for given words using NLTK. Lemmatization is the process of
reducing words to their base or dictionary form, called lemmas. Unlike stemming,
lemmatization considers the context and converts words to their meaningful base form.
NLTK provides tools for lemmatization, aiding in accurate text normalization and
understanding.
Theory
Lemmatization is the process of grouping together different inflected forms of a word so they
can be analyzed as a single item.
Code
i mpor t nl t k
f r om nl t k. s t e m i mpor t Wor dNe t Le mma t i z e r
f r om nl t k. t oke ni z e i mpor t wor d_t oke ni z e
de f pe r f or m_l e mma t i z a t i on( t e xt ) :
# Downl oa d r e qui r e d da t a
nl t k. downl oa d( ' wor dne t ' )
nl t k. downl oa d( ' punkt ' )
# I ni t i a l i z e l e mma t i z e r
l e mma t i z e r = Wor dNe t Le mma t i z e r ( )
# Toke ni z e t e xt
wor ds = wor d_t oke ni z e ( t e xt )
# Pe r f or m l e mma t i z a t i on
l e mma t i z e d_wor ds = [ l e mma t i z e r . l e mma t i z e ( wor d) f or wor d i n wor ds ]
r e t ur n ' ' . j oi n( l e mma t i z e d_wor ds )
# Exa mpl e us a ge
t e xt = " The c hi l dr e n a r e pl a yi ng wi t h ba bi e s "
l e mma t i z e d_t e xt = pe r f or m_l e mma t i z a t i on( t e xt )
pr i nt ( f " Or i gi na l t e xt : { t e xt } " )
pr i nt ( f " Le mma t i z e d t e xt : { l e mma t i z e d_t e xt } " )
Output
Or i gi na l t e xt : The c hi l dr e n a r e pl a yi ng wi t h ba bi e s
Le mma t i z e d t e xt : The c hi l d a r e pl a yi ng wi t h ba by
Experiment 14- Simple Text Classification using NLTK
Aim
To implement a basic text classification system using NLTK for sentiment analysis.
Theory
Text classification involves categorizing text documents into predefined categories Text
classification is the process of assigning predefined categories to text data. NLTK facilitates
this by providing tools to preprocess text, extract features, and train machine learning models
to classify text into different categories, enhancing the automation of text analysis tasks.
. The basic process includes:
1. Text preprocessing (cleaning and tokenizing)
2. Feature extraction
3. Training a classifier
4. Predicting categories for new text
Code
i mpor t nl t k
f r om nl t k. t oke ni z e i mpor t wor d_t oke ni z e
f r om nl t k. c or pus i mpor t s t opwor ds
f r om nl t k. c l a s s i f y i mpor t Na i ve Ba ye s Cl a s s i f i e r
de f pr e pr oc e s s ( t e xt ) :
# Toke ni z e a nd c onve r t t o l owe r c a s e
t oke ns = wor d_t oke ni z e ( t e xt . l owe r ( ) )
# Re move s t op wor ds a nd non- a l pha be t i c t oke ns
s t op_wor ds = s e t ( s t opwor ds . wor ds ( ' e ngl i s h' ) )
t oke ns = [ t oke n f or t oke n i n t oke ns i f t oke n. i s a l pha ( ) a nd t oke n not i n
s t op_wor ds ]
r e t ur n t oke ns
de f c r e a t e _f e a t ur e s ( t oke ns ) :
r e t ur n { wor d: Tr ue f or wor d i n t oke ns }
de f t r a i n_c l a s s i f i e r ( ) :
# Tr a i ni ng da t a
pos i t i ve _t e xt s = [
" gr e a t pr oduc t a ma z i ng qua l i t y" ,
" e xc e l l e nt s e r vi c e hi ghl y r e c omme nde d" ,
" l ove t hi s pr oduc t pe r f e c t " ,
" be s t pur c ha s e wor t h e ve r y pe nny"
]
ne ga t i ve _t e xt s = [
" t e r r i bl e pr oduc t di s a ppoi nt e d" ,
" poor qua l i t y wa s t e mone y" ,
" woul d not r e c omme nd hor r i bl e " ,
" br oke qui c kl y ve r y unha ppy"
]
# Cr e a t e t r a i ni ng f e a t ur e s
t r a i ni ng_da t a = ( [ ( t e xt , ' pos i t i ve ' ) f or t e xt i n pos i t i ve _t e xt s ] +
[ ( t e xt , ' ne ga t i ve ' ) f or t e xt i n ne ga t i ve _t e xt s ] )
# Pr oc e s s a nd c r e a t e f e a t ur e s e t s
t r a i ni ng_f e a t ur e s = [ ( c r e a t e _f e a t ur e s ( pr e pr oc e s s ( t e xt ) ) , l a be l )
f or t e xt , l a be l i n t r a i ni ng_da t a ]
# Tr a i n c l a s s i f i e r
c l a s s i f i e r = Na i ve Ba ye s Cl a s s i f i e r . t r a i n( t r a i ni ng_f e a t ur e s )
r e t ur n c l a s s i f i e r
de f c l a s s i f y_t e xt ( c l a s s i f i e r , t e xt ) :
# Pr e pr oc e s s a nd c l a s s i f y ne w t e xt
t oke ns = pr e pr oc e s s ( t e xt )
f e a t ur e s = c r e a t e _f e a t ur e s ( t oke ns )
r e t ur n c l a s s i f i e r . c l a s s i f y( f e a t ur e s )
# Exa mpl e us a ge
i f __na me __ == " __ma i n__" :
# Downl oa d r e qui r e d NLTK da t a
nl t k. downl oa d( ' punkt ' )
nl t k. downl oa d( ' s t opwor ds ' )
# Tr a i n t he c l a s s i f i e r
c l a s s i f i e r = t r a i n_c l a s s i f i e r ( )
# Te s t t e xt s
t e s t _t e xt s = [
" t hi s pr oduc t i s f a nt a s t i c " ,
" hor r i bl e e xpe r i e nc e ne ve r a ga i n" ,
" r e a l l y ha ppy wi t h my pur c ha s e " ,
" c ompl e t e wa s t e of mone y"
]
# Cl a s s i f y t e s t t e xt s
pr i nt ( " Cl a s s i f i c a t i on Re s ul t s : " )
pr i nt ( " - " * 30)
f or t e xt i n t e s t _t e xt s :
s e nt i me nt = c l a s s i f y_t e xt ( c l a s s i f i e r , t e xt )
pr i nt ( f " Te xt : ' { t e xt } ' " )
pr i nt ( f " Se nt i me nt : { s e nt i me nt } \ n" )
Output
Cl a s s i f i c a t i on Re s ul t s :
------------------------------
Te xt : ' t hi s pr oduc t i s f a nt a s t i c '
Se nt i me nt : pos i t i ve
Te xt : ' hor r i bl e e xpe r i e nc e ne ve r a ga i n'
Se nt i me nt : ne ga t i ve
Te xt : ' r e a l l y ha ppy wi t h my pur c ha s e '
Se nt i me nt : pos i t i ve
Te xt : ' c ompl e t e wa s t e of mone y'
Se nt i me nt : ne ga t i ve