88



int nodeCount;
Node[] nodes = new Node[100];
HashMap nodeTable = new HashMap();

int edgeCount;
Edge[] edges = new Edge[500];

static final color nodeColor =#89F2FF;
static final color selectColor = #FF3030;
static final color fixedColor = #FF8080;
static final color edgeColor = #E8FCFF;

PFont font;

void setup() {
size(600,600);
loadData();
font = createFont("SansSerif",10);
textFont(font);
smooth();
}

void loadData() {
String[]lines = loadStrings("huckfinn.txt");

String line = join(lines,"");
line = line.replaceAll("__","\u2014");

String[]phrases = splitTokens(line,".,;:?!\u2014\"");
for(int i = 0; i < phrases.length; i++){
String phrase = phrases[i].toLowerCase();
String[]words = splitTokens(phrase," ");
for(int w =0; w < words.length-1; w++){
addEdge(words[w],words[w+1]);
}
}
}

void addEdge(String fromLabel,String toLabel){

if(ignoreWord(fromLabel)||ignoreWord(toLabel)) return;

Node from = findNode(fromLabel);
Node to = findNode(toLabel);
from.increment();
to.increment();

for(int i = 0; i < edgeCount; i++){
if(edges[i].from == from && edges[i].to == to){
edges[i].increment();
return;
}
}

Edge e = new Edge(from, to);
e.increment();

if(edgeCount == edges.length){
edges = (Edge[]) expand(edges);
}
edges[edgeCount++] = e;
}

String[] ignore = { "a","of","the","i","it","you","and","to"};

boolean ignoreWord(String what){
for(int i =0; i < ignore.length; i++){
if(what.equals(ignore[i])){
return true;
}
}
return false;
}

Node findNode(String label){
label = label.toLowerCase();
Node n = (Node) nodeTable.get(label);
if(n == null){
return addNode(label);
}
return n;
}

Node addNode(String label){
Node n = new Node(label);
if(nodeCount == nodes.length){
nodes = (Node[]) expand(nodes);
}
nodeTable.put(label, n);
nodes[nodeCount++] = n;
return n;
}


void draw(){
background(2,184,206);

for(int i =0; i < edgeCount; i++){
edges[i].relax();
}
for(int i = 0; i < nodeCount; i++){
nodes[i].relax();
}
for(int i = 0; i < nodeCount; i++){
nodes[i].update();
}
for(int i = 0; i < edgeCount; i++){
edges[i].draw();
}
for(int i =0; i < nodeCount; i++){
nodes[i].draw();
}
}

Node selection;

void mousePressed(){
float closest = 20;
for(int i =0; i < nodeCount; i++){
Node n = nodes[i];
float d = dist(mouseX,mouseY,n.x,n.y);
if(d < closest){
selection = n;
closest = d;
}
}
if(selection !=null){
if(mouseButton == LEFT){
selection.fixed = true;
}
else if(mouseButton == RIGHT){
selection.fixed = false;
}
}
}


void mouseDragged(){
if(selection != null){
selection.x = mouseX;
selection.y = mouseY;
}
}

void mouseReleased(){
selection = null;
}

void keyPressed(){
save("88.jpg");
}

コメント