为什么我没有打印时会打印

时间:2019-12-16 06:48:03

标签: java

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;


    }

}

我在运行代码时,它将打印我的positiveAdjectives列表。我什至没有运行该代码,所以对于为什么会这样感到困惑。

任何人都可以帮忙吗:)

我目前正在从事CS项目,该项目最终将对.txt文件进行审查,然后对形容词进行随机选择,以从随机的肯定形容词或随机否定形容词中选择一个。代码中显示了此内容,但该审核的.txt文件不是

1 个答案:

答案 0 :(得分:1)

您的System.out.println(temp);初始化程序块中有一个static语句。

该语句似乎可以打印positiveAdjectives.txt文件的行。

static类初始化时将执行Review块,这是在您的main方法执行之前发生的。