Goal: The goal of this assignment is to help students familiarize themselves wit
ID: 3748798 • Letter: G
Question
Goal: The goal of this assignment is to help students familiarize themselves with the following Java programming concepts:
1. Input/Output to and from the terminal.
2. Storing data in a file and reading data from a file.
3. Creating object-oriented classes and methods to handle data.
4. Using data structures to store data in main memory (e.g. HashSet).
5. Working with character strings.
6. Using Javadoc comments and generating and html documentation of the program.
Description: For this assignment you will create a program to classify a set of Tweets as positive, negative, or neutral based on their sentiment. This process is known as Sentiment Analysis. More information about sentiment analysis can be found on Wikipedia and other sources. Although complex algorithms have been developed for sentiment analysis, in this assignment we will classify a tweet as positive, negative, or neutral, by just counting the number of positive and negative words that appear in that tweet. These positive and negative words will be given to the program as input in two separate files, namely positive-words.txt and negative-words.txt. The set of tweets to be classified, will be also given as an input to the program in a CSV (comma separated values) file: testdata.manual.2009.06.14.csv The file contains 498 tweets extracted using the twitter API. The tweets have been annotated (0 = negative, 2 = neutral, 4 = positive) and they can be used to detect sentiment. It contains the following 6 fields:
1. target: the polarity of the tweet (0 = negative, 2 = neutral, 4 = positive)
2. ids: The id of the tweet ( 2087)
3. date: the date of the tweet (Sat May 16 23:58:44 UTC 2009)
4. flag: The query (lyx). If there is no query, then this value is NO_QUERY.
5. user: the user that tweeted (robotickilldozr)
6. text: the text of the tweet We only care about fields 1 and 6 in this file.
Your program should operate in the following manner:
1. When the program starts, it asks the user to provide the file paths of the positive words, negative words, and twitter data file.
2. The program loads the positive words and negative words and stores them in two separate lookup tables. The HashSet data structure can be used as a lookup table in Java as it provides a fast way to look if a word exists in it or not.
3. The program iterates over the tweets in the twitter data file and it counts the number of positive and negative words that the tweet contains. If the tweet contains more positive than negative words it is classified as positive, and vice versa. If no positive or negative words were found on the tweet, it is classified as neutral. It the same number of positive and negative words were found on the tweet, it counts as negative.
4. After each tweet has been classified, the program prints out in the command line the tweet itself, its real label and its predicted label.
5. At the end the program should also print how many tweets were correctly classified and how many were misclassified.
Explanation / Answer
public class Package {
String trackingNumber;
String type;
String specification;
String mailingClass;
float weight;
int volume;
public Package(){
}
public Package(String trackingNumber, String type, String specification, String mailingClass, float weight,
int volume) {
super();
this.trackingNumber = trackingNumber;
this.type = type;
this.specification = specification;
this.mailingClass = mailingClass;
this.weight = weight;
this.volume = volume;
}
public String getTrackingNumber() {
return trackingNumber;
}
public void setTrackingNumber(String trackingNumber) {
this.trackingNumber = trackingNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSpecification() {
return specification;
}
public void setSpecification(String specification) {
this.specification = specification;
}
public String getMailingClass() {
return mailingClass;
}
public void setMailingClass(String mailingClass) {
this.mailingClass = mailingClass;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
}
Shipping Store class-
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class ShippingStore {
private static ArrayList<Package> shippingData=new ArrayList<>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
try{
BufferedReader bufferedReader=new BufferedReader(new FileReader("packages.txt"));
String line="";
int index=0;
while((line=bufferedReader.readLine())!=null){
//Split line on the basis of white space to get values
String[] tokens=line.split(" ");
String trackingNumber=tokens[0];
String type=tokens[1];
String specification=tokens[2];
String mailingClass=tokens[3];
float weight=Float.parseFloat(tokens[4]);
int volume=Integer.parseInt(tokens[5]);
//Add package to arrayList
shippingData.add(new Package(trackingNumber,type,specification,mailingClass,weight,volume));
index++;
}
bufferedReader.close();
int choice=0;
do{
System.out.println(" Menu : 1.Show all the existing package records in the database. 2.Add new pacakge record to database. 3.Delete package record from database. 4.Search for a package. 5.Show a list of pacakges within a given range. 6.Exit");
System.out.println("Enter choice : ");
choice=scanner.nextInt();
switch(choice){
case 1:
showPackages();
break;
case 2:
System.out.print("Enter Tracking number : ");
String trackingNumber=scanner.next();
System.out.println("Enter type : ");
String type=scanner.next();
System.out.println("Enter specification : ");
String specification=scanner.next();
System.out.println("Enter mailing class : ");
String mailingClass=scanner.next();
System.out.println("Enter weight : ");
float weight=Float.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.