NLP Lab Manual
NLP Lab Manual
● Script Validation
● Stop Word Removal
● Stemming
import re
import nltk
from [Link] import stopwords
from [Link] import PorterStemmer
def preprocess_text(text):
# Step 1: Tokenization
tokens = nltk.word_tokenize(text)
print("Tokens:", tokens)
# Step 2: Filtration
filtered_tokens = [word for word in tokens if [Link](r'^[a-zA-Z]+$', word)]
print("Filtered Tokens:", filtered_tokens)
# Step 5: Stemming
stemmer = PorterStemmer()
stemmed_tokens = [[Link](word) for word in cleaned_tokens]
print("Stemmed Tokens:", stemmed_tokens)
return stemmed_tokens
# Example text
text = "The quick brown fox jumps over the lazy dog! 123"
OUTPUT:
[nltk_data] AIML\AppData\Roaming\nltk_data...
[nltk_data] AIML\AppData\Roaming\nltk_data...
Tokens: ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog', '!', '123']
Filtered Tokens: ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
English Tokens: ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
Tokens after Stop Word Removal: ['quick', 'brown', 'fox', 'jumps', 'lazy',
2. Demonstrate the N-gram modeling to analyze and establish the probability distribution
across sentences and explore the utilization of unigrams, bigrams, and trigrams in diverse
English sentences to illustrate the impact of varying n-gram orders on the calculated
probabilities.
# Sample sentences
sentences = [
"I love ice cream",
"I love chocolate",
"Ice cream is delicious"
]
# Print results
print("Unigram Probabilities:")
for word, prob in unigram_probabilities.items():
print(f"P({word}) = {prob:.4f}")
print("\nBigram Probabilities:")
for (word1, word2), prob in bigram_probabilities.items():
print(f"P({word2} | {word1}) = {prob:.4f}")
print("\nTrigram Probabilities:")
for (word1, word2, word3), prob in trigram_probabilities.items():
print(f"P({word3} | {word1}, {word2}) = {prob:.4f}")
OUTPUT:
Unigram Probabilities:
P(i) = 0.1818
P(love) = 0.1818
P(ice) = 0.1818
P(cream) = 0.1818
P(chocolate) = 0.0909
P(is) = 0.0909
P(delicious) = 0.0909
Bigram Probabilities:
P(love | i) = 1.0000
Trigram Probabilities:
1.0000
3. Investigate the Minimum Edit Distance (MED) algorithm and its application in string
comparison and the goal is to understand how the algorithm efficiently computes the minimum
number of edit operations required to transform one string into another. ● Test the algorithm on
strings with different type of variations (e.g., typos, substitutions, insertions, deletions) ●
Evaluate its adaptability to different types of input variations
return D[m][n]
# Test cases
print(min_edit_distance("kitten", "sitten")) # Output: 1 (substitution)
print(min_edit_distance("kitten", "kittens")) # Output: 1 (insertion)
print(min_edit_distance("kitten", "kiten")) # Output: 1 (deletion)
print(min_edit_distance("sitting", "kitten")) # Output: 3 (substitution + substitution + deletion)
print(min_edit_distance("kitten", "kitten")) # Output: 0 (no changes)
print(min_edit_distance("abc", "xyz")) # Output: 3 (substitution x 3)
OUTPUT:
4. Write a program to implement top-down and bottom-up parser using appropriate context free
grammar.
class TopDownParser:
def init (self, grammar):
[Link] = grammar
self.input_string = ""
[Link] = 0
def E(self):
result = self.T()
if result is None:
return None
while [Link] < len(self.input_string) and self.input_string[[Link]] == '+':
[Link] += 1 # consume '+'
result2 = self.T()
if result2 is None:
return None
return result
def T(self):
result = self.F()
if result is None:
return None
while [Link] < len(self.input_string) and self.input_string[[Link]] == '*':
[Link] += 1 # consume '*'
result2 = self.F()
if result2 is None:
return None
return result
def F(self):
if [Link] < len(self.input_string) and self.input_string[[Link]] == '(':
[Link] += 1 # consume '('
result = self.E()
if result is None or [Link] >= len(self.input_string) or self.input_string[[Link]] != ')':
return None
[Link] += 1 # consume ')'
return result
elif [Link] < len(self.input_string) and self.input_string[[Link]].isalpha():
[Link] += 1 # consume id
return True
return None
# Example usage:
topdown_parser = TopDownParser(grammar=None)
input_expr = "id+id*id"
print("Top-Down Parser Result:", topdown_parser.parse(input_expr))
OUTPUT:
class BottomUpParser:
def init (self, grammar):
[Link] = grammar
[Link] = []
self.input_string = ""
[Link] = 0
def shift(self):
if [Link] < len(self.input_string):
[Link](self.input_string[[Link]])
[Link] += 1
def reduce(self):
# Look for possible reductions based on the grammar rules
if len([Link]) >= 3 and [Link][-3:] == ['id', '+', 'id']:
[Link] = [Link][:-3] # reduce to E -> E + T
[Link]('E')
return True
elif len([Link]) >= 3 and [Link][-3:] == ['id', '*', 'id']:
[Link] = [Link][:-3] # reduce to T -> T * F
[Link]('T')
return True
elif len([Link]) >= 1 and [Link][-1] == 'id':
[Link] = [Link][:-1] # reduce to F -> id
[Link]('F')
return True
return False
# Example usage:
bottomup_parser = BottomUpParser(grammar=None)
input_expr = "id+id*id"
print("Bottom-Up Parser Result:", bottomup_parser.parse(input_expr))
OUTPUT:
5. Given the following short movie reviews, each labeled with a genre, either comedy or action:
● fun, couple, love, love comedy ● fast, furious, shoot action ● couple, fly, fast, fun, fun comedy
● furious, shoot, shoot, fun action ● fly, fast, shoot, love action and A new document D: fast,
couple, shoot, fly Compute the most likely class for D. Assume a Naive Bayes classifier and
use add-1 smoothing for the likelihoods.
# Step 1: Count the word frequencies for each class (comedy and action)
class_word_counts = {"comedy": Counter(), "action": Counter()}
class_doc_counts = {"comedy": 0, "action": 0}
vocab_size = len(all_words)
log_prob_comedy += prior_comedy
log_prob_action += prior_action
OUTPUT:
6. Demonstrate the following using appropriate programming tool which illustrates the
use of information retrieval in NLP:
● Study the various Corpus – Brown, Inaugural, Reuters, udhr with various methods like
filelds, raw, words, sents, categories 3
● Create and use your own corpora (plaintext, categorical)
● Study Conditional frequency distributions
● Study of tagged corpora with methods like tagged_sents, tagged_words
● Write a program to find the most frequent noun tags
● Map Words to Properties Using Python Dictionaries
● Study Rule based tagger, Unigram Tagger
Find different words from a given plain text without any space by comparing this text with a
given corpus of words. Also find the score of words.
import nltk
from [Link] import brown, inaugural, reuters, udhr
from [Link] import ConditionalFreqDist
from [Link] import pos_tag
#[Link]('all') # Ensure all required corpora are downloaded
OUTPUT:
Brown Corpus File IDs: ['ca01', 'ca02', 'ca03', 'ca04', 'ca05', 'ca06', 'ca07', 'ca08', 'ca09', 'ca10',
'ca11', 'ca12', 'ca13', 'ca14', 'ca15', 'ca16', 'ca17', 'ca18', 'ca19', 'ca20', 'ca21', 'ca22', 'ca23', 'ca24',
'ca25', 'ca26', 'ca27', 'ca28', 'ca29', 'ca30', 'ca31', 'ca32', 'ca33', 'ca34', 'ca35', 'ca36', 'ca37', 'ca38',
'ca39', 'ca40', 'ca41', 'cg20', 'cg21', 'cg22', 'cg23', 'cg24', 'cg25', 'cg26', 'cg27', 'cg28', 'cg29', 'cg30',
'cg31', 'cg32', 'cg33', 'cg34', 'cg35', 'cg36', 'cg37', 'cg38', 'cg39', 'cg40', 'cg41', 'cg42', 'cg43',
'cg44', 'cg45', 'cg46', 'cg47', 'cg48', 'cg49', 'cp22', 'cp23', 'cp24', 'cp25', 'cp26', 'cp27', 'cp28',
'cp29', 'cr01', 'cr02', 'cr03', 'cr04', 'cr05', 'cr06', 'cr07', 'cr08', 'cr09']
Reuters Categories: ['acq', 'alum', 'barley', 'bop', 'carcass', 'castor-oil', 'cocoa', 'coconut',
'coconut-oil', 'coffee', 'copper', 'copra-cake', 'corn', 'cotton', 'cotton-oil', 'cpi', 'cpu', 'crude', 'dfl',
'dlr', 'dmk', 'earn', 'fuel', 'gas', 'gnp', 'gold', 'grain', 'groundnut', 'groundnut-oil', 'heat', 'hog',
'housing', 'income', 'instal-debt', 'interest', 'ipi', 'iron-steel', 'jet', 'jobs', 'l-cattle', 'lead', 'lei', 'lin-oil',
'livestock', 'lumber', 'meal-feed', 'money-fx', 'money-supply', 'naphtha', 'nat-gas', 'nickel', 'nkr',
'nzdlr', 'oat', 'oilseed', 'orange', 'palladium', 'palm-oil', 'palmkernel', 'pet-chem', 'platinum', 'potato',
'propane', 'rand', 'rape-oil', 'rapeseed', 'reserves', 'retail', 'rice', 'rubber', 'rye', 'ship', 'silver',
'sorghum', 'soy-meal', 'soy-oil', 'soybean', 'strategic-metal', 'sugar', 'sun-meal', 'sun-oil', 'sunseed',
'tea', 'tin', 'trade', 'veg-oil', 'wheat', 'wpi', 'yen', 'zinc']
Preamble
First 20 words of Brown Corpus: ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday',
'an', 'investigation', 'of', "Atlanta's", 'recent', 'primary', 'election', 'produced', '``', 'no', 'evidence',
"''", 'that']
First 5 sentences of Inaugural Corpus: [['Fellow', '-', 'Citizens', 'of', 'the', 'Senate', 'and', 'of',
'the', 'House', 'of', 'Representatives', ':'], ['Among', 'the', 'vicissitudes', 'incident', 'to', 'life', 'no',
'event', 'could', 'have', 'filled', 'me', 'with', 'greater', 'anxieties', 'than', 'that', 'of', 'which', 'the',
'notification', 'was', 'transmitted', 'by', 'your', 'order', ',', 'and', 'received', 'on', 'the', '14th', 'day', 'of',
'the', 'present', 'month', '.'], ['On', 'the', 'one', 'hand', ',', 'I', 'was', 'summoned', 'by', 'my', 'Country',
',', 'whose', 'voice', 'I', 'can', 'never', 'hear', 'but', 'with', 'veneration', 'and', 'love', ',', 'from', 'a',
'retreat', 'which', 'I', 'had', 'chosen', 'with', 'the', 'fondest', 'predilection', ',', 'and', ',', 'in', 'my',
'flattering', 'hopes', ',', 'with', 'an', 'immutable', 'decision', ',', 'as', 'the', 'asylum', 'of', 'my',
'declining', 'years', '--', 'a', 'retreat', 'which', 'was', 'rendered', 'every', 'day', 'more', 'necessary', 'as',
'well', 'as', 'more', 'dear', 'to', 'me', 'by', 'the', 'addition', 'of', 'habit', 'to', 'inclination', ',', 'and', 'of',
'frequent', 'interruptions', 'in', 'my', 'health', 'to', 'the', 'gradual', 'waste', 'committed', 'on', 'it', 'by',
'time', '.'], ['On', 'the', 'other', 'hand', ',', 'the', 'magnitude', 'and', 'difficulty', 'of', 'the', 'trust', 'to',
'which', 'the', 'voice', 'of', 'my', 'country', 'called', 'me', ',', 'being', 'sufficient', 'to', 'awaken', 'in',
'the', 'wisest', 'and', 'most', 'experienced', 'of', 'her', 'citizens', 'a', 'distrustful', 'scrutiny', 'into', 'his',
'qualifications', ',', 'could', 'not', 'but', 'overwhelm', 'with', 'despondence', 'one', 'who', '(',
'inheriting', 'inferior', 'endowments', 'from', 'nature', 'and', 'unpracticed', 'in', 'the', 'duties', 'of',
'civil', 'administration', ')', 'ought', 'to', 'be', 'peculiarly', 'conscious', 'of', 'his', 'own', 'deficiencies',
'.'], ['In', 'this', 'conflict', 'of', 'emotions', 'all', 'I', 'dare', 'aver', 'is', 'that', 'it', 'has', 'been', 'my',
'faithful', 'study', 'to', 'collect', 'my', 'duty', 'from', 'a', 'just', 'appreciation', 'of', 'every',
'circumstance', 'by', 'which', 'it', 'might', 'be', 'affected', '.']]
Category: news, Sample Text: Today the stock market soared as investors cheered positive
earnings.
Category: sports, Sample Text: The local team won the championship in a thrilling final game.
Common words in 'news': [('the', 6386), (',', 5188), ('.', 4030), ('of', 2861), ('and', 2186)]
Common words in 'romance': [(',', 3899), ('.', 3736), ('the', 2988), ('and', 1905), ('to', 1517)]
First 5 tagged sentences in 'news': [[('The', 'AT'), ('Fulton', 'NP-TL'), ('County', 'NN-TL'),
('Grand', 'JJ-TL'), ('Jury', 'NN-TL'), ('said', 'VBD'), ('Friday', 'NR'), ('an', 'AT'), ('investigation',
'NN'), ('of', 'IN'), ("Atlanta's", 'NP$'), ('recent', 'JJ'), ('primary', 'NN'), ('election', 'NN'),
('produced', 'VBD'), ('``', '``'), ('no', 'AT'), ('evidence', 'NN'), ("''", "''"), ('that', 'CS'), ('any', 'DTI'),
('irregularities', 'NNS'), ('took', 'VBD'), ('place', 'NN'), ('.', '.')], [('The', 'AT'), ('jury', 'NN'),
('further', 'RBR'), ('said', 'VBD'), ('in', 'IN'), ('term-end', 'NN'), ('presentments', 'NNS'), ('that',
'CS'), ('the', 'AT'), ('City', 'NN-TL'), ('Executive', 'JJ-TL'), ('Committee', 'NN-TL'), (','Common
words in 'news': [('the', 6386), (',', 5188), ('.', 4030), ('of', 2861), ('and', 2186)]
Common words in 'romance': [(',', 3899), ('.', 3736), ('the', 2988), ('and', 1905), ('to', 1517)]
First 5 tagged sentences in 'news': [[('The', 'AT'), ('Fulton', 'NP-TL'), ('County', 'NN-TL'),
('Grand', 'JJ-TL'), ('Jury', 'NN-TL'), ('said', 'VBD'), ('Friday', 'NR'), ('an', 'AT'), ('investigation',
'NN'), ('of', 'IN'), ("Atlanta's", 'NP$'), ('recent', 'JJ'), ('primary', 'NN'), ('election', 'NN'),
('produced', 'VBD'), ('``', '``'), ('no', 'AT'), ('evidenceCommon words in 'news': [('the', 6386), (',',
5188), ('.', 4030), ('of', 2861), ('and', 2186)]
Common words in 'romance': [(',', 3899), ('.', 3736), ('the', 2988), ('and', 1905), ('to', 1517)]
First 5 tagged sentences in 'news': [[('The', 'AT'), ('Fulton', 'NP-TL'), ('County', 'NN-TL'),
('Grand', 'JJ-TL'), ('Jury', 'NN-TL'), ('said', 'VBD'), ('Friday', 'NR'), ('an', 'AT'Common words in
'news': [('the', 6386), (',', 5188), ('.', 4030), ('of', 2861), ('and', 2186)]
Common words in 'romance': [(',', 3899), ('.', 3736), ('the', 2988), ('and', 1905), ('to', 1517)]
First 5 tagged sentences in 'news': [[('The', 'AT'), ('Fulton', 'NP-TL'), ('County', 'NN-TL'),
('Grand', 'JJ-TL'), ('Jury', 'NN-TL'), ('said', 'VBD'), ('Friday', 'NR'), ('an', 'AT'Common words in
'news': [('the', 6386), (',', 5188), ('.', 4030), ('of', 2861), ('and', 2186)]
Common words in 'news': [('the', 6386), (',', 5188), ('.', 4030), ('of', 2861), ('and', 2186)]
Common words in 'romance': [(',', 3899), ('.', 3736), ('the', 2988), ('and', 1905), ('to', 1517)]
First 5 tagged sentences in 'news': [[('The', 'AT'), ('Fulton', 'NP-TL'), ('County', 'NN-TL'),
('Grand', 'JJ-TL'), ('Jury', 'NN-TL'), ('said', 'VBD'), ('Friday', 'NR'), ('an', 'AT'), ('investigation',
'NN'), ('of', 'IN'), ("Atlanta's", 'NP$'), ('recent', 'JJ'), ('primary', 'NN'), ('election', 'NN'),
('produced', 'VBD'), ('``', '``'), ('no', 'AT'), ('evidence', 'NN'), ("''", "''"), ('that', 'CS'), ('any', 'DTI'),
('irregularities', 'NNS'), ('took', 'VBD'), ('place', 'NN'), ('.', '.')], [('The', 'AT'), ('jury', 'NN'),
('further', 'RBR'), ('said', 'VBD'), ('in', 'IN'), ('term-end', 'NN'), ('presentments', 'NNS'), ('that',
'CS'), ('the', 'AT'), ('City', 'NN-TL'), ('Executive', 'JJ-TL'), ('Committee', 'NN-TL'), (','),
('investigation', 'NN'), ('of', 'IN'), ("Atlanta's", 'NP$'), ('recent', 'JJ'), ('primary', 'NN'), ('election',
'NN'), ('produced', 'VBD'), ('``', '``'), ('no', 'AT'), ('evidence', 'NN'), ("''", "''"), ('that', 'CS'), ('any',
'DTI'), ('irregularities', 'NNS'), ('took', 'VBD'), ('place', 'NN'), ('.', '.')], [('The', 'AT'), ('jury', 'NN'),
('further', 'RBR'), ('said', 'VBD'), ('in', 'IN'), ('term-end', 'NN'), ('presentments', 'NNS'), ('that',
'CS'), ('the', 'AT'), ('City', 'NN-TL'), ('Executive', 'JJ-TL'), ('Committee', 'NN-TL'), (','', 'NN'),
("''", "''"), ('that', 'CS'), ('any', 'DTI'), ('irregularities', 'NNS'), ('took', 'VBD'), ('place', 'NN'), ('.',
'.')], [('The', 'AT'), ('jury', 'NN'), ('further', 'RBR'), ('said', 'VBD'), ('in', 'IN'), ('term-end', 'NN'),
('presentments', 'NNS'), ('that', 'CS'), ('the', 'AT'), ('City', 'NN-TL'), ('Executive', 'JJ-TL'),
('Committee', 'NN-TL'), (',', ','), ('which', 'WDT'), ('had', 'HVD'), ('over-all', 'JJ'), ('charge', 'NN'),
('of', 'IN'), ('the', 'AT'), ('election', 'NN'), (',', ','), ('``', '``'), ('deserves', 'VBZ'), ('the', 'AT'), ('praise',
'NN'), ('and', 'CC'), ('thanks', 'NNS'), ('of', 'IN'), ('the', 'AT'), ('City', 'NN-TL'), ('of', 'IN-TL'),
('Atlanta', 'NP-TL'), ("''", "''"), ('for', 'IN'), ('the', 'AT'), ('manner', 'NN'), ('in', 'IN'), ('which',
'WDT'), ('the', 'AT'), ('election', 'NN'), ('was', 'BEDZ'), ('conducted', 'VBN'), ('.', '.')], [('The',
'AT'), ('September-October', 'NP'), ('term', 'NN'), ('jury', 'NN'), ('had', 'HVD'), ('been', 'BEN'),
('charged', 'VBN'), ('by', 'IN'), ('Fulton', 'NP-TL'), ('Superior', 'JJ-TL'), ('Court', 'NN-TL'),
('Judge', 'NN-TL'), ('Durwood', 'NP'), ('Pye', 'NP'), ('to', 'TO'), ('investigate', 'VB'), ('reports',
'NNS'), ('of', 'IN'), ('possible', 'JJ'), ('``', '``'), ('irregularities', 'NNS'), ("''", "''"), ('in', 'IN'), ('the',
'AT'), ('hard-fought', 'JJ'), ('primary', 'NN'), ('which', 'WDT'), ('was', 'BEDZ'), ('won', 'VBN'),
('by', 'IN'), ('Mayor-nominate', 'NN-TL'), ('Ivan', 'NP'), ('Allen', 'NP'), ('Jr.', 'NP'), ('.', '.')], [('``',
'``'), ('Only', 'RB'), ('a', 'AT'), ('relative', 'JJ'), ('handful', 'NN'), ('of', 'IN'), ('such', 'JJ'), (''), ('Ivan',
'NP'), ('Allen', 'NP'), ('Jr.', 'NP'), ('.', '.')], [('``', '``'), ('Only', 'RB'), ('a', 'AT'), ('relative', 'JJ'),
('handful', 'NN'), ('of', 'IN'), ('such', 'JJ'), ('reports', 'NNS'), ('was', 'BEDZ'), ('received', 'VBN'),
("''", "''"), (',', ','), ('the', 'AT'), ('jury', 'NN'), ('said', 'VBD'), (',', ','), ('``', '``'), ('considering', 'IN'),
(reports', 'NNS'), ('was', 'BEDZ'), ('received', 'VBN'), ("''", "''"), (',', ','), ('the', 'AT'), ('jury', 'NN'),
('said', 'VBD'), (',', ','), ('``', '``'), ('considering', 'IN'), ('the', 'AT'), ('widespread', 'JJ'), ('interest',
'NN'), ('in', 'IN'), ('the', 'AT'), ('election', 'NN'), (',', ','), ('the', 'AT'), ('number', 'NN'), ('of', 'IN'),
('voters', 'NNS'), ('and', 'CC'), ('the', 'AT'), ('size', 'NN'), ('of', 'IN'), ('this', 'DT'), ('city', 'NN'), ("''",
"''"), ('.', '.')], [('The', 'AT'), ('jury', 'NN'), ('said', 'VBD'), ('it', 'PPS'), ('did', 'DOD'), ('find', 'VB'),
('that', 'CS'), ('many', 'AP'), ('of', 'IN'), ("Georgia's", 'NP$'), ('registration', 'NN'), ('and', 'CC'),
('election', 'NN'), ('laws', 'NNS'), ('``', '``'), ('are', 'BER'), ('outmoded', 'JJ'), ('or', 'CC'),
('inadequate', 'JJ'), ('and', 'CC'), ('often', 'RB'), ('ambiguous', 'JJ'), ("''", "''"), ('.', '.')]]
First 10 tagged words in 'news': [('The', 'AT'), ('Fulton', 'NP-TL'), ('County', 'NN-TL'),
('Grand', 'JJ-TL'), ('Jury', 'NN-TL'), ('said', 'VBD'), ('Friday', 'NR'), ('an', 'AT'), ('investigation',
'NN'), ('of', 'IN')]
Word Properties: {'this': {'length': 4, 'frequency': 1}, 'is': {'length': 2, 'frequency': 1}, 'a':
{'length': 1, 'frequency': 1}, 'sample': {'length': 6, 'frequency': 3}, 'text': {'length': 4, 'frequency':
1}, 'with': {'length': 4, 'frequency': 1}, 'words': {'length': 5, 'frequency': 1}, 'and': {'length': 3,
'frequency': 1}, 'analysis': {'length': 8, 'frequency': 1}}
7. Write a Python program to find synonyms and antonyms of the word "active" using WordNet.
import nltk
import nltk
#[Link]('wordnet')
#[Link]('omw-1.4')
from [Link] import wordnet
OUTPUT:
import nltk
import tensorflow as tf
import numpy as np
from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import Embedding, LSTM, Dense, Input
from [Link] import Model
def load_parallel_corpus(dataset_name):
# Placeholder: Load real parallel dataset
source_sentences = ["hello world", "how are you", "good morning"]
target_sentences = ["hola mundo", "como estas", "buenos dias"]
return source_sentences, target_sentences
def preprocess_sentences(sentences):
# Tokenization and lowercasing
return [nltk.word_tokenize([Link]()) for sent in sentences]
# Load data
source_sentences, target_sentences = load_parallel_corpus("low_resource_dataset")
preprocessed_source = preprocess_sentences(source_sentences)
preprocessed_target = preprocess_sentences(target_sentences)
# Tokenization
source_tokenizer = Tokenizer()
source_tokenizer.fit_on_texts(augmented_source)
source_vocab_size = len(source_tokenizer.word_index) + 1
target_tokenizer = Tokenizer()
target_tokenizer.fit_on_texts(augmented_target)
target_vocab_size = len(target_tokenizer.word_index) + 1
# Padding
max_length = max(max(len(seq) for seq in source_sequences), max(len(seq) for seq in
target_sequences))
source_sequences = pad_sequences(source_sequences, maxlen=max_length, padding='post')
target_sequences = pad_sequences(target_sequences, maxlen=max_length, padding='post')
# Encoder
encoder_inputs = Input(shape=(max_length,))
enc_embedding = Embedding(source_vocab_size, embedding_dim, mask_zero=True)
(encoder_inputs)
encoder_lstm = LSTM(hidden_size, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(enc_embedding)
encoder_states = [state_h, state_c]
# Decoder
decoder_inputs = Input(shape=(max_length,))
dec_embedding = Embedding(target_vocab_size, embedding_dim,
mask_zero=True)(decoder_inputs)
decoder_lstm = LSTM(hidden_size, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(dec_embedding, initial_state=encoder_states)
decoder_dense = Dense(target_vocab_size, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# Define Model
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
[Link](optimizer='adam', loss='sparse_categorical_crossentropy')
# Train Model
epochs = 10
y_train = np.expand_dims(target_sequences, -1)
[Link]([source_sequences, target_sequences], y_train, epochs=epochs)
# Inference Function
def translate_sentence(sentence):
seq = source_tokenizer.texts_to_sequences([nltk.word_tokenize([Link]())])
seq = pad_sequences(seq, maxlen=max_length, padding='post')
prediction = [Link]([seq, seq])
predicted_ids = [Link](prediction[0], axis=-1)
return ' '.join([target_tokenizer.index_word[idx] for idx in predicted_ids if idx > 0])
# Example Translation
test_sentence = "hello world"
translation = translate_sentence(test_sentence)
print("Source:", test_sentence)
print("Translation:", translation)
OUTPUT:
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX_VNNI
FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Epoch 1/10
Epoch 2/10
Epoch 3/10
Epoch 4/10
Epoch 5/10
Epoch 6/10
Epoch 7/10
Epoch 8/10
Epoch 9/10
Epoch 10/10
mundo