set - Invert Concordance Using Java -


today i'm working client creates concordance text file using java. need invert concordance recreate text start finish. now, issue seem having start , how each step. of have tried create array of words , iterate through symbol table , assign each key array. end getting list of words concordance. reason problem makes me feel stupid because seems should simple solution. can't seem think of valid ideas me started recreating story. have included source here:

public class invertedconcordance {  public static st<string, set<integer>> createconcordance (string[] words) {     st<string, set<integer>> st = new st<string, set<integer>>();     (int = 0; < words.length; i++) {         string s = words[i];         if (!st.contains(s)) {             st.put(s, new set<integer>());         }         set<integer> set = st.get(s);         set.add(i);     }     return st; } public static string[] invertconcordance (st<string, set<integer>> st) {   //this have far //here have doesnt work for(string key : st.keys()) { inv[i++] = key; } for(int z = 0; z< inv.length; z++) { system.out.println(inv[z]); }    string[]inv = new string[st.size()];      return inv; } private static void savewords (string filename, string[] words) {     int max_length = 70;     out out = new out (filename);     int length = 0;     (string word : words) {         length += word.length ();         if (length > max_length) {             out.println ();             length = word.length ();         }         out.print (word);         out.print (" ");         length++;     }     out.close (); } public static void main(string[] args) {     string filename = "data/tale.txt";     in in = new in (filename);     string[] words = in.readall().split("\\s+");      st<string, set<integer>> st = createconcordance (words);     stdout.println("finished building concordance");      // write file , read in (to check serialization works)     //serialize ("data/concordance-tale.txt", st);     //st = deserialize ("data/concordance-tale.txt");      words = invertconcordance (st);     savewords ("data/reconstructed-tale.txt", words); } 

}

first of - why using weird classes like:

  • set
  • st

instead of built-in java classes:

  • set
  • map

which nedded here?

as problem, code should not compile @ since declaring variable inv after using it:

public static string[] invertconcordance (st<string, set<integer>> st) {   //this have far //here have doesnt work for(string key : st.keys()) { inv[i++] = key; } for(int z = 0; z< inv.length; z++) { system.out.println(inv[z]); }    string[]inv = new string[st.size()];      return inv; } 

if understand idea correctly, concordances creates list of words , sets containing indices on there found. if correct interpretation inverse operation be:

public static string[] invertconcordance (st<string, set<integer>> st) {  //first - figure out length of document, maximum index in concordancer int document_length = 0; for(string key : st.keys()){   for(integer : st.get(key)){     if(i>document_length){       document_length=i;     }   } }      //create document string[] document = new string[document_length+1];  //reconstruct for(string key : st.keys()){   for(integer : st.get(key)){     document[i] = key;   } }  return document; } 

i assumed, indices numbered 0 document's length-1, if there stored 1 document'length should modify lines:

string[] document = new string[document_length+1]; 

to

string[] document = new string[document_length]; 

and

    document[i] = key; 

to

    document[i-1] = key; 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -