SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Hollywood Style

Dependencies between classes and interfaces in the “Hollywood” style
Click on the image to see the “requires” dependency highlighted.

ThreePhaseFileProcessor

package hollywood;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ThreePhaseFileProcessor {

  public interface Loader { void load(String pathToFile) throws IOException; }
  public interface Worker { void work(); }
  public interface Finisher { void finish();  }

  private List<Loader> loaders = new ArrayList<Loader>();
  private List<Worker> workers = new ArrayList<Worker>();
  private List<Finisher> finishers = new ArrayList<Finisher>();

  public void registerLoader(Loader loader) {
    loaders.add(loader);
  }

  public void registerWorker(Worker worker) {
    workers.add(worker);
  }

  public void registerFinisher(Finisher finisher) {
    finishers.add(finisher);
  }

  public void run(String pathToFile) {
    try {
      for (Loader loader : loaders) {
        loader.load(pathToFile);
      }
      for (Worker worker : workers) {
        worker.work();
      }
      for (Finisher finisher : finishers) {
        finisher.finish();
      }
    } catch (IOException e) {
      System.out.println("Could not process all files: " + e);
    }
  }

}

WordFilter

package hollywood;

public interface WordFilter {

  public boolean accept(String word);

}

StopWordFilter

package hollywood;

import hollywood.ThreePhaseFileProcessor.Loader;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class StopWordFilter implements Loader, WordFilter {

  private static final String ENCODING = "UTF-8";
  private static final String STOP_WORDS_TXT = "stop_words.txt";
  private static final String ANY_NON_WORD_CHARACTERS_OR_UNDERSCORE = "[\\W_]+";

  Set<String> stopWords = new HashSet<String>();

  public StopWordFilter(ThreePhaseFileProcessor processor) {
    processor.registerLoader(this);
  }

  @Override
  public void load(String ignore) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(STOP_WORDS_TXT), ENCODING);
    scanner.useDelimiter(ANY_NON_WORD_CHARACTERS_OR_UNDERSCORE);
    while (scanner.hasNext()) {
      stopWords.add(scanner.next().toLowerCase());
    }
    scanner.close();
    for (char c = 'a'; c <= 'z'; c++) {
      stopWords.add(Character.toString(c));
    }
  }

  @Override
  public boolean accept(String word) {
    return !stopWords.contains(word);
  }

}

WordStorage

package hollywood;

import hollywood.ThreePhaseFileProcessor.Loader;
import hollywood.ThreePhaseFileProcessor.Worker;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class WordStorage implements Loader, Worker {

  public interface WordHandler { void handle(String word); }

  private static final String ANY_NON_WORD_CHARACTER_OR_UNDERSCORE = "[\\W_]+";
  private static final String ENCODING = "UTF-8";

  private WordFilter filter;
  private List<WordHandler> handlers = new ArrayList<WordHandler>();

  String fileContent;

  public WordStorage(ThreePhaseFileProcessor processor, WordFilter filter) {
    this.filter = filter;
    processor.registerLoader(this);
    processor.registerWorker(this);
  }

  public void registerWordHandler(WordHandler handler) {
    handlers.add(handler);
  }

  @Override
  public void load(String pathToFile) throws IOException {
    File file = new File(pathToFile);
    int length = (int) file.length();
    FileInputStream stream = null;
    stream = new FileInputStream(file);
    byte[] rawData = new byte[length];
    stream.read(rawData);
    stream.close();
    fileContent = new String(rawData, ENCODING);
  }

  @Override
  public void work() {
    Scanner scanner;
    scanner = new Scanner(fileContent);
    scanner.useDelimiter(ANY_NON_WORD_CHARACTER_OR_UNDERSCORE);
    while (scanner.hasNext()) {
      String word = scanner.next().toLowerCase();
      if (!filter.accept(word))
        continue;
      for (WordHandler handler : handlers) {
        handler.handle(word);
      }
    }
    scanner.close();
  }

}

WordFrequencyCounter

package hollywood;

import hollywood.ThreePhaseFileProcessor.Finisher;
import hollywood.WordStorage.WordHandler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class WordFrequencyCounter implements Finisher, WordHandler {

  Map<String, Integer> wordFrequencies = new HashMap<String, Integer>();

  public WordFrequencyCounter(ThreePhaseFileProcessor processor, WordStorage storage) {
    processor.registerFinisher(this);
    storage.registerWordHandler(this);
  }

  @Override
  public void handle(String word) {
    Integer count = wordFrequencies.get(word);
    wordFrequencies.put(word, (count == null) ? 1 : count + 1);
  }

  @Override
  public void finish() {

    List<Entry<String, Integer>> orderedFrequencies = new ArrayList<Entry<String, Integer>>();
    orderedFrequencies.addAll(wordFrequencies.entrySet());
    Comparator<Entry<String, Integer>> comparator = new Comparator<Entry<String, Integer>>() {
      public int compare(Entry<String, Integer> left, Entry<String, Integer> right) {
        return -left.getValue().compareTo(right.getValue());
      }
    };
    Collections.sort(orderedFrequencies, comparator);

    Iterator<Entry<String, Integer>> it = orderedFrequencies.iterator();
    for (int printed = 0; it.hasNext() && (printed < 25); printed++) {
      Entry<String, Integer> wordFrequency = it.next();
      System.out.println("" + wordFrequency.getKey() + " - " + wordFrequency.getValue());
    }

  }

}

Main

package hollywood;

public class Main {

  public static void main(String[] args) {

    ThreePhaseFileProcessor processor = new ThreePhaseFileProcessor();

    StopWordFilter filter = new StopWordFilter(processor);
    WordStorage storage = new WordStorage(processor, filter);
    new WordFrequencyCounter(processor, storage);

    processor.run(args[0]);
  }

}
teaching/seminars/style/2014/hollywood_style.txt · Last modified: 2018/05/24 15:11 by Daniel Speicher

SEWiki, © 2021