1
import java.util.Scanner;
import java.io.File;
import java.util.HashMap;
import java.util.ArrayList;


/**
 * Class that contains helper methods for the Review Lab
 **/
public class Review {

    public static void main(String[] args) {
        //System.out.print(sentimentVal("ok"));
        //System.out.println(totalSentiment("simpleReview.txt"));
        System.out.println(starRating("simpleReview.txt"));
        //System.out.println(fakeReview("simpleReview.txt"));
    }





/*  public static double totalSentiment(String fileName) {
        String review = textToString("simpleReview.txt");   

        int s =  review.indexOf(" ");
        double sum = 0;

        //take first word from whole string
        //review.substring(0 --> " ")

        String firstWord = removePunctuation(review.substring(0, review.indexOf(" ")));
        sum += sentimentVal(firstWord);

        while(s != -1) {
            int nextSpace = review.indexOf(" ", s + 1);
            String word;

            if (nextSpace != -1){
                word = removePunctuation(review.substring(s + 1, nextSpace));
                sum += sentimentVal(word);


            } else {
                word = removePunctuation(review.substring(s + 1));
                sum += sentimentVal(word);

            }

            //System.out.println(sentimentVal(word));



            s = nextSpace; 
        }

        return sum;



    } */




  private static HashMap<String, Double> sentiment = new HashMap<String, Double>();
  private static ArrayList<String> posAdjectives = new ArrayList<String>();
  private static ArrayList<String> negAdjectives = new ArrayList<String>();


  private static final String SPACE = " ";

  static{
    try {
      Scanner input = new Scanner(new File("cleanSentiment.csv"));
      while(input.hasNextLine()){
        String[] temp = input.nextLine().split(",");
        sentiment.put(temp[0],Double.parseDouble(temp[1]));
        //System.out.println("added "+ temp[0]+", "+temp[1]);
      }
      input.close();
    }
    catch(Exception e){
      System.out.println("Error reading or parsing cleanSentiment.csv");
    }


  //read in the positive adjectives in postiveAdjectives.txt
     try {
      Scanner input = new Scanner(new File("positiveAdjectives.txt"));
      while(input.hasNextLine()){
        String temp = input.nextLine().trim();
        System.out.println(temp);
        posAdjectives.add(temp);
      }
      input.close();
    }
    catch(Exception e){
      System.out.println("Error reading or parsing postitiveAdjectives.txt\n" + e);
    }   

  //read in the negative adjectives in negativeAdjectives.txt
     try {
      Scanner input = new Scanner(new File("negativeAdjectives.txt"));
      while(input.hasNextLine()){
        negAdjectives.add(input.nextLine().trim());
      }
      input.close();
    }
    catch(Exception e){
      System.out.println("Error reading or parsing negativeAdjectives.txt");
    }   
  }

  /** 
   * returns a string containing all of the text in fileName (including punctuation), 
   * with words separated by a single space 
   */
  public static String textToString( String fileName )
  {  
    String temp = "";
    try {
      Scanner input = new Scanner(new File(fileName));

      //add 'words' in the file to the string, separated by a single space
      while(input.hasNext()){
        temp = temp + input.next() + " ";
      }
      input.close();

    }
    catch(Exception e){
      System.out.println("Unable to locate " + fileName);
    }
    //make sure to remove any additional space that may have been added at the end of the 
 string.
    return temp.trim();
  }

  /**
   * @returns the sentiment value of word as a number between -1 (very negative) to 1 (very 
 positive sentiment) 
   */
  public static double sentimentVal( String word )
  {
    try
    {
      return sentiment.get(word.toLowerCase());
    }
    catch(Exception e)
    {
      return 0;
    }
  }

  /**
   * Returns the ending punctuation of a string, or the empty string if there is none 
   */
  public static String getPunctuation( String word )
  { 
    String punc = "";
    for(int i=word.length()-1; i >= 0; i--){
      if(!Character.isLetterOrDigit(word.charAt(i))){
        punc = punc + word.charAt(i);
      } else {
        return punc;
      }
    }
    return punc;
  }

    /**
   * Returns the word after removing any beginning or ending punctuation
   */
  public static String removePunctuation( String word )
  {
    while(word.length() > 0 && !Character.isAlphabetic(word.charAt(0)))
    {
      word = word.substring(1);
    }
    while(word.length() > 0 && !Character.isAlphabetic(word.charAt(word.length()-1)))
    {
      word = word.substring(0, word.length()-1);
    }

    return word;
  }

  /** 
   * Randomly picks a positive adjective from the positiveAdjectives.txt file and returns it.
   */
  public static String randomPositiveAdj()
  {
    int index = (int)(Math.random() * posAdjectives.size());
    return posAdjectives.get(index);
  }

  /** 
   * Randomly picks a negative adjective from the negativeAdjectives.txt file and returns it.
   */
  public static String randomNegativeAdj()
  {
    int index = (int)(Math.random() * negAdjectives.size());
    return negAdjectives.get(index);

  }

  /** 
   * Randomly picks a positive or negative adjective and returns it.
   */
  public static String randomAdjective()
  {
    boolean positive = Math.random() < .5;
    if(positive){
      return randomPositiveAdj();
    } else {
      return randomNegativeAdj();
    }
  }




  /** Activity 2 starRating method
  Write the starRating method here which returns the number of stars for the review based on 
enter code here its totalSentiment.
 * @param fileName 
*/



public static int starRating(String filename){

    // determine number of stars between 0 and 4 based on totalSentiment value 

     double totalSentiment = totalSentiment("simpleReview.txt");

     // write if statements here
     if (totalSentiment < 15 && totalSentiment >= 10) {
         return 4;
     } else if(totalSentiment < 10 && totalSentiment >= 5) {
         return 3;
     } else if(totalSentiment < 5 && totalSentiment >= 0) {
         return 2;
     } else if(totalSentiment < 0) {
         return 1;
     } else {
         return 0;
     }


}

    public static double totalSentiment(String simpleReview) {
        String review = textToString("simpleReview.txt");   
        int s =  review.indexOf(" ");
        double sum = 0;

        //take first word from whole string
        //review.substring(0 --> " ")

        String firstWord = removePunctuation(review.substring(0, review.indexOf(" ")));
        sum += sentimentVal(firstWord);

        while(s != -1) {
            int nextSpace = review.indexOf(" ", s + 1);
            String word;

            if (nextSpace != -1){
                word = removePunctuation(review.substring(s + 1, nextSpace));
                sum += sentimentVal(word);


            } else {
                word = removePunctuation(review.substring(s + 1));
                sum += sentimentVal(word);

            }

            s = nextSpace; 
        }

        return sum;


    }

}

While I am running my code, it will print a list of my positiveAdjectives. I am not even running that code, so I am confused as to why this is happening.

can anyone help please :)

I am currently working on a CS project which is ultimately taking a review on a .txt file then the adjectives to random one selected from a random positive or random negative adjective. This is shown in the code, but what is not is the .txt file for the review

1 Answers1

2

You have a System.out.println(temp); statement in your static initializer block.

That statement seems to print the lines of your positiveAdjectives.txt file.

That static block is executed when your Review class is initialized, which happens before your main method executes.

Eran
  • 387,369
  • 54
  • 702
  • 768