Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python program with NLTK Objective: Use n-gram models for text analysis. Turn in: your Python programs, zipped (just your 2 programs) - There is a

Python program with NLTK
Objective: Use n-gram models for text analysis.
Turn in: your Python programs, zipped (just your 2 programs)
- There is a hm_files, a ZIP file with files that includes files .DS_Store, LangId.sol, LangId.test, LangId.train.English, LangId.train.French, LangId.train.Italian
Since there is no text file in this zip file, what file am I reading for the part 1 of the assignment. Please use screenshots so I can understand better. Thanks in advance!
In this homework you will create bigram and unigram dictionaries for English, French, and Italian using the provided training data where the key is the unigram or bigram text and the value is the count of that unigram or bigram in the data. Then for the test data, calculate probabilities for each language and compare against the true labels.
Instructions:
1. Program 1: Build language models for 3 languages as follows.
a. create a function with a filename as argument
b. read in the text and remove newlines
c. tokenize the text
d. use nltk to create a bigrams list
e. use nltk to create a unigrams list
f. use the bigram list to create a bigram dictionary of bigrams and counts, [token1 token2] -> count
g. use the unigram list to create a unigram dictionary of unigrams and counts, [token] -> count
h. return the unigram dictionary and bigram dictionary from the function
i. in the main body of code, call the function 3 times for each training file, pickle the 6 dictionaries and save to files with appropriate names. The reason we are pickling them in one program and unpickling them in another is that NLTK ngrams is slow and if you put this all in one program you will waste a lot of time waiting for ngrams() to finish.
2. Program 2.
a. Read in your pickled dictionaries.
b. For each test file, calculate a probability for each language (see note below) and write the language with the highest probability to a file.
c. Compute and output your accuracy as the percentage of correctly classified instances in the test set. The file LangId.sol holds the correct classifications.
d. output your accuracy, as well as the line numbers of the incorrectly classified items
HINT:
Creating the dictionaries in Program 1:
You can use the NLTK ngrams() function to create a bigrams and a unigrams generator object. Then you can iterate over each to create the dictionary using Pythons .count() string method to extract counts from the text you read in.
Calculating probabilities in Program 2:
The probabilities will be large enough so that you dont need to use logs, we will simply multiply the probabilities together. Each bigrams probability with Laplace smoothing is: (b + 1) / (u + v) where b is the bigram count, u is the unigram count of the first word in the bigram, and v is the total vocabulary size (add the lengths of the 3 unigram dictionaries).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions

Question

13-4 What are alternative methods for building information systems?

Answered: 1 week ago