package concordance.processing; import java.io.*; import java.nio.*; import java.nio.channels.*; import java.util.*; import a2z.*; import java.util.regex.*; import concordance.treemap.Word; import processing.core.PApplet; import processing.core.PFont; import krister.Ess.*; import rita.*; import rita.RiPosTagger; public class ConcordanceProcessing3 extends PApplet { AudioChannel mySound; AudioChannel mySound2; TreeMap words; PFont f; Iterator iterator; boolean iteratorSet; int width = 1300; int height = 800; int currentWord = 0; Vector tokenVector = new Vector(); Vector wordCountVector = new Vector(); Vector posVector = new Vector(); String outputWords = ""; String outputWordCounts = ""; String outputPos = ""; RiPosTagger postTagger = new RiPosTagger(null); public static void main(String _args[]) { PApplet.main(new String[] { getQualifiedClassName() }); } public static String getQualifiedClassName() { return new Exception().getStackTrace()[1].getClassName(); } public void setup() { size(width, height); frameRate(25); Ess.start(this); mySound = new AudioChannel("snarekick1.aif"); mySound.smoothPan = true; // mySound.pan(Ess.LEFT); mySound2 = new AudioChannel("bassdrum1.aif"); mySound2.smoothPan = true; // mySound2.pan(Ess.LEFT); fillConcordance("obama1.txt"); f = createFont("Georgia", 16, true); iteratorSet = false; fill(255, 0, 0); rect(0, 0, width, height); } public void draw() { background(255); if (currentWord < tokenVector.size()-1) { textAlign(CENTER); String thisWord = tokenVector.elementAt(currentWord); String thisPos = postTagger.tag(thisWord); String thisNextWord = tokenVector.elementAt(currentWord + 1); int thisWordCount = (Integer.parseInt(wordCountVector.elementAt(currentWord))); System.out.print(currentWord + " " + thisWord + " (" + thisPos.indexOf("v") + " "); // "); fill((thisWordCount * 40), 0, 0); // fill(0,0,0); rect(0, 0, width, height); fill(255 - (thisWordCount - 1) * 80, 0, 0); // fill(255,0,0); int thisFontSize = 0; thisFontSize = 300 - thisWordCount * 30; if (thisFontSize < 30) { thisFontSize = 30; } textFont(f, thisFontSize); // System.out.println(thisWord + " " + currentWord + " " + thisWordCount); text(thisWord, width / 2, height / 2 + thisFontSize / 5); // text(thisWord + " " + currentWord + " " + thisWordCount, 200, 200); // System.out.println(currentWord); /* if(thisWordCount == 1){ mySound.play(); mySound2.play(); delay(300); mySound.stop(); mySound2.stop(); } else if (thisWordCount > 1) { mySound2.play(); delay(300); mySound2.stop(); } else { delay(200); } */ delay(500); currentWord++; } else { currentWord = 0; } } public void fillConcordance(String path) { try { A2ZFileReader fr = new A2ZFileReader(path); String content = fr.getContent(); words = new TreeMap(); // String regex = " "; // String tokens[] = content.split("\""); String firstTokens[] = content.split(" "); String output = ""; String[] regexInArray = {" —", "I ", "\\b(it|It)", "\\b(\\w{8,20}),", "\\bI (\\w+) ", "\\bhimself\\b", "\\bThat\\b", "\\that\\b", "\\bThey\\b", "\\bthey\\b", "(\\w{7,20})\\.", "\\bhave to\\b", /*"\\bhave\\b",*/ "\\b(his|her)\\b", "\\bhe\\b", "\\bHe\\b", "(\\w) (\\w{9,10})\\b", "( \\w{11,20})\\b", "\\?", "\\bare[^,]", "\\bis\\b", "\\bbe\\b", ",,"}; String[] regexOutArray = {" ", "I, like, ", "$0, like,", "$1, and stuff,", "I $1, like, ", "hisself", "That", "dat", "Dudes", "dudes", " $1 and whatnot.", "gotta", /*"got",*/ "dude's", "dude", "Dude","$1 uh, $2", " um,$1", ", you know?", "are totally ", "is totally ", "totally be", ","}; // for (int j = 0; j < firstTokens.length; j++) { // if (j % 2 == 1) { for (int i = 0; i < regexInArray.length; i++) { content = content.replaceAll(regexInArray[i], regexOutArray[i]); } // output += " " + firstTokens[j]; // } else { // output += inputArray[j]; // } // } String tokens[] = content.split(" "); // String tokens[] = content.split(regex); // Pattern p = Pattern.compile("[a-z']+", Pattern.CASE_INSENSITIVE); Pattern p = Pattern.compile("\\S+", Pattern.CASE_INSENSITIVE); // For every word for (int i = 0; i < tokens.length; i++) { System.out.println(tokens[i]); String s = tokens[i].toLowerCase(); // If it matches our regex, insert it in the tree Matcher m = p.matcher(s); if (m.matches()) { if (words.containsKey(s)) { Word w = (Word) words.get(s); w.count(); } else { Word w = new Word(s); words.put(s, w); w.getWord(); } } } String wordsString = ""; String wordCountsString = ""; String posString = ""; for (int i = 0; i < tokens.length; i++) { if (words.containsKey(tokens[i].toLowerCase())) { String thisWord = tokens[i].toLowerCase(); String thisGetPos = postTagger.tag(thisWord); String thisPos; if (thisGetPos.indexOf("v") > -1) { thisPos = "v"; } else { thisPos = "X"; } Word wo = (Word) words.get(tokens[i].toLowerCase()); tokenVector.add(tokens[i]); int wordCount = wo.getCount(); String wordCountString = Integer.toString(wordCount); wordCountVector.add(wordCountString); /* System.out.println(tokenVector.size() + " " + tokenVector.elementAt(tokenVector.size()-1) + " " + wordCountVector.elementAt(wordCountVector.size()-1)); */ wordsString += tokens[i] + " "; wordCountsString += wordCount + " "; posString += thisPos + " "; } } FileOutputStream fos = new FileOutputStream("newTest.txt"); FileChannel outfc = fos.getChannel(); String variablesString = "variable1=" + wordsString + "&variable2=" + wordCountsString + "&variable3=" + posString + "&"; ByteBuffer bb = ByteBuffer.wrap(variablesString.getBytes()); outfc.write(bb); outfc.close(); } catch (IOException e) { // //System.out.println("File I/O Error"); e.printStackTrace(); } } public void stop() { Ess.stop(); // When program stops, stop Ess too super.stop(); } }