java - creating a word transformation graph -
i'm trying create graph of words dictionary adjacent words in graph should agree of these 2 rules: 1) 1 of words has 1 more character, , without character, identical 2) of equal length , identical except 1 letter (one-character substitution)
i wrote 2 methods create graph.
first one(addladder), uses first rule, working properly: i'm having trouble second method (addeq) uses second rule:
public static void addladder(){ for(map.entry<integer,set<string>> e:themap.entryset()) { int thewordlen = e . getkey ( ) ; set<string>thewords = e.getvalue( ); if(thewordlen>1){ set<string>shorterwords = themap.get(thewordlen-1); for(string s :thewords) { for(int i=0; i<thewordlen ; i++) { string shorter = removeonechar (s,i); if(shorterwords.contains( shorter ) ) { addedge(s,shorter,s.length()); addedge(shorter,s,s.length()); } } } } } } public static void addeq(){ for(map.entry<integer,set<string>> e:themap.entryset()) { int thewordlen = e.getkey ( ) ; set<string>thewords = e.getvalue( ); for(int i=0; i<thewordlen ; i++) { map<string , list<string>>repmap = new treemap<string , list<string>>(); list<string> mylist = new arraylist<string>(); for(string w:thewords ) { string shorter = removeonechar(w,i); mylist.add(shorter); repmap.put(w, mylist); } } }
}
thanks tips.
just check words character per character long edit distance 1: e.g.:
private boolean ruletwo (string s1, string s2) { if (s1.length() != s2.length()) return false; int different = 0; (int i=0; i<s1.length() && different <= 1; i++) { if (s1.charat(i) != s2.charat(i)) different ++; } return different == 1; }
Comments
Post a Comment