Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. /**
  2. * CountingString example
  3. * by Daniel Shiffman.
  4. *
  5. * This example demonstrates how to use a IntDict to store
  6. * a number associated with a String. Java HashMaps can also
  7. * be used for this, however, this example uses the IntDict
  8. * class offered by Processing's data package for simplicity
  9. * and added functionality.
  10. *
  11. * This example uses the IntDict to perform a simple concordance
  12. * http://en.wikipedia.org/wiki/Concordance_(publishing)
  13. *
  14. */
  15.  
  16. // An IntDict pairs Strings with integers
  17. IntDict concordance;
  18.  
  19. // The raw array of words in
  20. String[] tokens;
  21. int counter = 0;
  22.  
  23. void setup() {
  24. size(640, 360);
  25.  
  26. concordance = new IntDict();
  27.  
  28. // Load file and chop it up
  29. String[] lines = loadStrings("dracula.txt");
  30. String allText = join(lines, " ").toLowerCase();
  31. tokens = splitTokens(allText, " ,.?!:;[]-\"");
  32.  
  33. // Create the font
  34. textFont(createFont("SourceCodePro-Regular.ttf", 24));
  35. }
  36.  
  37. void draw() {
  38. background(51);
  39. fill(255);
  40.  
  41. // Look at words one at a time
  42. if (counter < tokens.length) {
  43. String s = tokens[counter];
  44. counter++;
  45. concordance.increment(s);
  46. }
  47.  
  48. // x and y will be used to locate each word
  49. float x = 0;
  50. float y = 48;
  51.  
  52. concordance.sortValues();
  53.  
  54. String[] keys = concordance.keyArray();
  55.  
  56. // Look at each word
  57. for (String word : keys) {
  58. int count = concordance.get(word);
  59.  
  60. // Only display words that appear 3 times
  61. if (count > 3) {
  62. // The size is the count
  63. int fsize = constrain(count, 0, 48);
  64. textSize(fsize);
  65. text(word, x, y);
  66. // Move along the x-axis
  67. x += textWidth(word + " ");
  68. }
  69.  
  70. // If x gets to the end, move y
  71. if (x > width) {
  72. x = 0;
  73. y += 48;
  74. // If y gets to the end, we're done
  75. if (y > height) {
  76. break;
  77. }
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement