近义词维护
给定接口,设置两个单词相互近义。近义词具有相互传递性,如果A和B为近义词,B和C是近义词,那么A、B、C都为近义词。要求提供接口,查询给定的两个但是是否是近义词关系。并且能提供接口清除所有的近义词关系。
接口说明
/**
* 设置2个单词为近义词
* @param word1 单词一
* @param word2 单词二
* @return 0为成功,-1为失败或其他异常
*/
public int setSynonyms(String word1, String word2)
/**
*判断2个单词是否为近义词(同一单词视为近义词)
*@param word1 单词一
*@param word2 单词二
*@return 为近义词返回true,否则返回false
*/
public boolean isSynonyms(String word1, String word2)
/**
* 清除单词之间的近义词关系
*/
public void clearRelations()
package huawei;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
/**
* <pre>
* 近义词维护
*
*
* 给定接口,设置两个单词相互近义。
* 近义词具有相互传递性,如果A和B为近义词,B和C是近义词,那么A、B、C都为近义词。
* </pre>
*
*/
public class Demo {
public static ArrayList<HashSet<String>> arraylist = new ArrayList<HashSet<String>>();
/**
* 设置2个单词为近义词
*
* @param word1 单词一
* @param word2 单词二
* @return {@code 0}为成功,{@code -1}为失败或其他异常
*/
public int setSynonyms(String word1, String word2) {
if(null == word1 || null == word2 || "".equals(word1) || "".equals(word2) || word1.equals(word2)){
return -1;
}
if(arraylist.isEmpty()){
HashSet<String> st = new HashSet<String>();
st.add(word1);
st.add(word2);
arraylist.add(st);
}
else
{
for (int i = 0;i < arraylist.size();i++)
{
if(arraylist.get(i).contains(word1) || arraylist.get(i).contains(word2)){
arraylist.get(i).add(word1);
arraylist.get(i).add(word2);
break;
}
if(i == arraylist.size()-1)
{
HashSet<String> hashSet = new HashSet<String>();
hashSet.add(word1);
hashSet.add(word2);
arraylist.add(hashSet);
}
}
}
return 0;
}
/**
* 判断2个单词是否为近义词(同一单词视为近义词)
*
* @param word1 单词一
* @param word2 单词二
* @return 为近义词返回{@code true},否则返回{@code false}
*/
public boolean isSynonyms(String word1, String word2) {
if(null == word1 || null == word2 || "".equals(word1) || "".equals(word2)){
return false;
}
if(word1.equals(word2)){
return true;
}
for (Set<String> set : arraylist) {
if(set.contains(word1) && set.contains(word2)){
return true;
}
}
return false;
}
/**
* 清除单词之间的近义词关系
*/
public void clearRelations() {
arraylist.clear();
}
}
package testcase;
import huawei.Demo;
import junit.framework.TestCase;
public class DemoTest extends TestCase{
public void testSynonyms() {
Demo demo = new Demo();
demo.setSynonyms("good", "nice");
demo.setSynonyms("nice", "great");
demo.setSynonyms("perfect", "great");
demo.setSynonyms("aaa", "bbb");
demo.setSynonyms("bbb", "ccc");
assertTrue(demo.isSynonyms("good","nice"));
assertTrue(demo.isSynonyms("good","great"));
assertTrue(demo.isSynonyms("good","perfect"));
assertTrue(demo.isSynonyms("good","good"));
assertTrue(demo.isSynonyms("nice","nice"));
assertTrue(demo.isSynonyms("aaa","ccc"));
assertFalse(demo.isSynonyms("great","aaa"));
assertFalse(demo.isSynonyms("good","bad"));
assertFalse(demo.isSynonyms("good","not bad"));
demo.clearRelations();
assertTrue(demo.isSynonyms("good","good"));
assertTrue(demo.isSynonyms("nice","nice"));
assertFalse(demo.isSynonyms("good","nice"));
assertFalse(demo.isSynonyms("good","great"));
assertFalse(demo.isSynonyms("good","perfect"));
assertFalse(demo.isSynonyms("aaa","ccc"));
}
}