我需要帮助解决这个问题。
我有两个不相交的字符串列表:list A = {a1, ..., an}和list B = {b1, ..., bn} --列表中的一个元素可以是一个简单的单词,如“人工”或“智能”,也可以由更多的单词(如“人工智能”)复合。我还有一个句子,里面有很多单词。其中一些人在这两份名单中之一。
我要做的是在同一个句子中计算出两个列表中的两个字符串在一起的次数。
问题是,如果我在句子中找到一个类似人工智能的词,那么正确的考虑词将仅仅是“人工智能”(而不是“人工智能”或“智力”)。
我想把列表中的每一个单词都加进一棵树中,然后按长度排序,只取最长的单词,但我认为这个解决方案不是很好,也不是很有效。
目前,代码看起来是这样的(但它仍然存在我正在讨论的问题)
// iterates on the words from the list A
for (String a: A)
// if the phrase contains the word
if (phrase.matches(".*\\b" + a + "\\b.*")
// iterates on the words from the list B
for (String b: B)
// if the phrase contains the word
if (phrase.matches(".*\\b" + b + "\\b.*")
// do stuffs你有什么意见建议?谢谢!
发布于 2013-08-17 19:18:33
你有两个名单。对于列表中的每个单词,从第一个单词到列表中的其余单词都要绘制一张地图。例如,如果您在此列表中有“人工智能”、“蝙蝠洞”、“狗”,您可以将其存储为:
"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 有许多优化工作要做。
发布于 2013-08-17 17:29:21
我不确定我是否完全理解了您的需求,但是如果您只需要计数,则可以对列表中的字符串给予权重。例如,如果有条目
artificial -> 1
intelligence -> 1
artificial intelligence -> -1如果句子中包含“人工智能”,那么这三个句子都将匹配一个权重之和= 1。
这需要一些预处理来计算字符串的正确权重。
发布于 2013-08-17 18:12:08
我的想法是跟踪考虑过的单词,然后清理。
试着做这样的事情:
int counter = 0;
List<String[]> h = new ArrayList<String[]>();
HashSet<String> words = new HashSet<String>();
// iterates on the words from the list A
for (String a: A)
// if the phrase contains the word
if (phrase.matches(".*\\b" + a + "\\b.*"))
// iterates on the words from the list B
for (String b: B)
// if the phrase contains the word
if (phrase.matches(".*\\b" + b + "\\b.*")) {
h.add(new String[]{a,b});
words.add(a);
words.add(b);
}
// clean up:
// 1. clean words
for (String i:words) {
// in words, keep only strings that are not contained by others
}
// 2. clean h
for (String[] i : h) {
// if i[0] or i[1] are both in words, then
// increment counter... or whatever you want
}希望我能理解你的问题
https://stackoverflow.com/questions/18290699
复制相似问题