1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Synonym { public static void main(String[] args){ String sentence="A PDA is an invaluable tool to help manage our life."; String[] word={"PDA","tool","life"}; String[] syn={"handheld organizer","device","daily tasks"}; for(int i=0;i<word.length;i++){ if(sentence.contains(word[i])){ sentence=sentence.replace(word[i],syn[i]); } } System.out.println(sentence); } } |
HashMap
The following program utilizes HashMap to do the same thing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.*; public class Synonym2 { public static void main(String[] args){ Map map = new HashMap(); map.put("PDA","handheld organizer"); map.put("tool","device"); map.put("life","daily tasks"); ArrayList<String> lst=new ArrayList<String>(); String sentence="A PDA is an invaluable tool to help manage our life."; StringTokenizer st=new StringTokenizer(sentence," "); while(st.hasMoreTokens()){ String temp=st.nextToken(); if(temp.endsWith(("."))){ temp=temp.substring(0,temp.length()-1); } lst.add(temp); } for(int i=0;i<lst.size();i++){ for(int j=0;j<map.size();j++){ if(map.get(lst.get(i))!=null){ String stemp=map.get(lst.get(i)).toString(); sentence=sentence.replace(lst.get(i),stemp); } } } System.out.println(sentence); } } |
Map map = new HashMap(); map.put("PDA","handheld organizer"); map.put("tool","device"); map.put("life","daily tasks");
HashMap is a class that implements Map interface. It stores key-value pairs (like a dictionary). The put method adds elements. The first argument is the key and the second is the value linked to the key.
StringTokenizer st=new StringTokenizer(sentence," "); while(st.hasMoreTokens()){ String temp=st.nextToken(); if(temp.endsWith(("."))){ temp=temp.substring(0,temp.length()-1); } lst.add(temp); }
StringTokenizer is used to split the sentence into words. It breaks a string into tokens (space character in this case). The hasMoreTokens() detects if there is still a space character in the sentence that is not yet processed. The nextToken() method returns returns the string value of the next token. The value is given to a temporary String named "temp".
if(temp.endsWith(("."))){ temp=temp.substring(0,temp.length()-1); } lst.add(temp);
This code detects if there is a dot (".") in the word that has just been extracted. If dot (".") is found, it will be removed from the word using substring method. All the extracted words are stored in an ArrayList.
for(int i=0;i<lst.size();i++){ for(int j=0;j<map.size();j++){ if(map.get(lst.get(i))!=null){ String stemp=map.get(lst.get(i)).toString(); sentence=sentence.replace(lst.get(i),stemp); } } }
These lines of code perform the replacement process. The first loop iterates through the ArrayList. The second loop iterates through the HashMap. Put it simply, all the words in the sentence are compared with all the keys in the HashMap. If there is a match, the corresponding value is retrieved and the replacement process begins.
Suggestions:
ReplyDeleteInstead of 3 loops (first outer loop and second outer loop with inner loop), we could do away with just one loop by using TreeMap. Lookup from TreeMap will be faster....Let me see if i can get the code...
http://www.techiesinfo.com/performance
ConcurrentHashMap seems faster for lookups than TreeMap.
ReplyDeletepublic class Synonym {
ReplyDeleteprivate final Map synonyms = new ConcurrentHashMap<>();
public void addSynonym(K key, V value) {
synonyms.put(key, value);
}
public String synonymize(String sentence) {
String words[] = sentence.split(" ");
StringBuilder synonymized = new StringBuilder();
for (String word : words) {
V value = synonyms.get(word);
if (value != null) {
synonymized.append(value);
} else {
synonymized.append(word);
}
}
return synonymized.toString();
}
}
// Code inside Client Class
// create Synonym dictionary
Synonym addressSynonym = new Synonym<>();
addressSynonym.addSynonym("west", "w");
addressSynonym.addSynonym("Drive", "dr");
String address1 = "100 W Beverly Drive";
String address2 = "100 West Beverly Dr";
long time = System.nanoTime();
// convert to lower case
address1 = address1.toLowerCase();
address2 = address2.toLowerCase();
// replace input sentence with synonyms
address1 = addressSynonym.synonymize(address1);
address2 = addressSynonym.synonymize(address2);
time = (System.nanoTime() - time);
if (address1.equalsIgnoreCase(address2)) {
assertTrue("Synonym Worked", true);
} else {
assertTrue("Synonym Failed", false);
}
The above code response time is 77 microseconds.
http://www.techiesinfo.com/performance
Joy Of Java: Replacing Words With Their Synonyms >>>>> Download Now
ReplyDelete>>>>> Download Full
Joy Of Java: Replacing Words With Their Synonyms >>>>> Download LINK
>>>>> Download Now
Joy Of Java: Replacing Words With Their Synonyms >>>>> Download Full
>>>>> Download LINK 9f