Word Count Text Table (literally. a table)
in
Share your Work
•
1 year ago
What's the role of Computational Design in Industrial Design?
A series of experiments in how we might design physical objects with code.
Tell me what you think!
This "table" takes a .txt file as input, this one uses Hamlet, sorts it to find word count, then makes a "table" out of the top 256 terms.
- import peasy.org.apache.commons.math.*;
- import peasy.*;
- import peasy.org.apache.commons.math.geometry.*;
- import processing.opengl.*;
- PeasyCam cam;
- HashMap newWords; // HashMap object
- HashMap commonWords; // HashMap object
- HashMap sortedWords;
- HashMap countedWords;
- float blockSize=1;
- float blockWidth = 10;
- float blockHeight = 1;
- String[] newTokens; // Array of all words from input file
- String[] commonTokens; // Array of all words from input file
- List mapKeys;
- List mapKeysClone;
- List<Integer> mapValues;
- List<Integer> mapValuesClone;
- ArrayList ascWords;
- int counter;
- PFont displayFont;
- PFont font;
- void setup(){
- size(800, 800, OPENGL);
- fill(0);
- perspective(PI/3, 1, 5, 10000);
- cam = new PeasyCam(this,0,0,-150, 500);
- //cam.setMaximumDistance(10000);
- cam.rotateX(-PI/3);
- font = loadFont("Inconsolata-24.vlw");
- textFont(font, 2);
- //textMode(SCREEN);
- newWords = new HashMap();
- commonWords = new HashMap();
- sortedWords = new HashMap();
- countedWords = new HashMap();
- // Load new file and chop it up
- String[] newLines = loadStrings("hamlet.txt");
- String allNewText = join(newLines, " ");
- String[] commonLines = loadStrings("100words.txt");
- String allCommonText = join(commonLines, " ");
- //create hash table
- newTokens = splitTokens(allNewText, " -/%=#&,.?!:;[]1234567890()\t\n\r\f");
- commonTokens = splitTokens(allCommonText, " -/%=#&,.?!:;[]1234567890()\t\n\r\f");
- //println(newTokens.length);
- for (int iWord = 0; iWord < commonTokens.length; iWord++) {
- commonWords.put(commonTokens[iWord].toLowerCase(), commonTokens[iWord].toLowerCase());
- }
- for (int i=0; i<newTokens.length; i++){
- String newString = newTokens[i].toLowerCase();
- counter = (counter + 1) % newTokens.length;
- if (!commonWords.containsKey(newString)) {
- if (newWords.containsKey(newString)) {
- // Get the word object and increase the count
- // We access objects from a HashMap via its key, the String
- Word includedWord = (Word) newWords.get(newString);
- includedWord.countIncrease();
- } else {
- // Otherwise make a new word
- Word includedWord = new Word(newString);
- // And add to the HashMap
- // put() takes two arguments, "key" and "value"
- // The key for us is the String and the value is the Word object
- newWords.put(newString, includedWord);
- }
- }
- }
- //print(newWords.keySet());
- // Make an iterator to look at all the things in the HashMap
- Iterator inw = newWords.values().iterator();
- while (inw.hasNext()) {
- Word wordChk = (Word) inw.next();
- //println(wordChk.word + ": " + wordChk.count);
- countedWords.put(wordChk.word, wordChk.count);
- }
- mapKeys = new ArrayList(countedWords.keySet());
- mapKeysClone = new ArrayList(countedWords.keySet());
- mapValues = new ArrayList(countedWords.values());
- mapValuesClone = new ArrayList(countedWords.values());
- ascWords = new ArrayList();
- Collections.sort(mapValues);
- Collections.sort(mapValuesClone);
- Iterator valueIt = mapValues.iterator();
- while (valueIt.hasNext()) {
- Object val = valueIt.next();
- Iterator keyIt = mapKeys.iterator();
- while (keyIt.hasNext()) {
- Object ky = keyIt.next();
- String comp1 = countedWords.get(ky).toString();
- String comp2 = val.toString();
- if (comp1.equals(comp2)){
- countedWords.remove(ky);
- mapKeys.remove(ky);
- ascWords.add(ky.toString());
- break;
- }
- }
- //println(sortedMap);
- }
- //println(sortedWords);
- }
- float xpos;
- float ypos;
- float rad;
- float lastrad=0;
- float radinc = 0.25;
- float ang;
- float lastang=0;
- float anginc = PI/32;
- float lastx, lasty;
- void draw(){
- background(255);
- //println("drawing");
- xpos = 0;
- ypos = 0;
- rad = 0;
- ang = 0;
- lastx = 0;
- lasty = 0;
- int rowLenY=1;
- int rowLenX=1;
- int movedX=0;
- int movedY=0;
- boolean whoseTurn = true; // true=x, false=y
- boolean xisdone = false;
- boolean yisdone = false;
- int posneg = 1;
- for (int i=mapValuesClone.size(); i>mapValuesClone.size()-256; i--){
- float imap = map(i, mapValuesClone.size(), mapValuesClone.size()-256, 0, 256);
- //println(imap);
- int num = (int) mapValuesClone.get(i-1);
- String ltrs = (String) ascWords.get(i-1);
- stroke(0);
- //xpos = rad*cos(ang);
- //ypos = rad*sin(ang);
- for (int j=0; j<num; j++){
- fill(50, 150);
- stroke(0);
- pushMatrix();
- translate(xpos*blockWidth, ypos*blockWidth, -j*blockHeight);
- box(blockWidth, blockWidth, blockHeight);
- popMatrix();
- }
- fill(50);
- //line(xpos,ypos,0, xpos,ypos,num);
- pushMatrix();
- textAlign(CENTER);
- translate(xpos*blockWidth, ypos*blockWidth, blockHeight/2 + 1);
- //rotateX(-PI/2);
- //rotateY(-PI/4);
- // if (whoseTurn == true){
- // rotateY(-posneg*PI/2);
- // } else {
- // rotateY(-posneg*PI);
- // }
- rotateZ(-PI/4);
- //rotateZ(ang);
- fill(200);
- text(ltrs, 0,0,0);
- popMatrix();
- if (whoseTurn == true){
- xpos = lastx + (posneg*blockSize);
- movedX++;
- } else {
- ypos = lasty + (posneg*blockSize);
- movedY++;
- }
- if (whoseTurn == true && movedX == rowLenX){
- whoseTurn = !whoseTurn;
- rowLenX++;
- xisdone = true;
- movedX=0;
- } else if (whoseTurn == false && movedY == rowLenY){
- whoseTurn = !whoseTurn;
- rowLenY++;
- yisdone = true;
- movedY=0;
- }
- if (xisdone == true && yisdone == true){
- posneg *= -1;
- xisdone = false;
- yisdone = false;
- }
- lastrad = rad;
- lastang = ang;
- rad += radinc;
- ang += anginc;
- lastx = xpos;
- lasty = ypos;
- }
- // if (frameCount%5==0){
- // //println("save!");
- // String Date = year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second() + ".tiff";
- // save(Date);
- // }
- }
- class Word {
- int count;
- String word;
- Word(String s) {
- word = s;
- count = 1;
- }
- void countIncrease() {
- count++;
- }
- }
- void keyPressed() {
- if (key == 's'){
- String Date = year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second() + ".tiff";
- save(Date);
- println("saved img");
- }
- }
An early version that is prettier, but harder to 'make'.
Hamlet
And of course I ran it on the bible.