/**
* 一个词组的单词如果是另一个词组单词的子集,就认为是个borad match,例如对于 "a b c","a", "b c" "c a" "a b c"都匹配,而 "a d"不匹配。
* 现有一个搜索匹配模块,输入为用户的查询来匹配一个词组字典,找到字典中所有可以和输入broad match的词组,输出预定的词组整型序号。
* 例如"cheap iphone in china",字典中有 1. "cheap iphone", 2. "cheap mobile", 3 "china iphone"
* @author Administrator
*
* 一个词组的单词如果是另一个词组单词的子集,就认为是个borad match,例如对于 "a b c","a", "b c" "c a" "a b c"都匹配,而 "a d"不匹配。
* 现有一个搜索匹配模块,输入为用户的查询来匹配一个词组字典,找到字典中所有可以和输入broad match的词组,输出预定的词组整型序号。
* 例如"cheap iphone in china",字典中有 1. "cheap iphone", 2. "cheap mobile", 3 "china iphone"
* @author Administrator
*
*/
package com.org.improve.contact.indexof;
public class BoradMatch {
public static boolean isBoradMatch(String parent, String children){
String[] childrenItems=children.split(" ");//以空格为分隔符
for (String childrenItem:childrenItems) {
if (parent.indexOf(childrenItem)==1) {
return false;
}
}
return true;
}
public static void main(String[] args) {
StringBuffer result=new StringBuffer();
String parent="cheap iphone in china";
String [] dicts={"cheap iphone","cheap mobile","in china"};
for (int i = 0; i < dicts.length; i++) {
if (isBoradMatch(parent,dicts[i])) {
result.append(",").append(i+1);
}
}
if (result.length()>0) {
System.out.println("匹配的结果为:"+result.substring(1));
} else{
System.out.println("无任何匹配结果!");
}
}
}