[英]Efficient algorithm to find matching common words or phrases from a corpus
我正在嘗試找到一種有效的方法來查找常用短語。 我想我可以用一個例子更好地解釋。
輸入:將每行視為句子
B
B C
A B C B
D E
F D E
輸出:
B
D E
刪除了第2行和第3行,因為B(第1行)是它們共同的。 由於第4行很常見,因此省去了第5行。
我希望我已經解釋了!
我可以通過匹配來運行O(n ^ 2)。 欣賞任何更好的東西。
更新 :請考慮順序(例如DE
應該與句子FDE
匹配, ED
應該不匹配。)
我能想到的最快方法是:
public static void main(String[] args) throws Exception {
List<String> toOutput = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
String line;
while ((line = br.readLine()) != null) {
boolean add = true;
for (int i = 0; i < toOutput.size(); i++) {
if (toOutput.get(i).contains(line)) {
toOutput.remove(i);
} else if (line.contains(toOutput.get(i))) {
add = false;
break;
}
}
if (add) {
toOutput.add(line);
}
}
br.close();
for (String s : toOutput) {
System.out.println(s);
}
}
input.txt中:
B
B C
A B C B
F D E
D E
輸出:
B
D E
驗證當前句子是否包含我們當前發現唯一的任何字符串。 我認為沒有更有效的方法可以做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.