我是java的新手。我需要在一个句子中计算彼此的单词关联。例如,对于句子“狗是狗而猫是猫”,最终的关联计数将是 - 第一排:Dog-Dog(0),Dog-is(2),Dog-a(2)Dog-and(1),Dog-Cat(2)
等等。
开发关联矩阵。关于如何开发的任何建议?
答案 0 :(得分:3)
谢谢罗马。我可以从句子中分开单词 -
String sentence=null;
String target="Dog is a Dog and Cat is a Cat";
int index = 0;
Locale currentLocale = new Locale ("en","US");
BreakIterator wordIterator = BreakIterator.getWordInstance(currentLocale);
//Creating the sentence iterator
BreakIterator bi = BreakIterator.getSentenceInstance();
bi.setText(target);
while (bi.next() != BreakIterator.DONE) {
sentence = target.substring(index, bi.current());
System.out.println(sentence);
wordIterator.setText(sentence);
int start = wordIterator.first();
int end = wordIterator.next();
while (end!=BreakIterator.DONE){
String word = sentence.substring(start,end);
if (Character.isLetterOrDigit(word.charAt(0))) {
System.out.println(word);
}//if (Character.isLetterOrDigit(word.charAt(0)))
start = end;
end = wordIterator.next();
}//while (end!=BreakIterator.DONE)
index = bi.current();
} // while (bi.next() != BreakIterator.DONE)
但没有得到你的另外两点。感谢。
答案 1 :(得分:0)
这很简单:
String[] words = sentence.split("\\s"); //first step
List<List<String>> pairs =
new ArrayList<List<String>>((int)(((words.length) / 2.0) * (words.length - 1)));
for (int i = 0; i < words.length - 1; i++) {
for (int j = i + 1; j < words.length; j++) {
List<String> pair = Arrays.asList(words[i], words[j]);
Collections.sort(pair);
pairs.add(pair);
}
} //second step
Map<List<String>, Integer> pair2count = new LinkedHashMap<List<String>, Integer>();
for (List<String> pair : pairs) {
if (pair2count.containsKey(pair)) {
pair2count.put(pair, pair2count.get(pair) + 1);
} else {
pair2count.put(pair, 1);
}
} //third step
//output
System.out.println(pair2count);