java - search sub words in word from array of words? -


i have been working on assignment in have read words file , find longest word , check how many sub words contains in longest word? should work words in file.

i tried using java code wrote works small amount of data in file task process huge amount of data.

example: file words: "call","me","later","hey","how","callmelater","now","iam","busy","noway","nowiambusy"

o/p: callmelater : subwords->call,me,later

in i'm reading file words storing in linked list , finding longest word & removing list checking how many sub-words extracted word contains.

main class assignment:

import java.util.scanner; public class assignment { public static void main (string[] args){     long start = system.currenttimemillis();;         assignment = new assignment();     a.throwinstructions();      scanner userinput = new scanner(system.in);     string filename = userinput.nextline();  //  string filename = "ab.txt"; //  string filename = "abc.txt";     logic testrun = new logic(filename); //  //testrun.result();      long end = system.currenttimemillis();;      system.out.println("time taken:"+(end - start) + " ms"); }  public void throwinstructions(){     system.out.println("keep input file in same directory, code is");     system.out.println("please specify fie name : "); } 

subclass logic processing:

import java.io.bufferedreader; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.hashmap; import java.util.iterator; import java.util.linkedlist; import java.util.list; import java.util.map; import java.util.set;  public class logic { private string filename; private file file; private list<string> words = new linkedlist<string>(); private map<string, string> matchedwords = new hashmap();  @override public string tostring() {     return "logic [words=" + words + "]"; }  // constructor public logic(string filename) {     this.filename = filename;     file = new file(this.filename);     fetchfile();     run();     result(); }  // find such words , store in map public void run() {      while (!words.isempty()) {         string longestword = extractlongestword(words);         findmatch(longestword);     } }  // find longest word private string extractlongestword(list<string> words) {     string longword;     longword = words.get(0);     int maxlength = words.get(0).length();     (int = 0; < words.size(); i++) {         if (maxlength < words.get(i).length()) {             maxlength = words.get(i).length();             longword = words.get(i);         }     }     words.remove(words.indexof(longword));     return longword; }  // find match word in array of sub words private void findmatch(string longestword) {     boolean chunkfound = false;     int chunkcount = 0;     stringbuilder subwords = new stringbuilder();     (int = 0; < words.size(); i++) {         if (longestword.indexof(words.get(i)) != -1) {             subwords.append(words.get(i) + ",");             chunkfound = true;             chunkcount++;         }     }      if (chunkfound) {         matchedwords.put(longestword,                 "\t" + (subwords.substring(0, subwords.length() - 1))                         + "\t:subword count:" + chunkcount);     } }  // fetch data file , store in list public void fetchfile() {     string word;     try {         filereader fr = new filereader(file);         bufferedreader br = new bufferedreader(fr);         while ((word = br.readline()) != null) {             words.add(word);         }         fr.close();         br.close();     } catch (filenotfoundexception e) {         // e.printstacktrace();         system.out                 .println("error: file -> "                         + file.tostring()                         + " not exists,please check filename or location , try again.");     } catch (ioexception e) {         // e.printstacktrace();         system.out.println("error: problem reading -> " + file.tostring()                 + " file, problem file format.");     }  }  // display result public void result() {     set set = matchedwords.entryset();     iterator = set.iterator();     system.out.println("word:\tword-length:\tsubwords:\tsubwords-count");     while (i.hasnext()) {         map.entry me = (map.entry) i.next();         system.out.print(me.getkey() + ": ");         system.out.print("\t" + ((string) me.getkey()).length() + ": ");         system.out.println(me.getvalue());     } } } 

this programs lacks , goes never ending loop. complexity of program high. reduce processing time need efficient approach binary/merge sort approach take least time o(log n) or o(nlog n).

if can me or @ least suggestion in direction should proceed. please suggest me programming language implement such text processing tasks in fast way ?

thanks in advance

this problem requires trie. have augment trie: generic 1 not do. geek viewpoint has trie written in java. particular work happen in method getwordlist. getwordlist take input longest word (i.e. longestword) , try see if each substring comprises words exist in dictionary. think have given enough -- can't work you. if have further question, don't hesitate ask.

other in getwordlist, might able pretty keep trie geek viewpoint way is.

you in luck because geek viewpoint demonstrates trie using boggle example , problem very trivial version of boggle.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -