jeudi 3 novembre 2016

Mood of a Website with multithreading

I am developing an engine for an application, which will determine the mood of a website and which needs to be updated to reflect the current threads on a website.

For creating the engine the following things need to be taken in consideration: Determine how to keep the results in memory; What type(s) of collections are useful here?; What Design Patterns will help?; Does the Graph have its’ own mechanism to store things, and if so, can you make use of that or do you need to map things? Or would storing results doubly ease development (SortedMap: stores the collection as both a Map and a SortedSet, and uses the one most close to the method call being processed.); What building blocks does the engine use? Reading sites, processing the text, counting words, where do the results go?; What would the threads be in this case? And how to synchronize them?

I believe that I have to use a producer consumer pattern (such as the one from here http://ift.tt/2f6g00g). I created the way in which to get the mood of a webpage, however I don’t know how to implement the threading.

My current code:

public class Engine implements Runnable{

    public static String return1;
    public static String inputLine;
    public static String print2;
    public static int sum = 0;
    public static Object j = ControllerAddWebsite.giveURLLabel; // gets the url
    public static String getEngine(String url) throws Exception, MalformedURLException {
    CSVReader reader = null;


    try {

            URL url_link = new URL((String) j);
            // URL url_link = new URL("http://ift.tt/28OY4xH");//Average = 0.5734597156398105 total words similar211.0 the total sum is:121
            URLConnection yc = url_link.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));

            StringBuilder a = new StringBuilder();
            String[] nextLine;

            while ((inputLine = in.readLine()) != null) {
                a.append(inputLine.replaceAll("\\<[^>]*>", " ").replaceAll("[^\\p{L}- ]", "").toUpperCase()).append('\n');
            }
            return1 = a.toString();
            in.close();
            double wordsCount = 0;

            String[] t = return1.split(" ");
            for (String s : t) {
                reader = new CSVReader(new FileReader("WordValues.csv"), ';');

                while ((nextLine = reader.readNext()) != null) {
                    int closingScore = 0;
                    try {
                        closingScore = Integer.parseInt(nextLine[1]);
                    } catch (NumberFormatException ignore) {

                    }

                    String c = nextLine[0];
                    if (s.equalsIgnoreCase(c)) {

                        sum += closingScore;
                        wordsCount++;
                        System.out.println("you are here" + wordsCount);
                    }
                }
            }
            print2 = "Average = " + (sum / wordsCount) + "\n total words similar" + wordsCount + "\n the total sum is:"
                    + sum;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ignore) {

            }
        }

        return print2;
    }

    public static String textread;


    public static void main(String[] args) throws Exception, MalformedURLException {

        try {
            textread = getEngine(textread);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(textread);

    }

My question would be: how to implement the threading to my code? In order to automatically update if there is any change on a website, so that the application can recalculate the new mood.

Aucun commentaire:

Enregistrer un commentaire