Package Filter

Source Code of Filter.Controller

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

import java.io.IOException;
import java.io.Reader;

import DataAcquisition.*;
import preprocessing.*;

import java.util.*;

import dataSources.DSourcesLogic;

import seekfeel.miners.Sentiment;
import seekfeel.miners.sentiwordnet.SentiPanel;
import seekfeel.ui.*;
import seekfeel.utilities.PropertiesGetter;
import seekfeel.dataholders.DataUnit;
import seekfeel.dataholders.Product_Feature;
import seekfeel.dataholders.Review;

/**
* @author acer
*/
public final class Controller {

    Reader source = null;
    ArrayList<DataUnit> Revs = null;
    TextCleaner tc = null;
    SentenceSplitter sp = null;
    Reviews_preprocessor rp = null;
    MiraAspectExtractor miraAspExt = null;
    DPAspectExtractor dpAspExt = null;
    ArrayList<ArrayList<String>> allExtractedFeatures = null;
    // ///////////////////////////// To be removed
    public ArrayList<Integer> featursIndex = new ArrayList<Integer>();
    // /////////////////////////////
    ArrayList<AspectExtractor> allSubscribedExtractors = null;
    RelevanceCalculator rc = null;
    SentiPanel sentimizer;
    DataSource dataSource;

    public Controller() {
        sentimizer = new SentiPanel();
        tc = new TextCleaner();
        sp = new SentenceSplitter();
        rp = new Reviews_preprocessor();
        allSubscribedExtractors = new ArrayList<AspectExtractor>();
        allExtractedFeatures = new ArrayList<ArrayList<String>>();
    }

    public ArrayList<LinkedHashMap<String, Sentiment>> fillMap() {
        ArrayList<LinkedHashMap<String, Sentiment>> allFeats = new ArrayList<LinkedHashMap<String, Sentiment>>();
        LinkedHashMap<String, Sentiment> sentence = new LinkedHashMap<String, Sentiment>();

        Sentiment s1 = Sentiment.Positive;
        Sentiment s2 = Sentiment.Positive;
        Sentiment s3 = Sentiment.Negative;
        Sentiment s4 = Sentiment.Neutral;

        sentence.put("design", s1);
        sentence.put("focus", s2);
        sentence.put("size", s3);
        sentence.put("clarity", s4);

        allFeats.add(sentence);

        LinkedHashMap<String, Sentiment> sentence1 = new LinkedHashMap<String, Sentiment>();
        Sentiment s11 = Sentiment.Positive;
        Sentiment s12 = Sentiment.Positive;
        Sentiment s13 = Sentiment.Positive;
        Sentiment s14 = Sentiment.Positive;

        sentence1.put("display", s11);
        sentence1.put("sound quality", s12);
        sentence1.put("memory card", s13);
        sentence1.put("elegance", s14);

        allFeats.add(sentence1);

        LinkedHashMap<String, Sentiment> sentence2 = new LinkedHashMap<String, Sentiment>();
        Sentiment s21 = Sentiment.Negative;
        Sentiment s22 = Sentiment.Negative;
        Sentiment s23 = Sentiment.Neutral;


        sentence2.put("graphics card", s21);
        sentence2.put("processor", s22);
        sentence2.put("memory", s23);

        allFeats.add(sentence2);
        return allFeats;
    }

    public ArrayList<featuredSentence> fillSentenceFeats() {
        ArrayList<featuredSentence> sentenceFeats = new ArrayList<featuredSentence>();
        featuredSentence fs1 = new featuredSentence();
        ArrayList<String> feats1 = new ArrayList<String>();
        fs1.setSentence("I like the design and the focus of the camera but I hate its size  and the clarity is normal ");
        feats1.add("design");
        feats1.add("focus");
        feats1.add("size");
        feats1.add("clarity");
        fs1.setFeatures(feats1);
        sentenceFeats.add(fs1);

        featuredSentence fs11 = new featuredSentence();
        ArrayList<String> feats11 = new ArrayList<String>();
        fs11.setSentence("I love the display, sound quality, memory card and elegance of this mobile");
        feats11.add("display");
        feats11.add("sound quality");
        feats11.add("memory card");
        feats11.add("elegance");
        fs11.setFeatures(feats11);
        sentenceFeats.add(fs11);

        featuredSentence fs21 = new featuredSentence();
        ArrayList<String> feats21 = new ArrayList<String>();
        fs21.setSentence("The graphics card is poor and the processor is slow and the memory is normal");
        feats21.add("graphics card");
        feats21.add("processor");
        feats21.add("memory");
        fs21.setFeatures(feats21);
        sentenceFeats.add(fs21);

        return sentenceFeats;
    }

    void setSource(Reader r) {
        source = r;
    }

    void getReviewsFromSource(String Query) {
        Revs = ((seekfeel.datareaders.Reader) source).getData(Query); // get all the reviews of a text] file
        // and put it in an array of Reviews
        rc = new RelevanceCalculator(
                PropertiesGetter.getProperty("Frequencies"), Revs);
    }

    public void addAspectextractor(AspectExtractor ae) {
        allSubscribedExtractors.add(ae);
    }

    public void preprocess() {
        //  rp.AddProcessor(tc); // text cleaner is added to review processor
        //rp.AddProcessor(sp); // sentence splitting is added to review parser
        //rp.processReviwes(Revs);
        Review temp;
        for (DataUnit r : Revs) {
            temp = (Review) r;
            temp.Review_Sentences = new ArrayList<String>(Arrays.asList(((Review) r).Review_Body.split("[.,?!:;]+")));
        }
    }

    public ArrayList<String> formattAndGetReviwes(int index) {
        ((Review) Revs.get(index)).formatt();
        return getFeatures((Review) Revs.get(index));

    }

    public DataUnit getReviewByIndex(int index) {
        return Revs.get(index);
    }

    public ArrayList<String> getOriginalFeatures(int index) {
        Review r = (Review) Revs.get(index);

        ArrayList<String> returnedList = new ArrayList<String>();
        for (ArrayList<Product_Feature> pfs : r.Sentences_Features) {
            for (Product_Feature pf : pfs) {
                returnedList.add(pf.Name);
            }
        }
        return returnedList;
    }

    public void addIndeces(int index, int count) {
        for (int i = 0; i < count; i++) {
            featursIndex.add(index);
        }
    }
    public ArrayList<String> getFeatures(Review rv) {
        ArrayList<String> features = new ArrayList<String>();
        int index = 0;
        for (AspectExtractor AE : allSubscribedExtractors) {
            ArrayList<String> fes = AE.getFeatures(rv);
            for (String f : fes) {
                if (!features.contains(f)) {
                    features.add(f);
                    featursIndex.add(index);
                }
            }
            index++;
        }
        return features;
    }
    public ArrayList<RelevanceData> getRelevances(ArrayList<String> words) {

        ArrayList<RelevanceData> returnedList = new ArrayList<RelevanceData>();

        for (String word : words) {
            returnedList.add(rc.getRelvanceData(word));
        }
        return returnedList;
    }
    public ResultSummary sentimize() {
        //  miraAspExt = new MiraAspectExtractor();
        try {
            long startTime = System.currentTimeMillis();
            long endtime;
            dpAspExt = new DPAspectExtractor();
            addAspectextractor(dpAspExt);
            //  addAspectextractor(miraAspExt);
            int revsSize = Revs.size();
            ArrayList<featuredSentence> fSentences = new ArrayList<featuredSentence>();
            rc = new RelevanceCalculator(
                    PropertiesGetter.getProperty("Frequencies"), Revs);
            Review tempReview = null;
            FeaturesFilter f;
            ArrayList<String> extractedFeatures = null;
            ArrayList<String> filteredFeatures = null;
            for (int index = 0; index < revsSize; index++) {

                f = new FeaturesFilter();
                tempReview = ((Review) Revs.get(index));
                tempReview.formatt();
                extractedFeatures = getFeatures(tempReview);
                filteredFeatures = f.getFileteredFeatures(extractedFeatures,getRelevances(extractedFeatures));
                fSentences.addAll(getFeaturedSentences(tempReview, extractedFeatures));
            }
            endtime = System.currentTimeMillis();
            System.out.println((startTime - endtime));
            // /////////////////////// sentiment analysis goes here ...............
            ArrayList<LinkedHashMap<String, Sentiment>> featsSentiment = new ArrayList<LinkedHashMap<String, Sentiment>>();
            LinkedHashMap<String, FeatureSummary> summary = new LinkedHashMap<String, FeatureSummary>();
            FeatureSummary fs = new FeatureSummary();
            int index = 0;
            for (featuredSentence sentenceWithFeats : fSentences) {
                LinkedHashMap<String, Sentiment> sentenceSentimized = sentimizer.classifyText(sentenceWithFeats.getSentenceTokens(), sentenceWithFeats.getFeatures());
                index++;
                Iterator<java.util.Map.Entry<String, Sentiment>> entriesIterator = sentenceSentimized.entrySet().iterator();
                java.util.Map.Entry<String, Sentiment> currentEntry;

                // looping thru the hash<feat,senti>
                while (entriesIterator.hasNext()) {
                    currentEntry = entriesIterator.next();
                    String keySentence = currentEntry.getKey();
                    Sentiment valueSenti = currentEntry.getValue();

                    if (summary.containsKey(keySentence)) // update
                    {
                        FeatureSummary tempFeatSumm = summary.get(keySentence);
                        if (valueSenti.equals(Sentiment.Positive)) {
                            tempFeatSumm.setNumPositive(tempFeatSumm.getNumPositive() + 1);
                            tempFeatSumm.getPosReviews().add(sentenceWithFeats.getSentence());
                        } else if (valueSenti.equals(Sentiment.Negative)) {
                            tempFeatSumm.setNumNegative(tempFeatSumm.getNumNegative() + 1);
                            tempFeatSumm.getNegReviews().add(sentenceWithFeats.getSentence());
                        } else if (valueSenti.equals(Sentiment.Neutral)) {
                            tempFeatSumm.setNumNeutral(tempFeatSumm.getNumNeutral() + 1);
                            tempFeatSumm.getNeutralReviews().add(sentenceWithFeats.getSentence());
                        }
                    } else // create new
                    {
                        FeatureSummary tempFeatSumm = new FeatureSummary();

                        if (valueSenti.equals(Sentiment.Positive)) {
                            tempFeatSumm.setNumPositive(tempFeatSumm.getNumPositive() + 1);
                            tempFeatSumm.getPosReviews().add(sentenceWithFeats.getSentence());
                        } else if (valueSenti.equals(Sentiment.Negative)) {
                            tempFeatSumm.setNumNegative(tempFeatSumm.getNumNegative() + 1);
                            tempFeatSumm.getNegReviews().add(sentenceWithFeats.getSentence());
                        } else if (valueSenti.equals(Sentiment.Neutral)) {
                            tempFeatSumm.setNumNeutral(tempFeatSumm.getNumNeutral() + 1);
                            tempFeatSumm.getNeutralReviews().add(sentenceWithFeats.getSentence());
                        }

                        summary.put(keySentence, tempFeatSumm);
                    }
                }
            }
            //return featsSentiment;
            ResultSummary res = new ResultSummary();
            res.setFeatsMap(summary);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResultSummary res = new ResultSummary();
        return res;
    }
    public ResultSummary manualSentimize(ArrayList<String> features) {
        //  miraAspExt = new MiraAspectExtractor();
        try {

            //dpAspExt = new DPAspectExtractor();
            //addAspectextractor(dpAspExt);
            //  addAspectextractor(miraAspExt);
            int revsSize = Revs.size();
            ArrayList<featuredSentence> fSentences = new ArrayList<featuredSentence>();
            rc = new RelevanceCalculator(
                    PropertiesGetter.getProperty("Frequencies"), Revs);
            Review tempReview = null;
            for (int index = 0; index < revsSize; index++) {

                tempReview = ((Review) Revs.get(index));
                tempReview.formatt();
                fSentences.addAll(getFeaturedSentences(tempReview, features));
            }
            // /////////////////////// sentiment analysis goes here ...............
            ArrayList<LinkedHashMap<String, Sentiment>> featsSentiment = new ArrayList<LinkedHashMap<String, Sentiment>>();
            LinkedHashMap<String, FeatureSummary> summary = new LinkedHashMap<String, FeatureSummary>();
            FeatureSummary fs = new FeatureSummary();
            int index = 0;
            for (featuredSentence sentenceWithFeats : fSentences) {
                LinkedHashMap<String, Sentiment> sentenceSentimized = sentimizer.classifyText(sentenceWithFeats.getSentenceTokens(), sentenceWithFeats.getFeatures());
                index++;
                Iterator<java.util.Map.Entry<String, Sentiment>> entriesIterator = sentenceSentimized.entrySet().iterator();
                java.util.Map.Entry<String, Sentiment> currentEntry;

                // looping thru the hash<feat,senti>
                while (entriesIterator.hasNext()) {
                    currentEntry = entriesIterator.next();
                    String keySentence = currentEntry.getKey();
                    Sentiment valueSenti = currentEntry.getValue();

                    if (summary.containsKey(keySentence)) // update
                    {
                        FeatureSummary tempFeatSumm = summary.get(keySentence);
                        if (valueSenti.equals(Sentiment.Positive)) {
                            tempFeatSumm.setNumPositive(tempFeatSumm.getNumPositive() + 1);
                            tempFeatSumm.getPosReviews().add(sentenceWithFeats.getSentence());
                        } else if (valueSenti.equals(Sentiment.Negative)) {
                            tempFeatSumm.setNumNegative(tempFeatSumm.getNumNegative() + 1);
                            tempFeatSumm.getNegReviews().add(sentenceWithFeats.getSentence());
                        } else if (valueSenti.equals(Sentiment.Neutral)) {
                            tempFeatSumm.setNumNeutral(tempFeatSumm.getNumNeutral() + 1);
                            tempFeatSumm.getNeutralReviews().add(sentenceWithFeats.getSentence());
                        }
                    } else // create new
                    {
                        FeatureSummary tempFeatSumm = new FeatureSummary();

                        if (valueSenti.equals(Sentiment.Positive)) {
                            tempFeatSumm.setNumPositive(tempFeatSumm.getNumPositive() + 1);
                            tempFeatSumm.getPosReviews().add(sentenceWithFeats.getSentence());
                        } else if (valueSenti.equals(Sentiment.Negative)) {
                            tempFeatSumm.setNumNegative(tempFeatSumm.getNumNegative() + 1);
                            tempFeatSumm.getNegReviews().add(sentenceWithFeats.getSentence());
                        } else if (valueSenti.equals(Sentiment.Neutral)) {
                            tempFeatSumm.setNumNeutral(tempFeatSumm.getNumNeutral() + 1);
                            tempFeatSumm.getNeutralReviews().add(sentenceWithFeats.getSentence());
                        }

                        summary.put(keySentence, tempFeatSumm);
                    }
                }
            }
            //return featsSentiment;
            ResultSummary res = new ResultSummary();
            res.setFeatsMap(summary);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResultSummary res = new ResultSummary();
        return res;
    }
    public ArrayList<LinkedHashMap<String, Sentiment>> sentimizeTrial(
            String trialRev) {
        ConsoleReader cReader = new ConsoleReader();
        Review rev = (Review) cReader.getData(trialRev).get(0);
        dpAspExt = new DPAspectExtractor();
        rev.formatt();
        ArrayList<String> allFeats = dpAspExt.getFeatures(rev);
        ArrayList<featuredSentence> fSentences;
        fSentences = getFeaturedSentences(rev, allFeats);
        ArrayList<LinkedHashMap<String, Sentiment>> featsSentiment = new ArrayList<LinkedHashMap<String, Sentiment>>();
        for (featuredSentence sentenceWithFeats : fSentences) {
            featsSentiment.add(sentimizer.classifyText(
                    sentenceWithFeats.getSentence(),
                    sentenceWithFeats.getFeatures()));
        }
        return featsSentiment;
    }

    public ArrayList<featuredSentence> getFeaturedSentences(Review rev,
                                                            ArrayList<String> features) {
        try {
            ArrayList<featuredSentence> fSentences = new ArrayList<featuredSentence>();
            int sz = rev.Review_Sentences.size();
            String sen;
            featuredSentence fs;
            Formatted_Text formSentece;
            for (int i = 0; i < sz; i++) {
                sen = rev.Review_Sentences.get(i);
                fs = new featuredSentence();
                formSentece = rev.formatedSentences.get(i);
                fs.setSentence(sen);
                fs.setSentenceTokens(formSentece);
                ArrayList<String> fes = new ArrayList<String>();
                for (String fe : features) {
                    if (sen.contains(fe)) {
                        fes.add(fe);
                    }
                }
                if (!(fes.isEmpty())) {
                    fs.setFeatures(fes);
                    fSentences.add(fs);
                }
            }
            return fSentences;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ArrayList<String> getRelevantAspects(ArrayList<String> features,
                                                ArrayList<String> relvs) {
        ArrayList<String> returnedList = new ArrayList<String>();

        int sz = features.size();
        for (int i = 0; i < sz; i++) {

            if (relvs.get(i).equals("Relevant")) {
                returnedList.add(features.get(i));
            }

        }
        return returnedList;
    }

    void setSource(String query,DataSources ds, SearchOptions opts) {

        if (ds == DataSources.Twitter) {
            dataSource = new TwitterSource();
            ((TwitterSource) (dataSource)).setSearchQuery(query);
        }
        else if (ds == DataSources.InputFile){
          dataSource = new InputFileSource();
          ((InputFileSource)(dataSource)).setInReader(opts.getInputStream());
        }
        else {
            String site = "";
            if (ds == DataSources.Amazon) {
                site = "www.Amazon.com";
            } else if (ds == DataSources.Epinions) {
                site = "www.Epinions.com";
            }
            Bing.setQuery(query, site); // the resulting query will be
            Bing.doWork();
            ArrayList<String> urls = Bing.getURLs();
            dataSource = ds == DataSources.Amazon ? new AmazonSource(
                    urls.get(0)) : new EpinionSource(urls.get(0));
        }
    }

    public ResultSummary mainLogic(DSourcesLogic selectedDS) {
        ResultSummary finalResult = new ResultSummary();
        // Here going the logic
        String query;
        SearchOptions ops = selectedDS.getOps(); // here is where the GUi mut be
        for (DataSources ds : ops.getdSource()) {
            query = ds == DataSources.Twitter || ops.getTheQuery().isEmpty()? ops.getTheQuery() : ops.getTheQuery() + " Customer reviews";
            setSource(query,ds, ops);
            getReviewsFromSource();
            try {
                preprocess();
                if(ops.getAutomate())
                {
                    finalResult = sentimize();
                }
                else
                {
                    finalResult = manualSentimize(ops.getFeatures());
                }
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        return finalResult;
    }

    public void getReviewsFromSource() {
        Revs = dataSource.harvest();
    }
}
TOP

Related Classes of Filter.Controller

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.