Package seekfeel.datareaders

Source Code of seekfeel.datareaders.Reviews_Reader

package seekfeel.datareaders;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


//package reviews_parser;

/**
*
* @author Ashraf
* This is the class that reads the Reviews from the text file.
*/

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import  java.util.ArrayList;

import seekfeel.dataholders.DataUnit;
import seekfeel.dataholders.Product_Feature;
import seekfeel.dataholders.Review;
import seekfeel.fileparsers.Reviews_Parser;

public class Reviews_Reader implements Reader{

    File file = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    BufferedReader dis = null;
    InputStreamReader isr = null;
    Reviews_Parser RP = new Reviews_Parser();
    public ArrayList Get_Reviews_From_File( String Review_File_Name )
    {

      ArrayList Found_Reviews = new ArrayList();
      String Review_File_Full_Name = Review_File_Name ;
      System.out.println(Review_File_Full_Name);
      file = new File(Review_File_Full_Name);
      try {
                  fis = new FileInputStream(file);
                  // Here BufferedInputStream is added for fast reading.
                  bis = new BufferedInputStream(fis);
                  isr = new InputStreamReader(bis);
                  dis = new BufferedReader(isr);
                  int NumberOfReviews =0;
                  int c = 0;
                    while (dis.ready()!= false)
                    {

                        String Sentence = dis.readLine();
                        if(Sentence.contains("[t]") )
                        {
                           RP.Parse_Review_Title(Sentence);
                           c++;
                        }
                        if(Sentence.contains("##") )
                        {
                            NumberOfReviews++;
                            Review  R = RP.Parse_Review_Data(Sentence);
                            if( R.getDataBody() != null)
                            {
                              Found_Reviews.add(R);
                            }
                        }
                    }

                  fis.close();
                  bis.close();
                  dis.close();
           }


        catch (FileNotFoundException e)
        {
          e.printStackTrace();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }

        return Combine_Reviews_By_Title(Found_Reviews);
    }

    ArrayList<Review> Combine_Reviews_By_Title(ArrayList<Review> Reviews)
    {

        String title = Reviews.get(0).Review_Title;
        ArrayList<Review> ReturenedReviews = new ArrayList<Review>();
        Review r = new Review();
        int size=  Reviews.size();
        ArrayList<Product_Feature> Features = new ArrayList<Product_Feature>();

        for (int i = 0; i < size; i++) {
            if(Reviews.get(i).Review_Title.equals(title))
            {
                r.Review_Sentences.add(Reviews.get(i).getDataBody());
                Features = new ArrayList<Product_Feature>();
                for(Product_Feature f : ((ArrayList<Product_Feature>)Reviews.get(i).Features) )
                {
                    Features.add(f);
                }
                r.Sentences_Features.add(Features);
            }
            else
            {
                r.Review_Title = title;
                if(!r.Review_Sentences.isEmpty() )
                ReturenedReviews.add(r);
                 r = new Review();
                 title = Reviews.get(i).Review_Title;
                 r.Review_Sentences.add(Reviews.get(i).getDataBody());
                  Features = new ArrayList<Product_Feature>();
                for(Product_Feature f : ((ArrayList<Product_Feature>)Reviews.get(i).Features) )
                {
                    Features.add(f);
                }
                r.Sentences_Features.add(Features);
            }


        }
        r.Review_Title = title;
        ReturenedReviews.add(r);
        return ReturenedReviews;
    }

  @Override
  public ArrayList<DataUnit> getData(String dataParam) {
    // TODO Auto-generated method stub
    return null;
  }

}
TOP

Related Classes of seekfeel.datareaders.Reviews_Reader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.