您有2個列表。對於列表中的每個單詞,從第一個單詞到列表中其餘單詞的映射。例如,如果你在此列表中有 「人工智能」, 「蝙蝠洞」, 「狗」,你會保存它作爲:
"artificial" => { "artificial intelligence" }
"bat" => { "bat cave" }
"dog" => { "dog" }
這將是第一步。對列表進行預處理,以獲取第一首的地圖到列表中剩餘的單詞。
現在當你的行包含像「人工智能很酷」的聲明。你用\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
有很多的優化做的。
非常感謝:) – Epi