Java implementation of synonym maintenance

Source: Internet
Author: User

Title Requirements:
First, synonyms maintain a given interface, set two words to each other synonyms. Synonyms have mutual transitive, if A and B are synonyms, B and C are synonyms, then A, B, C are synonyms. requires an interface to query a given two but whether it is a synonym relationship. and can provide the interface to clear all the synonyms relationship. Interface Description/** * Set 2 words for synonyms * @param word1 word one * @param word2 word two * @return 0 for success,-1 for failure or other exception */public int setsynonyms (String word1 , String word2)/** * Determines whether 2 words are synonyms (the same word is considered synonyms) * @param word1 Word one * @param word2 Word two * @return for synonyms return true, otherwise return false */public Boo Lean issynonyms (String word1, String word2)/** * Clear synonyms between words */public void clearrelations ()

The procedure is as follows: first set A and B synonyms relationship, and then set B and C synonyms, B and C and a and C are synonyms, so the first two is true, when the clear method is executed, the map is emptied, the last print is false
1 ImportJava.util.HashMap;2 ImportJava.util.Iterator;3 ImportJava.util.Map;4 ImportJava.util.Scanner;5  Public classSynonyms {6     Private StaticBoolean Issyn =false;7     Staticmap<string, string> map =NewHashmap<string, string>();8      Public Static voidMain (string[] args) {9Scanner scan =NewScanner (system.in);TenString line =scan.nextline (); Onestring[] str = line.split (""); ASystem.out.println (setsynonyms (str[0), str[1])); -String line2 =scan.nextline (); -string[] str2 = Line2.split (""); theSystem.out.println (setsynonyms (str2[0), str2[1])); -System.out.println (issynonyms (str2[0), str2[1])); -System.out.println (issynonyms (str[0), str2[1])); - clearrelations (); +System.out.println (issynonyms (str2[0), str2[1])); - scan.close (); +     } A  at      Public Static intsetsynonyms (String word1, String word2) { - map.put (Word1, word2); - map.put (Word2, word1); -  -         if(Word1! = "" & Word2! = "") { -             //Issyn = true; in             return0; -}Else to             return-1; +  -     } the  *      Public Static Booleanissynonyms (String word1, String word2) { $         if(!Map.containskey (word1)) {Panax NotoginsengIssyn =false; -         } the         if(Map.containskey (word1)) { +              for(String key:map.keySet ()) { A                  for(inti = 0; I < key.length (); i++) { theString value =Map.get (word1); +String value2 =Map.get (value); -                     if(Value2.equals (Word2)) { $Issyn =true; $}Else if(Word2.equals (Map.get (word1))) -Issyn =true; -                 } the             } - Wuyi}Else theIssyn =false; -         returnIssyn; Wu     } -  About      Public Static voidclearrelations () { $Iterator it =Map.keyset (). iterator (); -String key =NULL; -          while(It.hasnext ()) { -Key =It.next (). toString (); A It.remove (); +  the         } -     } $}

Java implementation of synonym maintenance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.