Example #1
0
  private List<Word> ranking(SWCDocument document, CommandLineArguments cmd) {
    RankingAlgo algo = RankingAlgorithmRegistry.getById(cmd.getRankAlgorithm());
    document.weightFilter(cmd.getMaxWords(), algo);

    List<Word> words = document.getWords();
    if (words.size() < 10)
      throw new RuntimeException("The input text is too short (" + words.size() + " words)");

    return words;
  }
Example #2
0
  public static void main(String[] args) {
    CommandLineArguments cmd = new CommandLineArguments(args);

    if (!cmd.parse() || cmd.isPrintUsage()) {
      cmd.usage();
      System.exit(1);
    }

    try {
      new Main().constructWordCloud(cmd);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
Example #3
0
  private SWCDocument readDocument(CommandLineArguments cmd) throws FileNotFoundException {
    Scanner scanner =
        cmd.getInputFile() != null
            ? new Scanner(new File(cmd.getInputFile()))
            : new Scanner(System.in);
    StringBuilder sb = new StringBuilder();
    while (scanner.hasNextLine()) {
      sb.append(scanner.nextLine() + "\n");
    }
    scanner.close();

    SWCDocument doc = new SWCDocument(sb.toString());
    doc.parse(cmd.getParseOptions());

    return doc;
  }
Example #4
0
  private void constructWordCloud(CommandLineArguments cmd) throws Exception {
    // read the document
    SWCDocument document = readDocument(cmd);

    // init fonts
    FontUtils.initialize(new AWTFontProvider(cmd.getFont()));

    // rank the words
    List<Word> words = ranking(document, cmd);

    // calculate pairwise similarities
    Map<WordPair, Double> similarity = computeSimilarity(document, cmd);

    // create graph
    WordGraph wordGraph = new WordGraph(words, similarity);

    // layout the words in the plane
    LayoutResult layout = layout(wordGraph, cmd);

    // coloring
    ColorScheme colorScheme = coloring(wordGraph, cmd);

    // draw the result
    visualize(wordGraph, layout, colorScheme, cmd);
  }
Example #5
0
  private void visualize(
      WordGraph wordGraph, LayoutResult layout, ColorScheme colorScheme, CommandLineArguments cmd)
      throws FileNotFoundException {
    List<UIWord> uiWords = UIWord.prepareUIWords(wordGraph.getWords(), layout, colorScheme);
    WordCloudRenderer renderer =
        new WordCloudRenderer(uiWords, cmd.getMaxWidth(), cmd.getMaxHeight());
    byte[] content = RenderUtils.createCloud(renderer, cmd.getOutputFormat());

    OutputStream out = createOutputStream(cmd);

    try {
      out.write(content);
      out.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
Example #6
0
 private OutputStream createOutputStream(CommandLineArguments cmd) throws FileNotFoundException {
   if (cmd.isAutogenOutputFile() && cmd.getInputFile() != null) {
     String inputFile = cmd.getInputFile();
     int index = inputFile.lastIndexOf('.');
     String filename = (index != -1 ? inputFile.substring(0, index) : inputFile);
     filename += "." + cmd.getOutputFormat();
     return new FileOutputStream(filename);
   } else {
     return cmd.getOutputFile() != null ? new FileOutputStream(cmd.getOutputFile()) : System.out;
   }
 }
Example #7
0
 private ColorScheme coloring(WordGraph wordGraph, CommandLineArguments cmd) {
   ColorScheme colorScheme = ColorSchemeRegistry.getByCmdIndex(cmd.getColor());
   colorScheme.initialize(wordGraph);
   return colorScheme;
 }
Example #8
0
 private LayoutResult layout(WordGraph wordGraph, CommandLineArguments cmd) {
   LayoutAlgo algo = LayoutAlgorithmRegistry.getById(cmd.getLayoutAlgorithm());
   algo.setAspectRatio(cmd.getAspectRatio());
   return algo.layout(wordGraph);
 }
Example #9
0
 private Map<WordPair, Double> computeSimilarity(SWCDocument document, CommandLineArguments cmd) {
   SimilarityAlgo algo = SimilarityAlgorithmRegistry.getById(cmd.getSimilarityAlgorithm());
   return algo.computeSimilarity(document);
 }