Toxiclib example
in
Programming Questions
•
1 year ago
Dear all interested readers,
I am currently seeking for methods to visualise a list of keywords I obtained from my webserver. The example I used is from Toxiclibs. I changed some stuff, but I don't know how to delete the "WordLengthComparator" and the "FrequencyComparator". The list I use is allready alphabetical, so I just want the list to be unmodified.
Here's my code:
// start
boolean aan = false;
PFont font;
import toxi.util.*;
import toxi.math.*;
List<HistogramWord> uniqueWords;
ZoomLensInterpolation zoomLens = new ZoomLensInterpolation();
float smoothStep=0.12;
int gap=30;
int labelGap=0;
int wordLimit=1900;
HistogramSorter sortFunction;
void setup() {
size(680, 800);
font = createFont("Arial", 40);
textFont(font, 40);
initHistogram();
String[] fontList = PFont.list();
println(fontList);
}
void draw() {
float maxBarHeight= height-labelGap-20;
float normFactor=maxBarHeight/sortFunction.getMetric(uniqueWords.get(0));
float focalPos=map(mouseY, gap, width-gap, 0.0, 1.0);
zoomLens.setLensPos(focalPos, smoothStep);
zoomLens.setLensStrength(map(mouseX, 0, width/5, -1, +1), smoothStep);
int focalX=(int)zoomLens.interpolate(gap, width-gap, focalPos);
background(255);
noStroke();
textAlign(LEFT);
fill(0);
int space=10;
int maxWords=min(uniqueWords.size(), wordLimit);
for (int i=0; i<maxWords; i++) {
float relativePos=(float)i/maxWords;
HistogramWord w=uniqueWords.get(i);
float barHeight=sortFunction.getMetric(w)*normFactor;
int x=(int)zoomLens.interpolate(gap, width-gap, relativePos);
int x2=(int)zoomLens.interpolate(gap, width-gap, (float)(i+1)/maxWords);
int barWidth=max(x2-x-1, 1);
int barCenter=(x+x2)/2;
if (abs(barCenter-focalX)<=barWidth/2) {
fill(0, 0, 255);
// println(w.word);
if (aan == true) {
println("GO"+w.word);
}
aan = false;
}
else {
fill(0);
}
// rect(width/2, (int)x, -barHeight, barWidth);
rect(40, (int)x, -30, barWidth);
float ts=min(x2-x, 30);
if (ts>3) {
textSize(ts);
pushMatrix();
translate(48, barCenter+ts/4);
text(w.word, 0, 0);
//text("("+sortFunction.getMetric(w)+") "+w.word, 0, 0);
popMatrix();
}
}
}
void initHistogram() {
String fileName=null;
if (fileName==null) {
fileName="http://www.witregel.nl/scraper/getkeywords.php";
}
String[] lines= loadStrings(fileName);
for ( int i = 0; i < lines.length; i++) {
lines[i] = lines[i].replaceAll("charset=utf", "");
lines[i] = lines[i].replaceAll("equiv=", "");
lines[i] = lines[i].replaceAll("8", "");
lines[i] = lines[i].replaceAll("type", "");
lines[i] = lines[i].replaceAll("head><body>aaa", "");
lines[i] = lines[i].replaceAll("lang=", "");
lines[i] = lines[i].replaceAll("html", "");
lines[i] = lines[i].replaceAll("><head><meta http", "");
lines[i] = lines[i].replaceAll("html", "");
lines[i] = lines[i].replaceAll("http", "");
lines[i] = lines[i].replaceAll("www", "");
lines[i] = lines[i].replaceAll("< xmlns=", "");
lines[i] = lines[i].replaceAll("><", "");
lines[i] = lines[i].replaceAll("hntm", "");
lines[i] = lines[i].replaceAll("contenttype", "");
lines[i] = lines[i].replaceAll("text", "");
lines[i] = lines[i].replaceAll("body>", "");
lines[i] = lines[i].replaceAll("xml:", "");
lines[i] = lines[i].replaceAll("-", "");
lines[i] = lines[i].replaceAll("content-type", "");
lines[i] = lines[i].replaceAll("content=", "");
lines[i] = lines[i].replaceAll("text/; -", "");
lines[i] = lines[i].replaceAll("/></-status", "");
lines[i] = lines[i].replaceAll("://.w3.org/1999/x", "");
lines[i] = lines[i].replaceAll("<", "");
}
int totalWordCount = 0;
HashMap<String, Integer> histogram=new HashMap<String, Integer>();
for (int i=0; i<lines.length; i++) {
if (0==i %2) {
println("processing line : " +i);
}
String[] words=splitTokens(lines[i], ",./?!:():;\"-&");
// String[] words=splitTokens(lines[i], " ,./?!:():;'\"-&");
for (String w : words) {
totalWordCount++;
w=w.toLowerCase();
if (histogram.get(w)==null) {
histogram.put(w, 1);
println("new word: "+w);
}
else {
// histogram.put(w, histogram.get(w)+1);
}
}
}
println("----\nstatistics:");
println("total: "+totalWordCount);
println("unique: "+histogram.size());
uniqueWords=new ArrayList<HistogramWord>();
for (String w : histogram.keySet()) {
HistogramWord hw=new HistogramWord(w, histogram.get(w));
uniqueWords.add(hw);
}
setSortFunction(new FrequencyComparator());
for (int i=0; i<1; i++) {
println(uniqueWords.get(i).word.length());
}
}
void keyPressed() {
if (key=='g') {
aan = true;
}
if (key=='f') {
setSortFunction(new FrequencyComparator());
}
if (key=='l') {
setSortFunction(new WordLengthComparator());
}
if (key=='r') {
initHistogram();
}
}
// Niet herhalen
void setSortFunction(HistogramSorter s) {
sortFunction=s;
Collections.sort(uniqueWords, sortFunction);
}
class HistogramWord {
String word;
int count;
HistogramWord(String w, int c) {
word = w;
count = c;
}
String toString() {
return word+": "+count;
}
}
abstract class HistogramSorter implements Comparator<HistogramWord> {
abstract int compare(HistogramWord a, HistogramWord b);
abstract int getMetric(HistogramWord w);
}
class FrequencyComparator extends HistogramSorter {
int compare(HistogramWord a, HistogramWord b) {
return b.count-a.count;
}
public int getMetric(HistogramWord w) {
return w.count;
}
}
class WordLengthComparator extends HistogramSorter {
int compare(HistogramWord a, HistogramWord b) {
return b.word.length()-a.word.length();
}
public int getMetric(HistogramWord w) {
return w.word.length();
}
}
// end
Hope someone can advise me on this matter.
Thanks in advance.
I am currently seeking for methods to visualise a list of keywords I obtained from my webserver. The example I used is from Toxiclibs. I changed some stuff, but I don't know how to delete the "WordLengthComparator" and the "FrequencyComparator". The list I use is allready alphabetical, so I just want the list to be unmodified.
Here's my code:
// start
boolean aan = false;
PFont font;
import toxi.util.*;
import toxi.math.*;
List<HistogramWord> uniqueWords;
ZoomLensInterpolation zoomLens = new ZoomLensInterpolation();
float smoothStep=0.12;
int gap=30;
int labelGap=0;
int wordLimit=1900;
HistogramSorter sortFunction;
void setup() {
size(680, 800);
font = createFont("Arial", 40);
textFont(font, 40);
initHistogram();
String[] fontList = PFont.list();
println(fontList);
}
void draw() {
float maxBarHeight= height-labelGap-20;
float normFactor=maxBarHeight/sortFunction.getMetric(uniqueWords.get(0));
float focalPos=map(mouseY, gap, width-gap, 0.0, 1.0);
zoomLens.setLensPos(focalPos, smoothStep);
zoomLens.setLensStrength(map(mouseX, 0, width/5, -1, +1), smoothStep);
int focalX=(int)zoomLens.interpolate(gap, width-gap, focalPos);
background(255);
noStroke();
textAlign(LEFT);
fill(0);
int space=10;
int maxWords=min(uniqueWords.size(), wordLimit);
for (int i=0; i<maxWords; i++) {
float relativePos=(float)i/maxWords;
HistogramWord w=uniqueWords.get(i);
float barHeight=sortFunction.getMetric(w)*normFactor;
int x=(int)zoomLens.interpolate(gap, width-gap, relativePos);
int x2=(int)zoomLens.interpolate(gap, width-gap, (float)(i+1)/maxWords);
int barWidth=max(x2-x-1, 1);
int barCenter=(x+x2)/2;
if (abs(barCenter-focalX)<=barWidth/2) {
fill(0, 0, 255);
// println(w.word);
if (aan == true) {
println("GO"+w.word);
}
aan = false;
}
else {
fill(0);
}
// rect(width/2, (int)x, -barHeight, barWidth);
rect(40, (int)x, -30, barWidth);
float ts=min(x2-x, 30);
if (ts>3) {
textSize(ts);
pushMatrix();
translate(48, barCenter+ts/4);
text(w.word, 0, 0);
//text("("+sortFunction.getMetric(w)+") "+w.word, 0, 0);
popMatrix();
}
}
}
void initHistogram() {
String fileName=null;
if (fileName==null) {
fileName="http://www.witregel.nl/scraper/getkeywords.php";
}
String[] lines= loadStrings(fileName);
for ( int i = 0; i < lines.length; i++) {
lines[i] = lines[i].replaceAll("charset=utf", "");
lines[i] = lines[i].replaceAll("equiv=", "");
lines[i] = lines[i].replaceAll("8", "");
lines[i] = lines[i].replaceAll("type", "");
lines[i] = lines[i].replaceAll("head><body>aaa", "");
lines[i] = lines[i].replaceAll("lang=", "");
lines[i] = lines[i].replaceAll("html", "");
lines[i] = lines[i].replaceAll("><head><meta http", "");
lines[i] = lines[i].replaceAll("html", "");
lines[i] = lines[i].replaceAll("http", "");
lines[i] = lines[i].replaceAll("www", "");
lines[i] = lines[i].replaceAll("< xmlns=", "");
lines[i] = lines[i].replaceAll("><", "");
lines[i] = lines[i].replaceAll("hntm", "");
lines[i] = lines[i].replaceAll("contenttype", "");
lines[i] = lines[i].replaceAll("text", "");
lines[i] = lines[i].replaceAll("body>", "");
lines[i] = lines[i].replaceAll("xml:", "");
lines[i] = lines[i].replaceAll("-", "");
lines[i] = lines[i].replaceAll("content-type", "");
lines[i] = lines[i].replaceAll("content=", "");
lines[i] = lines[i].replaceAll("text/; -", "");
lines[i] = lines[i].replaceAll("/></-status", "");
lines[i] = lines[i].replaceAll("://.w3.org/1999/x", "");
lines[i] = lines[i].replaceAll("<", "");
}
int totalWordCount = 0;
HashMap<String, Integer> histogram=new HashMap<String, Integer>();
for (int i=0; i<lines.length; i++) {
if (0==i %2) {
println("processing line : " +i);
}
String[] words=splitTokens(lines[i], ",./?!:():;\"-&");
// String[] words=splitTokens(lines[i], " ,./?!:():;'\"-&");
for (String w : words) {
totalWordCount++;
w=w.toLowerCase();
if (histogram.get(w)==null) {
histogram.put(w, 1);
println("new word: "+w);
}
else {
// histogram.put(w, histogram.get(w)+1);
}
}
}
println("----\nstatistics:");
println("total: "+totalWordCount);
println("unique: "+histogram.size());
uniqueWords=new ArrayList<HistogramWord>();
for (String w : histogram.keySet()) {
HistogramWord hw=new HistogramWord(w, histogram.get(w));
uniqueWords.add(hw);
}
setSortFunction(new FrequencyComparator());
for (int i=0; i<1; i++) {
println(uniqueWords.get(i).word.length());
}
}
void keyPressed() {
if (key=='g') {
aan = true;
}
if (key=='f') {
setSortFunction(new FrequencyComparator());
}
if (key=='l') {
setSortFunction(new WordLengthComparator());
}
if (key=='r') {
initHistogram();
}
}
// Niet herhalen
void setSortFunction(HistogramSorter s) {
sortFunction=s;
Collections.sort(uniqueWords, sortFunction);
}
class HistogramWord {
String word;
int count;
HistogramWord(String w, int c) {
word = w;
count = c;
}
String toString() {
return word+": "+count;
}
}
abstract class HistogramSorter implements Comparator<HistogramWord> {
abstract int compare(HistogramWord a, HistogramWord b);
abstract int getMetric(HistogramWord w);
}
class FrequencyComparator extends HistogramSorter {
int compare(HistogramWord a, HistogramWord b) {
return b.count-a.count;
}
public int getMetric(HistogramWord w) {
return w.count;
}
}
class WordLengthComparator extends HistogramSorter {
int compare(HistogramWord a, HistogramWord b) {
return b.word.length()-a.word.length();
}
public int getMetric(HistogramWord w) {
return w.word.length();
}
}
// end
Hope someone can advise me on this matter.
Thanks in advance.
1