抽象ArrayList和计数元素的问题

时间:2018-09-29 03:37:27

标签: java

我想在做作业方面得到一点帮助。我的方法无法按照我的预期去做。我很确定问题出在nlpClassify()中,但是我不确定。当前,clpClassify()只会为变量输出0。欢迎任何建议。

    public class TweetHandler implements TweetHandlerInterface {


private Scanner inFile = null;
private String tweet = null;
private String[] temp = null;
private int counter = 0;
List<AbstractTweet> tweetList = new ArrayList<AbstractTweet>();
List<AbstractTweet> dataList = new ArrayList<AbstractTweet>();
List<String[]> dataBaseList = new ArrayList<String[]>();

@Override
public List<AbstractTweet> loadTweetsFromText(String filePath) {
    MainApp main = null;

    try {
        inFile = new Scanner ( new FileReader(filePath));
    }
    catch( FileNotFoundException er ){
        System.err.println(er);
        main.printMenu();
    }

    String line = null;
    //String[] data = null;

    //int counter = 0;

    while ( inFile.hasNextLine() ) {

        line = inFile.nextLine();
        parseTweetLine(line);
        counter++;

    }

    System.out.println("Reading tweets from file..."
                       + "\n" + counter + " tweets read.");

    inFile.close();

    return tweetList;
}

@Override
public AbstractTweet parseTweetLine(String tweetLine) {
    temp = tweetLine.split("\",\"");
    tweet = temp[5];

    if( temp.length > 6 ){
        for( int i = 0; i < temp.length; i++ ){
            tweet += " " + temp[i];
        }
    }

    temp[0] = temp[0].replaceAll("\"","");
    tweet = tweet.replaceAll("\\p{Punct}", "");
    temp[5] = tweet;

    int target = Integer.parseInt(temp[0]);
    int id = Integer.parseInt(temp[1]);
    SimpleDateFormat format = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy");
    Date date = new Date();
    try{
        date = format.parse(temp[2]);
    }
    catch( ParseException e ){
        System.err.println("Error with date parsing. " + e);
    }

    String flag = temp[3];
    String user = temp[4];
    String text = temp[5];


    Tweet tweets = new Tweet(target,id,date,flag,user,text);

    return tweets;
}

/**
 * calls classifyTweet
 */
public void nlpClassify(){ //prints out accuracy
    System.out.println("Classifying tweets...");

    counter = 0;
    int correct = 0,
        wrong = 0;
    float accuracy = 0.0f;

    AbstractTweet tweets;

    for( int i = 0; i < tweetList.size(); i++ ){
        tweets = tweetList.get(i);
        tweets.setPredictedPolarity(classifyTweet(tweets));

        int target = tweets.getTarget();
        int polarity = tweets.getPredictedPolarity();

        System.out.println("Target: " + target );
        System.out.println("Prediction: " + polarity );

        for( int j = 0; j < 75; j++ ){
            System.out.print("=");
        }
        System.out.println();

        if( target == polarity ){
            correct++;
        }
        else{
            wrong++;
        }
        counter++;

        accuracy = ( correct / counter ) * 100.0f;   
    }

    System.out.println( "Classified " + counter + " tweets." );
    System.out.println( "Correct tweets: " + correct );
    System.out.println( "Wrong tweets: " + wrong );
    System.out.println( "Accuracy: " + accuracy );

}

@Override
public int classifyTweet(AbstractTweet tweet) {
    int calcPolarity = SentimentAnalyzer.getParagraphSentiment( tweet.getText() );
    tweet.setPredictedPolarity(calcPolarity);

    return calcPolarity;
}

@Override
public void addTweetsToDB(List<AbstractTweet> tweets) {

    System.out.println("Adding files to database... ");
    int i = 0;

    while( i < tweets.size()){
        dataList.add(tweets.get(i));
        i++;
    }

}

@Override
public void deleteTweet(int id) {
    int i = 0,
        temp = 0;
    Tweet obj = null;
    while( i < dataList.size() ){
        temp = obj.getId();
        if( id == temp ){
            dataList.remove(i);
            --counter;
        }
        ++i;
    }


}

@Override
public void saveSerialDB() {

    try{
        FileOutputStream fileOut = new FileOutputStream("DB.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);

        out.writeObject(dataList);
        out.close();
        fileOut.close();
    }
    catch( IOException e ){
        e.printStackTrace();
    }
}

@Override
public void loadSerialDB() {
    try{
        FileInputStream fileIn = new FileInputStream("DB.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);

        dataList = (List<AbstractTweet>)in.readObject();
        in.close();
        fileIn.close();
    }
    catch( Exception e ){
        e.printStackTrace();
    }
}

@Override
public List<AbstractTweet> searchByUser(String user) {
    int i = 0;
    String temp = null;
    Tweet obj = null;

    while( i < dataList.size() ){
        temp = obj.getUser();
        if( user.equals(temp) )
            dataList.add(obj);
     ++i;       
    }

    return dataList;
}

忽略searchByUser(String user),我知道这是错误的,还没有解决它。如果您有建议,那也很酷。 这是界面。

public interface TweetHandlerInterface {

/**
 * Loads tweets from a CSV text file.
 * @param filePath The path to the CSV file.
 * @return A list of tweets as objects.
 */
List<AbstractTweet> loadTweetsFromText(String filePath);

/**
 * Parses a single line from the CSV file and returns a tweet as an object. 
 * @param tweetLine A string containing the contents of a single line in the CSV file.
 * @return A tweet as an object.
 */
AbstractTweet parseTweetLine(String tweetLine);

/**
 * Classifies a tweet as negative, neutral, or positive by using the text of the tweet.
 * @param tweet A tweet object.
 * @return 0 = negative, 2 = neutral, 4 = positive.
 */
int classifyTweet(AbstractTweet tweet);

/**
 * Adds a list of new tweets to the existing database.
 * @param tweets A list of tweet objects.
 */
void addTweetsToDB(List<AbstractTweet> tweets);

/**
 * It deletes ad tweet from the database, given its id.
 * @param id The id value of the tweet.
 */
void deleteTweet(int id);

/**
 * Saves the database in the working directory as a serialized object. 
 */
void saveSerialDB();

/**
 * Loads tweet database.
 */
void loadSerialDB();

/**
 * Searches the tweet database by user name. It returns a list of all tweets
 * matching the given user name.
 * @param user The user name to search for.
 * @return A list of tweet objects.
 */
List<AbstractTweet> searchByUser(String user);

/**
 * Searches the tweet database for tweets posted on a given date.
 * @param date The date to search for.
 * @return A list of tweet objects.
 */
List<AbstractTweet> searchByDate(Date date);

/**
 * Searches the tweet database for tweets matching a given flag.
 * @param flag The flag to search for.
 * @return A list of tweet objects.
 */
List<AbstractTweet> searchByFlag(String flag);

/**
 * Searches the tweet database for tweets matching a given substring.
 * @param substring The substring to search for.
 * @return A list of tweet objects.
 */
List<AbstractTweet> searchBySubstring(String substring);
}

这是我的Main()。

public class MainApp {
public static void main (String[] args){

    TweetHandler handler = new TweetHandler();
    MainApp obj = new MainApp();
    Tweet tweetObj = null;

    String choice;

    do{
        obj.printMenu();

        System.out.print("Enter a choice from above: ");
        Scanner inputConsole = new Scanner( System.in );

        choice = inputConsole.next();

        String filePath = null;


        switch( choice ){
            case "0":
                System.exit(0);
                break;
            case "1":
                Scanner file = new Scanner ( System.in );
                System.out.print("Enter the input file path: ");
                filePath = file.nextLine();
                handler.loadTweetsFromText(filePath);
                break;
            case "2":
                handler.classifyTweet(handler.parseTweetLine(filePath));
                handler.nlpClassify();
                break;
            case "3":
                System.out.println("Enter ID of the tweet you wish to change: ");
                break;
            case "4":
                System.out.println("Adding tweets to database...");
                handler.addTweetsToDB(handler.loadTweetsFromText(filePath));
                break;
            case "5":
                Scanner removeChoice = new Scanner( System.in );
                System.out.println("Enter ID of tweet you wish to delete: ");
                int remove = removeChoice.nextInt();
                handler.deleteTweet(remove);
                break;
            case "6":
                Scanner searching = new Scanner( System.in );
                System.out.println("Search by: 1 User, 2 Date, 3 Flag, 4 Substring.");
                int search = searching.nextInt();
                switch(search){
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                }
                break;
        }

    } while ( !"0".equals(choice) );


}

void printMenu(){
    System.out.println("0. Exit program.\n"
               + "1. Load new tweet text file.\n"
               + "2. Classify tweets using NLP library and report accuracy.\n"
               + "3. Manually change tweet class label.\n"
               + "4. Add new tweets to database.\n"
               + "5. Delete tweet from database (given its id).\n"
               + "6. Search tweets by user, date, flag, or a matching substring.\n");
}

}

1 个答案:

答案 0 :(得分:0)

您使用tweetList.size()进行循环。您可以打印出tweetList.size()以查看该值。我猜它是0。在您的代码中没有看到任何tweetList.add(obj)。因此tweetList一直都是空的。