您有 2 个列表。为列表中的每个单词制作从第一个单词到列表中其余单词的映射。例如,如果您在此列表中有“人工智能”、“蝙蝠洞”、“狗”,则将其存储为:
"artificial" => { "artificial intelligence" }
"bat" => { "bat cave" }
"dog" => { "dog" }
这将是第一步。预处理列表以获取 firstword 到列表中其余单词的映射。
现在,当您的行包含诸如“人工智能很酷”之类的声明时。你用 分割线\w
。你得到的话。我们遇到的第一个词是“人造的”。我们查看之前获得的两个地图。artificial
所以我们在其中一张地图中看到了一个键。我们知道该行中的下一个单词是什么。尽管如此,我们还是想与最长的比赛进行比赛。所以我们比较得到对应的单词列表artificial
。并进行最长的子串匹配。我们artificial intelliegence
一起寻找,因为我们正在寻找最长的匹配。尽管如此,我们对第二个列表重复该过程。根据哪个更长,我们选择它属于列表 1 还是列表 2。
这是一些示例代码。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordSplits {
public static Map<String, List<String>> first2rest(List<String> wordList) {
Map<String, List<String>> first2RestWords = new HashMap<String, List<String>>();
for (String word : wordList) {
// TODO Make it use Pattern. Sample demo. Get the first word of
// every string.
String splits[] = word.split("\\W");
String firstWord = splits[0];
List<String> restWords = first2RestWords.get(firstWord);
if (restWords == null) {
restWords = new ArrayList<String>();
}
restWords.add(word);
// store the complete pattern nevertheless
first2RestWords.put(firstWord, restWords);
}
return first2RestWords;
}
public static Map<String, List<Integer>> longestSubstring(String line,
List<String> first, List<String> second) {
Map<String, List<Integer>> occurences = new LinkedHashMap<String, List<Integer>>();
Map<String, List<String>> first2RestWords = first2rest(first);
Map<String, List<String>> second2RestWords = first2rest(second);
Matcher wordMatcher = Pattern.compile("\\w+").matcher(line);
for (int start = 0; start < line.length() && wordMatcher.find(start);) {
String word = wordMatcher.group();
String maxWordFirst = "", maxWordSecond = "";
if (first2RestWords.containsKey(word)) {
maxWordFirst = longestMatch(
line.substring(wordMatcher.start()),
first2RestWords.get(word));
}
if (second2RestWords.containsKey(word)) {
maxWordSecond = longestMatch(
line.substring(wordMatcher.start()),
second2RestWords.get(word));
}
if (maxWordFirst.length() > 0 || maxWordSecond.length() > 0) {
if (maxWordFirst.equals(maxWordSecond)) {
System.out.println("Belongs to both the lists : " + maxWordFirst);
} else {
if (maxWordFirst.length() > maxWordSecond.length()) {
System.out.println("Belongs to first list: " + maxWordFirst);
} else if (maxWordSecond.length() > maxWordFirst.length()) {
System.out.println("Belongs to second list: " + maxWordSecond);
}
}
} else {
System.out.println(word + " does not belong to any list");
}
// Take some action
start = wordMatcher.start() + Math.max(maxWordFirst.length(), maxWordSecond.length()) + 1;
start = Math.max(wordMatcher.end(), start);
}
return occurences;
}
public static String longestMatch(String line, List<String> wordList) {
String maxWord = "";
// poor way to compare
for (String word : wordList) {
if (line.startsWith(word) && word.length() > maxWord.length()) {
maxWord = word;
}
}
return maxWord;
}
public static void main(String[] args) {
longestSubstring("artificial intelligence is cool. bat.",
Arrays.asList("dog", "cow", "dog", "artificial intelligence", "bat"),
Arrays.asList("artificial", "hound", "cool", "bat", "dog hound"));
}
}
要处理的行是"artificial intelligence is cool. bat."
l1 = `"dog", "cow", "dog", "artificial", "artificial intelligence", "bat"`
l2 = `"intelligence", "hound", "cool", "bat", "dog hound"`
程序的输出是
Belongs to first list: artificial intelligence
is does not belong to any list
Belongs to second list: cool
Belongs to both the lists : bat
有很多优化要做。