Loading...
Logo
Processing Forum
i have  text file and i want save the word and any punctuation associated with it and in the same file i want to remove words that are common and then keep the same formatting for the text by saving it in text file.

Copy code
  1. WordAnalysis analysis;
  2. }
  3.  void setup()
  4. {
  5.   analysis = new WordAnalysis();
  6.    String[] lines = loadStrings("input.txt");
  7.    String allText = join(lines, " ");
  8.   String[] wordList = split(allText, " ");
  9.  
  10.   for (int i = 1; i < wordList.length; i++)
  11.    {
  12.       println(wordList[i]);
  13. }

Replies(50)

For the record, this is the follow-up of the thread https://forum.processing.org/topic/using-savestrings
Which went far from the subject, so I suggested to create a new thread.

As I wrote there, it would be more comfortable if I know you fully understood the code I gave in:
http://bazaar.launchpad.net/~philho/+junk/Processing/view/head:/_QuickExperiments/_Java/_Collections/CountingWords/
Even if it is a bit too much for your needs, it offers so techniques that can be useful for you.
If you have not understood what is a HashMap, a HashSet, an ArrayList, it will be hard to do what you want to do here.
So don't hesitate to ask questions.

I can spoon-feed you with ready-to-use code to solve your problem here, but it would be more useful for everybody if you participate in the process.

For starters, you probably need to feed your WordAnaysis class (to be defined?) with your wordList. It should feed a Word class with each element, and this class should split the string between a word and the punctuation before and after it. Then WordAnaysis can remove the common words (technique shown in my code) and save the result with saveStrings() or PrintWriter.
Just for fun, I added my own version to decimate stop words from a text, in the sketch linked above, but I am not sure if you will benefit from it.
Thanks so much Philho about your time and your answering let me understand the link and then i will ask you 
thanks for your support 
I used it before thanks  Philho, but now my problem how to remove common words from text and keep the same format for text and save it in text file for example: i have this text 
What does this tell us?  Not much?  No!  The Count's child thought
see nothing, therefore he speak so free.  Your man thought see
nothing.  My man thought see nothing, till just now.
when we remove common word become look like this 
does this tell ?  much?  !  The Count's child thought
see nothing, speak free.  man thought see
nothing.  man thought see nothing, till just now.
text with same format but without common words because i want to visualize this file
what do you mean by format?
To have empty space instead of the deleted words?

Like this:

   does this tell ?     much?  !  The Count's child thought
see nothing,       speak free.     man thought see
nothing.    man thought see nothing, till just now.

I mean the same sentences without common words and remove spaces (place common words that we remove) but sentences same with ,or . (  any punctuation or spaces associated with words have be same )
what do you mean by

place common words that we remove


why you do not understand me i can explain for you
what do you mean by

place common words that we remove
empty space instead of the deleted words
yes.
thank you.

show your code.

This is code read text file and remove common words from it but output not the same format that i need 
i want to save output in text file but as i explain above
Copy code
  1. String[] tokens;  // Array of all words from input file

  2. String[] ignore = {  "only", "to", "For example", "can", "for", "of", "is", "as", "a", "the", "that", "an", "the", "i", "it",                                "you", "and", "to", "in", "on", "you", "they", "for", "from", "to", "which",};

  3. boolean ignoreWord(String what) {
  4.   for (int i = 0; i < ignore.length; i++) {
  5.     if (what.toLowerCase().equals(ignore[i])) {
  6.       return true;
  7.     }
  8.   }
  9.  
  10.   return false;
  11. }

  12. void setup() {
  13.   String[] output = new String[0];
  14.   // Load file 
  15.   String[] lines = loadStrings("input.txt");
  16.   for(int i = 0; i < lines.length; i ++) {
  17.    
  18.     if (ignoreWord(lines[i]))
  19.     {
  20.       String A = lines[i];
  21.       A = A.replace(lines[i], "");
  22.       println(A);
  23.       String[] before = subset(tokens, 0, i);
  24.       String[] after = subset(tokens, min(i + 1, tokens.length), tokens.length - 1 - i);
  25.       tokens = concat(before, after);
  26.       i --;
  27.       output = append(output, tokens[i]);
  28.     }
  29.   }
  30.  
  31.   saveStrings("result.txt", output);
  32. }
thank you.

does this code deliver this
does this tell ?  much?  !  The Count's child thought
see nothing, speak free.  man thought see
nothing.  man thought see nothing, till just now.

?



No input file text file included another words
you can save this in text file
This part will start you thinking about designing and analyzing algorithms. It is intended to be a gentle introduction to how we specify algorithms, some of the design strategies we will use throughout this book, and many of the fundamental
ideas used in algorithm analysis. Later parts of this book will build upon this base.Chapter 1 provides an overview of algorithms and their place in modern computing systems. This chapter defines what an algorithm is and lists some examples.
It also makes a case that we should consider algorithms as a technology, alongside technologies such as fast hardware, graphical user interfaces, object-oriented systems, and network
yes.

does this code work at least half way?

does it give you

does this tell ?  much?  !  The Count's child thought
see nothing, speak free.  man thought see
nothing.  man thought see nothing, till just now.



your ignore list is incorrect

it's missing
  • therefore
  • your
  • what
etc.

No wonder it doesn't work.





It is an easy task

I can only quote phi.lho:


I can spoon-feed you with ready-to-use code to solve your problem here, but it would be more useful for everybody if you participate in the process.

so check your ignore word list.

Then read your code.
Line by line.
What's happening?

Where does it go wrong and why?

I will add any words i want to remove to ignore list this is not problem.but how i can remove common words and keep the same format for sentences as i explained.
you check it now line by line. That is good.
But you also need to check it word by word inside of that.
You have not tried to run my new code, have you? You need the three files and the text file in the data folder.
I updated the old sketch with the new request, but I kept the first function, countWords, that runs before the second function, decimateWords, which does what you want. You can see the result in the outputDecimate.txt file.
But I used some hard-core stuff to reach my goal faster, namely the regular expression splitting the chunk of text between two spaces between punctuation sign before and after it, and the word itself.
It can be done the old way (charAt() for example), but with 50 lines where I had 14...
woow it is amazing Thanks so so so much PhiLho it is works very good 
Now i want to visualize  outputDecimate  file but how I will explain to you I need to draw directed graph .nodes it will be the words in the sentences and edges it will represent terms in the same sentence.if we have word before in different sentence just draw edge by different color from that sentence.
for example (this is what outputDecimate  file)
" Computer science, hash table hash map data structure uses hash function  . Map identifying values, known   keys , associated values .   Hash table implements  associative array. Hash function used transform key index array element .
  "

the graph for this file it will be 

This is the code:
Copy code
  1. int nodeCount;
  2. Node[] nodes = new Node[100];
  3. HashMap nodeTable = new HashMap();

  4. int edgeCount;
  5. Edge[] edges = new Edge[500];

  6. static final color nodeColor   = #F0C070;
  7. static final color selectColor = #FF3030;
  8. static final color fixedColor  = #FF8080;
  9. static final color edgeColor   = #000000;

  10. PFont font;


  11. void setup() {
  12.   size(600, 600);  
  13.   loadData();
  14.   font = createFont("SansSerif", 10);
  15.   writeData();  
  16. }


  17. void writeData() {
  18.   PrintWriter writer = createWriter("output.dot");
  19.   writer.println("digraph output {");
  20.   for (int i = 0; i < edgeCount; i++) {
  21.     String from = """ + edges[i].from.label + """;
  22.     String to = """ + edges[i].to.label + """;
  23.     writer.println(TAB + from + " -> " + to + ";");
  24.   }
  25.   writer.println("}");
  26.   writer.flush();
  27.   writer.close();
  28. }


  29. void loadData() {
  30.   String[] lines = loadStrings("input.txt");
  31.   
  32.   // Make the text into a single String object
  33.   String line = join(lines, " ");
  34.   
  35.   // Replace -- with an actual em dash
  36.   line = line.replaceAll("--", "—");
  37.   
  38.   // Split into phrases using any of the provided tokens
  39.   String[] phrases = splitTokens(line, ".,;:?!—"");
  40.   //println(phrases);

  41.   for (int i = 0; i < phrases.length; i++) {
  42.     // Make this phrase lowercase
  43.     String phrase = phrases[i].toLowerCase();
  44.     // Split each phrase into individual words at one or more spaces
  45.     String[] words = splitTokens(phrase, " ");
  46.     for (int w = 0; w < words.length-1; w++)
  47.     {
  48.       addEdge(words[w], words[w+1]);
  49.     }
  50.   }
  51. }


  52. void addEdge(String fromLabel, String toLabel) {
  53.   Node from = findNode(fromLabel);
  54.   Node to = findNode(toLabel);
  55.   from.increment();
  56.   to.increment();
  57.   
  58.   for (int i = 0; i < edgeCount; i++) {
  59.     if (edges[i].from == from && edges[i].to == to) {
  60.       edges[i].increment();
  61.       return;
  62.     }
  63.   } 
  64.   
  65.   Edge e = new Edge(from, to);
  66.   e.increment();
  67.   if (edgeCount == edges.length) {
  68.     edges = (Edge[]) expand(edges);
  69.   }
  70.   edges[edgeCount++] = e;
  71. }

  72. Node findNode(String label) {
  73.   label = label.toLowerCase();
  74.   Node n = (Node) nodeTable.get(label);
  75.   if (n == null) {
  76.     return addNode(label);
  77.   }
  78.   return n;
  79. }


  80. Node addNode(String label) {
  81.   Node n = new Node(label);  
  82.   if (nodeCount == nodes.length) {
  83.     nodes = (Node[]) expand(nodes);
  84.   }
  85.   nodeTable.put(label, n);
  86.   nodes[nodeCount++] = n;  
  87.   return n;
  88. }


  89. void draw() {
  90.   if (record) {
  91.     beginRecord(PDF, "output.pdf");
  92.   }

  93.   background(255);
  94.   textFont(font);  
  95.   smooth();  
  96.   
  97.   for (int i = 0 ; i < edgeCount ; i++) {
  98.     edges[i].relax();
  99.   }
  100.   for (int i = 0; i < nodeCount; i++) {
  101.     nodes[i].relax();
  102.   }
  103.   for (int i = 0; i < nodeCount; i++) {
  104.     nodes[i].update();
  105.   }
  106.   for (int i = 0 ; i < edgeCount ; i++) {
  107.     edges[i].draw();
  108.   }
  109.   for (int i = 0 ; i < nodeCount ; i++) {
  110.     nodes[i].draw();
  111.   }
  112.   
  113.   if (record) {
  114.     endRecord();
  115.     record = false;
  116.   }
  117. }


  118. boolean record;

  119. void keyPressed() {
  120.   if (key == 'r') {
  121.     record = true;
  122.   }
  123. }


  124. Node selection; 
  125. void mousePressed() {
  126.   // Ignore anything greater than this distance
  127.   float closest = 20;
  128.   for (int i = 0; i < nodeCount; i++) {
  129.     Node n = nodes[i];
  130.     float d = dist(mouseX, mouseY, n.x, n.y);
  131.     if (d < closest) {
  132.       selection = n;
  133.       closest = d;
  134.     }
  135.   }
  136.   if (selection != null) {
  137.     if (mouseButton == LEFT) {
  138.       selection.fixed = true;
  139.     } else if (mouseButton == RIGHT) {
  140.       selection.fixed = false;
  141.     }
  142.   }
  143. }


  144. void mouseDragged() {
  145.   if (selection != null) {
  146.     selection.x = mouseX;
  147.     selection.y = mouseY;
  148.   }
  149. }


  150. void mouseReleased() {
  151.   selection = null;
  152. }
  153. class Edge {
  154.   Node from;
  155.   Node to;
  156.   float len;
  157.   int count;


  158.   Edge(Node from, Node to) {
  159.     this.from = from;
  160.     this.to = to;
  161.     this.len = 50;
  162.   }
  163.   
  164.   
  165.   void increment() {
  166.     count++;
  167.   }
  168.   
  169.   void relax() {
  170.     float vx = to.x - from.x;
  171.     float vy = to.y - from.y;
  172.     float d = mag(vx, vy);
  173.     if (d > 0) {
  174.       float f = (len - d) / (d * 3);
  175.       float dx = f * vx;
  176.       float dy = f * vy;
  177.       to.dx += dx;
  178.       to.dy += dy;
  179.       from.dx -= dx;
  180.       from.dy -= dy;
  181.     }
  182.   }


  183.   void draw() {
  184.     stroke(edgeColor);
  185.     strokeWeight(0.35);
  186.     line(from.x, from.y, to.x, to.y);
  187.   }
  188. }
  189. class Node {
  190.   float x, y;
  191.   float dx, dy;
  192.   boolean fixed;
  193.   String label;
  194.   int count;


  195.   Node(String label) {
  196.     this.label = label;
  197.     x = random(width);
  198.     y = random(height);
  199.   }
  200.   
  201.   
  202.   void increment() {
  203.     count++;
  204.   }
  205.   
  206.   
  207.   void relax() {
  208.     float ddx = 0;
  209.     float ddy = 0;

  210.     for (int j = 0; j < nodeCount; j++) {
  211.       Node n = nodes[j];
  212.       if (n != this) {
  213.         float vx = x - n.x;
  214.         float vy = y - n.y;
  215.         float lensq = vx * vx + vy * vy;
  216.         if (lensq == 0) {
  217.           ddx += random(1);
  218.           ddy += random(1);
  219.         } else if (lensq < 100*100) {
  220.           ddx += vx / lensq;
  221.           ddy += vy / lensq;
  222.         }
  223.       }
  224.     }
  225.     float dlen = mag(ddx, ddy) / 2;
  226.     if (dlen > 0) {
  227.       dx += ddx / dlen;
  228.       dy += ddy / dlen;
  229.     }
  230.   }


  231.   void update() {
  232.     if (!fixed) {      
  233.       x += constrain(dx, -5, 5);
  234.       y += constrain(dy, -5, 5);
  235.       
  236.       x = constrain(x, 0, width);
  237.       y = constrain(y, 0, height);
  238.     }
  239.     dx /= 2;
  240.     dy /= 2;
  241.   }


  242.   void draw() {
  243.     fill(nodeColor);
  244.     stroke(0);
  245.     strokeWeight(10);
  246.     
  247.     ellipse(x, y, count, count);
  248.     float w = textWidth(label);

  249.     if (count > w+2) {
  250.       fill(0);
  251.       textAlign(CENTER, CENTER);
  252.       text(label, x, y);
  253.     }
  254.   }
  255. }
help please....If anyone can answer me 
how i can do that ......please share by any idea

Not much time right now, and since you already have some code, I don't know what is the problem.
Don't expect us to do all your assignment for you. Tell us where you are stuck, we will help you.
The problem how i can draw node for every word and i want to add edges between every word and next to it and if we already have this node just add edge by different color don't draw node for this word again how i can add this to my code.
If you don't understand i will explain.
Isn't the code you show already doing that? (I haven't tried it.) If not, why are you showing it? If yes, why aren't you using it?
Thanks Philho I don't know how i can add edges between words that already have nodes it depends on the word in the same sentence and different sentence as i explained before try to run the program maybe you understand me 
I add some modification for this code but doesn't work good for what i want
Copy code
  1.   for (int i = 0; i < phrases.length; i++) {
  2.     // Make this phrase lowercase
  3.     String phrase = phrases[i].toLowerCase();
  4.     // Split each phrase into individual words at one or more spaces
  5.     String[] words = splitTokens(phrase, " ");
  6.     for (int w = 0; w < words.length-1; w++) {
  7.      if(words[w]==words[w+1])

  8.      addEdge(words[w-1], words[w]);

  9.      else{
  10.       addEdge(words[w], words[w+1]);
      and i need to write the word inside the node i did that but doesn't appear when i run the program
Copy code

How I can draw edges from node that already have frequent word ...any help 
where your help you never be late  PhiLho How i can draw edges from nodes that already have if we have this word before and remove all extra edges this not clean in my code.

It surprises me that you keep coming up with problems that are way over you head and expect the forum (or even PhiLho in person) to help you.

Why not start with something easy for a change?

Nobody is obliged to help you. 



try this

Copy code
  1. int nodeCount;
  2. Node[] nodes = new Node[100];
  3. HashMap nodeTable = new HashMap();
  4. int edgeCount;
  5. Edge[] edges = new Edge[500];
  6. static final color nodeColor   = #F0C070;
  7. static final color selectColor = #FF3030;
  8. static final color fixedColor  = #FF8080;
  9. static final color edgeColor   = #000000;
  10. PFont font;
  11. void setup() {
  12.   size(600, 600); 
  13.   loadData();
  14.   font = createFont("SansSerif", 10);
  15.   writeData();
  16. }
  17. void writeData() {
  18.   PrintWriter writer = createWriter("output.dot");
  19.   writer.println("digraph output {");
  20.   for (int i = 0; i < edgeCount; i++) {
  21.     String from = "\"" + edges[i].from.label + "\"";
  22.     String to = "\"" + edges[i].to.label + "\"";
  23.     writer.println(TAB + from + " -> " + to + ";");
  24.   }
  25.   writer.println("}");
  26.   writer.flush();
  27.   writer.close();
  28. }
  29. void loadData() {
  30.   // change this if you have a file !!!!!!!!
  31.   //String[] lines = loadStrings("input.txt");
  32.   String[] lines =
  33.   {
  34.     "Computer science, hash table hash map data structure uses hash function. ",
  35.     "Map identifying values, known  keys , associated values. ",
  36.     "Hash table implements associative array. Hash function used transform key index array element. "
  37.   };
  38.   // Make the text into a single String object
  39.   String line = join(lines, " ");
  40.   // Replace -- with an actual em dash
  41.   line = line.replaceAll("--", "—");
  42.   // Split into phrases using any of the provided tokens
  43.   String[] phrases = splitTokens(line, ".,;:?!—\"");
  44.   //println(phrases);
  45.   for (int i = 0; i < phrases.length; i++) {
  46.     // Make this phrase lowercase
  47.     String phrase = phrases[i].toLowerCase();
  48.     // Split each phrase into individual words at one or more spaces
  49.     String[] words = splitTokens(phrase, " ");
  50.     for (int w = 0; w < words.length-1; w++)
  51.     {
  52.       addEdge(words[w], words[w+1]);
  53.     }
  54.   } // for
  55. } // func
  56. void addEdge(String fromLabel, String toLabel) {
  57.   Node from = findNode(fromLabel);
  58.   Node to = findNode(toLabel);
  59.   from.increment();
  60.   to.increment();
  61.   //
  62.   for (int i = 0; i < edgeCount; i++) {
  63.     if (edges[i].from == from && edges[i].to == to) {
  64.       edges[i].increment();
  65.       return;
  66.     }
  67.   }
  68.   //
  69.   Edge e = new Edge(from, to);
  70.   e.increment();
  71.   if (edgeCount == edges.length) {
  72.     edges = (Edge[]) expand(edges);
  73.   }
  74.   edges[edgeCount++] = e;
  75. } // func
  76. Node findNode(String label) {
  77.   label = label.toLowerCase();
  78.   Node n = (Node) nodeTable.get(label);
  79.   if (n == null) {
  80.     return addNode(label);
  81.   }
  82.   return n;
  83. } // func
  84. Node addNode(String label) {
  85.   Node n = new Node(label); 
  86.   if (nodeCount == nodes.length) {
  87.     nodes = (Node[]) expand(nodes);
  88.   }
  89.   nodeTable.put(label, n);
  90.   nodes[nodeCount++] = n; 
  91.   return n;
  92. } // func
  93. void draw() {
  94.   if (record) {
  95.     beginRecord(PDF, "output.pdf");
  96.   }
  97.   background(255);
  98.   textFont(font); 
  99.   smooth(); 
  100.   for (int i = 0 ; i < edgeCount ; i++) {
  101.     edges[i].relax();
  102.   }
  103.   for (int i = 0; i < nodeCount; i++) {
  104.     nodes[i].relax();
  105.   }
  106.   for (int i = 0; i < nodeCount; i++) {
  107.     nodes[i].update();
  108.   }
  109.   for (int i = 0 ; i < edgeCount ; i++) {
  110.     edges[i].draw();
  111.   }
  112.   for (int i = 0 ; i < nodeCount ; i++) {
  113.     nodes[i].draw();
  114.   }
  115.   if (record) {
  116.     endRecord();
  117.     record = false;
  118.   }
  119. } // func
  120. boolean record;
  121. void keyPressed() {
  122.   if (key == 'r') {
  123.     record = true;
  124.   }
  125. }
  126. Node selection;
  127. //
  128. void mousePressed() {
  129.   // Ignore anything greater than this distance
  130.   float closest = 20;
  131.   for (int i = 0; i < nodeCount; i++) {
  132.     Node n = nodes[i];
  133.     float d = dist(mouseX, mouseY, n.x, n.y);
  134.     if (d < closest) {
  135.       selection = n;
  136.       closest = d;
  137.     }
  138.   }
  139.   if (selection != null) {
  140.     if (mouseButton == LEFT) {
  141.       selection.fixed = true;
  142.     }
  143.     else if (mouseButton == RIGHT) {
  144.       selection.fixed = false;
  145.     }
  146.   }
  147. } // func
  148. void mouseDragged() {
  149.   if (selection != null) {
  150.     selection.x = mouseX;
  151.     selection.y = mouseY;
  152.   }
  153. }
  154. void mouseReleased() {
  155.   selection = null;
  156. }
  157. //
  158. // ==================================================
  159. //
  160. class Edge {
  161.   Node from;
  162.   Node to;
  163.   float len;
  164.   int count;
  165.   Edge(Node from, Node to) {
  166.     this.from = from;
  167.     this.to = to;
  168.     this.len = 50;
  169.   }
  170.   void increment() {
  171.     count++;
  172.   }
  173.   void relax() {
  174.     float vx = to.x - from.x;
  175.     float vy = to.y - from.y;
  176.     float d = mag(vx, vy);
  177.     if (d > 0) {
  178.       float f = (len - d) / (d * 3);
  179.       float dx = f * vx;
  180.       float dy = f * vy;
  181.       to.dx += dx;
  182.       to.dy += dy;
  183.       from.dx -= dx;
  184.       from.dy -= dy;
  185.     }
  186.   }
  187.   void draw() {
  188.     stroke(edgeColor);
  189.     strokeWeight(0.35);
  190.     line(from.x, from.y, to.x, to.y);
  191.   } // func
  192. } // class
  193. //
  194. //
  195. class Node {
  196.   float x, y;
  197.   float dx, dy;
  198.   boolean fixed;
  199.   String label;
  200.   int count;
  201.   Node(String _label) {
  202.     label = _label;
  203.     x = random(width);
  204.     y = random(height);
  205.   }
  206.   void increment() {
  207.     count++;
  208.   }
  209.   void relax() {
  210.     float ddx = 0;
  211.     float ddy = 0;
  212.     for (int j = 0; j < nodeCount; j++) {
  213.       Node n = nodes[j];
  214.       if (n != this) {
  215.         float vx = x - n.x;
  216.         float vy = y - n.y;
  217.         float lensq = vx * vx + vy * vy;
  218.         if (lensq == 0) {
  219.           ddx += random(1);
  220.           ddy += random(1);
  221.         }
  222.         else if (lensq < 100*100) {
  223.           ddx += vx / lensq;
  224.           ddy += vy / lensq;
  225.         }
  226.       }
  227.     }
  228.     float dlen = mag(ddx, ddy) / 2;
  229.     if (dlen > 0) {
  230.       dx += ddx / dlen;
  231.       dy += ddy / dlen;
  232.     }
  233.   } // func
  234.   void update() {
  235.     if (!fixed) {     
  236.       x += constrain(dx, -5, 5);
  237.       y += constrain(dy, -5, 5);
  238.       x = constrain(x, 0, width);
  239.       y = constrain(y, 0, height);
  240.     }
  241.     dx /= 2;
  242.     dy /= 2;
  243.   }
  244.   void draw() {
  245.     fill(nodeColor);
  246.     stroke(0);
  247.     strokeWeight(10);
  248.     ellipse(x, y, count, count);
  249.     float w = textWidth(label);
  250.     textSize((count+1)*7);
  251.     //if (count > w+2) {
  252.     fill(111);
  253.     textAlign(CENTER, CENTER);
  254.     // println(label);
  255.     text(label, x, y);
  256.     //}
  257.   } // func
  258.   //
  259. } // class
  260. //
I am new with using processing software that is way I am not familiar with message error that i have when i run the program 
that is why i always asking I am so sorry but when the other help me especially  PhiLho  help me to know things i do not now it before 
Hey, if you learned something out of my code, it is my pleasure.
Thanks, Chrisir, for helping, as I have no time in the week days to work on complex things (I mostly do short answers outside of week-ends...).
Thanks so much  Chrisir but why  Computer science is Sprite from the sentence 
 "Computer science, hash table hash map data structure uses hash function. ",
the end for any sentence ( .) but not( ,) I think this is only otherwise works good 



 

this...



Copy code
  1. int nodeCount;
  2. Node[] nodes = new Node[100];
  3. HashMap nodeTable = new HashMap();
  4. int edgeCount;
  5. Edge[] edges = new Edge[500];
  6. static final color nodeColor   = #F0C070;
  7. static final color selectColor = #FF3030;
  8. static final color fixedColor  = #FF8080;
  9. static final color edgeColor   = #000000;
  10. PFont font;
  11. void setup() {
  12.   size(600, 600); 
  13.   loadData();
  14.   font = createFont("SansSerif", 10);
  15.   writeData();
  16. }
  17. void writeData() {
  18.   PrintWriter writer = createWriter("output.dot");
  19.   writer.println("digraph output {");
  20.   for (int i = 0; i < edgeCount; i++) {
  21.     String from = "\"" + edges[i].from.label + "\"";
  22.     String to = "\"" + edges[i].to.label + "\"";
  23.     writer.println(TAB + from + " -> " + to + ";");
  24.   }
  25.   writer.println("}");
  26.   writer.flush();
  27.   writer.close();
  28. }
  29. void loadData() {
  30.   // change this if you have a file !!!!!!!!
  31.   //String[] lines = loadStrings("input.txt");
  32.   String[] lines =
  33.   {
  34.     "Computer science, hash table hash map data structure uses hash function. ",
  35.     "Map identifying values, known  keys , associated values. ",
  36.     "Hash table implements associative array. Hash function used transform key index array element. "
  37.   };
  38.   // Make the text into a single String object
  39.   String line = join(lines, " ");
  40.   // Replace -- with an actual em dash
  41.   line = line.replaceAll("--", "—");
  42.   // Split into phrases using any of the provided tokens
  43.   String[] phrases = splitTokens(line, ".,;:?!—\"");
  44.   //println(phrases);
  45.   String result = "";
  46.   for (int i = 0; i < phrases.length; i++) {
  47.     // Make this phrase lowercase
  48.     String phrase = phrases[i].toLowerCase();
  49.     phrase = trim(phrase);
  50.     result += phrase + " ";
  51.   }
  52.   //
  53.   println("result: " + result);
  54.   // Split each phrase into individual words at one or more spaces
  55.   String[] words = splitTokens(result, " ");
  56.   // println(words);
  57.   for (int w = 0; w < words.length-1; w++)
  58.   {
  59.     addEdge(words[w], words[w+1]);
  60.   } // for
  61. } // func
  62. void addEdge(String fromLabel, String toLabel) {
  63.   Node from = findNode(fromLabel);
  64.   Node to = findNode(toLabel);
  65.   from.increment();
  66.   to.increment();
  67.   //
  68.   for (int i = 0; i < edgeCount; i++) {
  69.     if (edges[i].from == from && edges[i].to == to) {
  70.       edges[i].increment();
  71.       return;
  72.     }
  73.   }
  74.   //
  75.   Edge e = new Edge(from, to);
  76.   e.increment();
  77.   if (edgeCount == edges.length) {
  78.     edges = (Edge[]) expand(edges);
  79.   }
  80.   edges[edgeCount++] = e;
  81. } // func
  82. Node findNode(String label) {
  83.   label = label.toLowerCase();
  84.   Node n = (Node) nodeTable.get(label);
  85.   if (n == null) {
  86.     return addNode(label);
  87.   }
  88.   return n;
  89. } // func
  90. Node addNode(String label) {
  91.   Node n = new Node(label); 
  92.   if (nodeCount == nodes.length) {
  93.     nodes = (Node[]) expand(nodes);
  94.   }
  95.   nodeTable.put(label, n);
  96.   nodes[nodeCount++] = n; 
  97.   return n;
  98. } // func
  99. void draw() {
  100.   if (record) {
  101.     beginRecord(PDF, "output.pdf");
  102.   }
  103.   background(255);
  104.   textFont(font); 
  105.   smooth(); 
  106.   for (int i = 0 ; i < edgeCount ; i++) {
  107.     edges[i].relax();
  108.   }
  109.   for (int i = 0; i < nodeCount; i++) {
  110.     nodes[i].relax();
  111.   }
  112.   for (int i = 0; i < nodeCount; i++) {
  113.     nodes[i].update();
  114.   }
  115.   for (int i = 0 ; i < edgeCount ; i++) {
  116.     edges[i].draw();
  117.   }
  118.   for (int i = 0 ; i < nodeCount ; i++) {
  119.     nodes[i].draw();
  120.   }
  121.   if (record) {
  122.     endRecord();
  123.     record = false;
  124.   }
  125. } // func
  126. boolean record;
  127. void keyPressed() {
  128.   if (key == 'r') {
  129.     record = true;
  130.   }
  131. }
  132. Node selection;
  133. //
  134. void mousePressed() {
  135.   // Ignore anything greater than this distance
  136.   float closest = 20;
  137.   for (int i = 0; i < nodeCount; i++) {
  138.     Node n = nodes[i];
  139.     float d = dist(mouseX, mouseY, n.x, n.y);
  140.     if (d < closest) {
  141.       selection = n;
  142.       closest = d;
  143.     }
  144.   }
  145.   if (selection != null) {
  146.     if (mouseButton == LEFT) {
  147.       selection.fixed = true;
  148.     }
  149.     else if (mouseButton == RIGHT) {
  150.       selection.fixed = false;
  151.     }
  152.   }
  153. } // func
  154. void mouseDragged() {
  155.   if (selection != null) {
  156.     selection.x = mouseX;
  157.     selection.y = mouseY;
  158.   }
  159. }
  160. void mouseReleased() {
  161.   selection = null;
  162. }
  163. //
  164. // ==================================================
  165. //
  166. class Edge {
  167.   Node from;
  168.   Node to;
  169.   float len;
  170.   int count;
  171.   Edge(Node from, Node to) {
  172.     this.from = from;
  173.     this.to = to;
  174.     this.len = 50;
  175.   }
  176.   void increment() {
  177.     count++;
  178.   }
  179.   void relax() {
  180.     float vx = to.x - from.x;
  181.     float vy = to.y - from.y;
  182.     float d = mag(vx, vy);
  183.     if (d > 0) {
  184.       float f = (len - d) / (d * 3);
  185.       float dx = f * vx;
  186.       float dy = f * vy;
  187.       to.dx += dx;
  188.       to.dy += dy;
  189.       from.dx -= dx;
  190.       from.dy -= dy;
  191.     }
  192.   }
  193.   void draw() {
  194.     stroke(edgeColor);
  195.     strokeWeight(0.35);
  196.     line(from.x, from.y, to.x, to.y);
  197.   } // func
  198. } // class
  199. //
  200. //
  201. class Node {
  202.   float x, y;
  203.   float dx, dy;
  204.   boolean fixed;
  205.   String label;
  206.   int count;
  207.   Node(String _label) {
  208.     label = _label;
  209.     x = random(width);
  210.     y = random(height);
  211.   }
  212.   void increment() {
  213.     count++;
  214.   }
  215.   void relax() {
  216.     float ddx = 0;
  217.     float ddy = 0;
  218.     for (int j = 0; j < nodeCount; j++) {
  219.       Node n = nodes[j];
  220.       if (n != this) {
  221.         float vx = x - n.x;
  222.         float vy = y - n.y;
  223.         float lensq = vx * vx + vy * vy;
  224.         if (lensq == 0) {
  225.           ddx += random(1);
  226.           ddy += random(1);
  227.         }
  228.         else if (lensq < 100*100) {
  229.           ddx += vx / lensq;
  230.           ddy += vy / lensq;
  231.         }
  232.       }
  233.     }
  234.     float dlen = mag(ddx, ddy) / 2;
  235.     if (dlen > 0) {
  236.       dx += ddx / dlen;
  237.       dy += ddy / dlen;
  238.     }
  239.   } // func
  240.   void update() {
  241.     if (!fixed) {     
  242.       x += constrain(dx, -5, 5);
  243.       y += constrain(dy, -5, 5);
  244.       x = constrain(x, 0, width);
  245.       y = constrain(y, 0, height);
  246.     }
  247.     dx /= 2;
  248.     dy /= 2;
  249.   }
  250.   void draw() {
  251.     fill(nodeColor);
  252.     stroke(0);
  253.     strokeWeight(10);
  254.     ellipse(x, y, count, count);
  255.     float w = textWidth(label);
  256.     textSize((count+1)*7);
  257.     //if (count > w+2) {
  258.     fill(111);
  259.     textAlign(CENTER, CENTER);
  260.     // println(label);
  261.     text(label, x, y);
  262.     //}
  263.   } // func
  264.   //
  265. } // class
  266. //

Thanks a lot for your helping It works as I want thank you again 
hiiii....I am sorry about keeping asking but I found the programming good and I am thinking about different things
If I need to output common words in output text file like count words.
Copy code
  1. void common(String text)
  2. {
  3.   WordList wordList = new WordList();
  4.    
  5.  String[] stopWordList = { "many", "require", "a", "that", "only", "the", "and", "For", "example","in", "which,",
  6.   "of", "are", "to", "for", "Although", "can", "take", "as long as", "well", "or", "our", "me", "my", "no", "not", "on",
  7.   "she", "so", "that", "the", "there", "they", "this", "to", "too", "you", "your"
  8. }; 

  9.   for (String word : stopWordList)
  10.   {
  11.     wordList.add(word);
  12.   }

  13.   // Print out the words and their frequency
  14.   wordList.print();
  15.   // Output to disk
  16.   wordList.export("commonwords.txt");
  17. }
The problem I found text file without nothing it is empty
Can I find some help,,,please I am sorry but I want to know where is the problem

what is the problem?

The program doesn't run when "input.txt" is empty?




I suppose you refer to my code in Launchpad.
If you look at the WordList code, you can see:
Copy code
  1.   // Add a word
  2.   void add(String w)
  3.   {
  4.     if (w.length() < 2)
  5.       return; // Exclude 0 or 1 sized words
  6.     if (stopWords.contains(w))
  7.       return; // Exclude known stop words
  8. // etc.
So this class is designed to automatically (and silently) eliminate stopwords. But your code add only these words! So the word list remains empty...
You can remove the two tests at the start of this method to be able to add any kind of word.
PhiLho this is part from your code but I want to modify some lines because I want to save   stopWordList  to text file like outputDecimate
do you then want to use the stopWordList from this text file (load it) instead of having it as text in your code?
How  I can load it in the text file


Copy code
  1.   WordList wordList = new WordList();
  2.    
  3.  String[] stopWordList = { "many", "require", "a", "that", "only", "the", "and", "For", "example","in", "which,",
  4.   "of", "are", "to", "for", "Although", "can", "take", "as long as", "well", "or", "our", "me", "my", "no", "not", "on",
  5.   "she", "so", "that", "the", "there", "they", "this", "to", "too", "you", "your"
  6. };

then use

saveString

sorry ...I use this but doesn't work 
Copy code
  1. String [] stopWordList = { "many", "require", "a", "that", "only", "the", "and", "For", "example","in", "which,",   "of", "are", "to", "for", "Although", "can", "take", "as long as", "well", "or", "our", "me", "my", "no", "not", "on",   "she", "so", "that", "the", "there", "they", "this", "to", "too", "you", "your"
    }; 

    saveStrings("commonwords.txt", stopWordList);
It is works good thanks so so so much
How could it be prevented that the nodes overlap? What's the best way to keep, let's say, 10px distance.

Here's the code. I added  

if (frameCount % 900 == 0) {

      nodes[i].fixed = true;
    }

To make the nodes stop shaking after a while, but the best thing would be to make sure that all the nodes (also bigger ones) have at least 10px distance.

Copy code
  1. int nodeCount;
    Node[] nodes = new Node[100];
    HashMap nodeTable = new HashMap();
    int edgeCount;
    Edge[] edges = new Edge[100];
    static final color nodeColor   = #F0C070;
    static final color selectColor = #FF3030;
    static final color fixedColor  = #FF8080;
    static final color edgeColor   = #000000;
    PFont font;
    void setup() {
      size(1000, 800); 
      loadData();
      font = createFont("SansSerif", 10);
    }


    void loadData() {
      // change this if you have a file !!!!!!!!
      //String[] lines = loadStrings("input.txt");
      String[] lines =
      {
        "In de Syrische stad Aleppo, de tweede stad van het land, woedde vanmiddag voor de vierde dag op rij een hevige strijd tussen het Syrische regeringsleger en rebellen van de oppositie. Het Syrische regime meldde intussen dat het weer de volledige controle heeft over hoofdstad Damascus.  Aleppo is het economische hart van Syrië en was tot nu toe relatief rustig gedurende de zeventien maanden die de volksopstand tegen het regime van president Assad nu duurt. Er woont net als in Damascus een elite die geprofiteerd heeft van banden met het regime. Afgelopen weekend braken er hevige gevechten uit uit. De opstandelingen kondigden toen een operatie aan die tot doel heeft Aleppo te “bevrijden”. Volgens activisten van de oppositie werd er vandaag vooral gevochten in wijken aan de rand van de stad. De strijd zou nog niet zijn doorgedrongen tot het centrum van Aleppo. Het Syrische staatspersbureau SANA beweerde dat het leger de rebellen zware verliezen heeft toegebracht. Volgens inwoners heeft het leger beschietingen en bombardementen uitgevoerd op verschillende delen van de stad.  Niet alleen in Aleppo, maar ook in de rest van Syrië houdt het geweld aan. Het aan de oppositie gelieerde Syrisch Observatorium voor Mensenrechten schat dat bij gevechten in het land gisteren meer dan 116 mensen om het leven kwam. Aan het begin van de middag meldde de organisatie dat bij het geweld van vandaag al zeker 33 mensen waren gedood."
      };
      // Make the text into a single String object
      String line = join(lines, " ");
      // Replace -- with an actual em dash
      line = line.replaceAll("--", "—");
      // Split into phrases using any of the provided tokens
      String[] phrases = splitTokens(line, ".,;:?!—\"");
      //println(phrases);
      for (int i = 0; i < phrases.length; i++) {
        // Make this phrase lowercase
        String phrase = phrases[i].toLowerCase();
        // Split each phrase into individual words at one or more spaces
        String[] words = splitTokens(phrase, " ");
        for (int w = 0; w < words.length-1; w++)
        {
          addEdge(words[w], words[w+1]);
        }
      } // for
    } // func
    void addEdge(String fromLabel, String toLabel) {
      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;
    } // func
    Node findNode(String label) {
      label = label.toLowerCase();
      Node n = (Node) nodeTable.get(label);
      if (n == null) {
        return addNode(label);
      }
      return n;
    } // func
    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;
    } // func
    void draw() {


      background(255);
      textFont(font); 
      smooth(); 




      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();

        // stop after 900

        if (frameCount % 900 == 0) {

          nodes[i].fixed = true;
        }
      }
    } // func


    Node selection;
    //
    void mousePressed() {
      // Ignore anything greater than this distance
      float closest = 10;
      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;
        }
      }
    } // func
    void mouseDragged() {
      if (selection != null) {
        selection.x = mouseX;
        selection.y = mouseY;
      }
    }
    void mouseReleased() {
      selection = null;
    }
    //
    // ==================================================
    //
    class Edge {
      Node from;
      Node to;
      float len;
      int count;
      Edge(Node from, Node to) {
        this.from = from;
        this.to = to;
        this.len = 100;
      }
      void increment() {
        count++;
      }
      void relax() {
        float vx = to.x - from.x;
        float vy = to.y - from.y;
        float d = mag(vx, vy);
        if (d > 0) {
          float f = (len - d) / (d * 6);
          float dx = f * vx;
          float dy = f * vy;
          to.dx += dx;
          to.dy += dy;
          from.dx -= dx;
          from.dy -= dy;
        }
      }
      void draw() {
        stroke(edgeColor);
        strokeWeight(0.35);
        line(from.x, from.y, to.x, to.y);
      } // func
    } // class
    //
    //
    class Node {
      float x, y;
      float dx, dy;
      boolean fixed;
      String label;
      int count;
      Node(String _label) {
        label = _label;


        x = random(width-30);
        y = random(height-30);
      }
      void increment() {
        count++;
      }
      void relax() {
        float ddx = 0;
        float ddy = 0;
        for (int j = 0; j < nodeCount; j++) {
          Node n = nodes[j];


          if (n != this) {
            float vx = x - n.x;
            float vy = y - n.y;
            float lensq = vx * vx + vy * vy;
            if (lensq == 0) {
              ddx += random(1);
              ddy += random(1);
            }
            else if (lensq < 100*100) {
              ddx += vx / lensq;
              ddy += vy / lensq;
            }
          }
        }
        float dlen = mag(ddx, ddy) / 2;
        if (dlen > 0) {
          dx += ddx / dlen;
          dy += ddy / dlen;
        }
      } // func
      void update() {
        if (!fixed) {     
          x += constrain(dx, -5, 5);
          y += constrain(dy, -5, 5);
          x = constrain(x, 40, width-40);
          y = constrain(y, 40, height-40);
        }



        dx /= 2;
        dy /= 2;
      }
      void draw() {
        fill(nodeColor);
        stroke(0);
        strokeWeight(10);
        ellipse(x, y, count, count);
        float w = textWidth(label);


        fill(0);
        textAlign(LEFT, CENTER);
        // println(label);
        text(label, x+5, y);

        textSize(9);
        // textSize((count+1)*7);
        if (count > 5) {
          fill(0, 0, 250);
          textSize(20);


          textAlign(LEFT, CENTER);
          // println(label);
          text(label, x+5, y);
        }
      } // func
      //
    } // class
    //



I don't understand the code
maybe one of the gurus can tell you........


wild guess:
search in your code:
Copy code
  1.       float f = (len - d) / (d * 6);

try 8 or 12 instead of 6

Greetings, Chrisir