簡單實用玛追,哈哈税课。
轉(zhuǎn)載原文地址:點擊這里
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Highlighter {
public static void main(String[] args) throws IOException {
String content = "挖掘頻繁項集的方法可以擴展到挖掘閉頻繁項集(由它們?nèi)菀讓С鲱l繁項集的集合)。這些方法結(jié)合了附加的優(yōu)化技術(shù)痊剖,如項合并韩玩、子項剪枝和項跳過,以及模式樹中產(chǎn)生的項集的有效子集檢查陆馁。"
+ "挖掘頻繁項集和關(guān)聯(lián)已經(jīng)用不同的方法擴展找颓,包括挖掘多層關(guān)聯(lián)規(guī)則和多維關(guān)聯(lián)規(guī)則。多層關(guān)聯(lián)規(guī)則可以根據(jù)每個抽象層的最小支持度閾值如何定義叮贩,使用多種策略挖掘击狮。如一致的支持度、遞減的支持度和基于分組的支持度益老。"
+ "冗余的多層(后代)關(guān)聯(lián)規(guī)則可以刪除彪蓬,如果根據(jù)其對應(yīng)的祖先規(guī)則,他們的支持度和置信度接近于期望值的話捺萌。挖掘多維關(guān)聯(lián)規(guī)則的技術(shù)可以根據(jù)對量化屬性的處理分為若干類寞焙。第一,量化屬性可以根據(jù)預定義的概念分層靜態(tài)離散化互婿。"
+ "數(shù)據(jù)立方體非常適合這種方法捣郊,因為數(shù)據(jù)立方體和量化屬性都可以利用概念分層。第二慈参,可以挖掘量化關(guān)聯(lián)規(guī)則呛牲,其中量化屬性根據(jù)分箱和/或聚類動態(tài)離散化,“鄰近的”關(guān)聯(lián)規(guī)則可以用聚類合并驮配,產(chǎn)生更簡潔娘扩、更有意義的規(guī)則。"
+ "基于約束的規(guī)則挖掘允許用戶通過提供元規(guī)則(即模式模板)和其他挖掘約束對規(guī)則搜索聚焦壮锻。這種挖掘推動了說明性數(shù)據(jù)挖掘查詢語言和用戶界面的使用琐旁,并對挖掘查詢優(yōu)化提出了巨大挑戰(zhàn)。"
+ "規(guī)則約束可以分為五類:反單調(diào)的猜绣、單調(diào)的灰殴、簡潔的、可轉(zhuǎn)變的和不可轉(zhuǎn)變的掰邢。前四類約束可以在頻繁項集挖掘中使用牺陶,使挖掘更有功效伟阔,更有效率。沒有進一步分析或領(lǐng)域知識掰伸,關(guān)聯(lián)規(guī)則不應(yīng)該直接用于預測皱炉。"
+ "它們不必指示因果關(guān)系。然而狮鸭,對于進一步探查合搅,它們是有幫助的切入點,使得它們成為理解數(shù)據(jù)的流行工具歧蕉。流數(shù)據(jù)不斷地在計算機系統(tǒng)中流進流出并且具有變化的更新速度历筝,涉及數(shù)據(jù)流的應(yīng)用非常廣泛。"
+ "大綱提供數(shù)據(jù)流的匯總廊谓,通常用來返回查詢的近似解答梳猪。隨機抽樣、滑動窗口蒸痹、直方圖春弥、多分辨率方法、梗概以及隨機算法都是大綱的形式叠荠。傾斜時間框架模型允許數(shù)據(jù)以多個時間粒度存儲匿沛,最近的時間記錄在最細的粒度上,"
+ "最遠的時間記錄在最粗的粒度上榛鼎。流立方體可以存儲壓縮的數(shù)據(jù)逃呼,對時間維度使用傾斜時間框架模型,并且僅在一些關(guān)鍵的層上存儲數(shù)據(jù)者娱,關(guān)鍵層反映了分析人員最感興趣的數(shù)據(jù)層抡笼,從而基于到關(guān)鍵層的“常用路徑”進行部分物化。";
String query = "數(shù)據(jù)挖掘";
long start = System.currentTimeMillis();
String s = new Highlighter(query).getBestFragment(content);
System.out.println("Generate HTML: ");
File f = new File("demo.html");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
osw.write("<pre><b>" + content + "</pre><br/><br/>" + "<pre><b>" + s + "</pre><br/><br/>");
osw.close();
System.out.println("Hightlighter -> " + s);
System.out.println("cost: " + (System.currentTimeMillis() - start));
}
private static String BEGIN = "<font color=\"red\">";
private static String END = "</font>";
private Set<Character> set = new HashSet<Character>();
public Highlighter(String query) {
char[] chars = null;
chars = query.toCharArray();
for (int i = 0; i < chars.length; i++) {
set.add(chars[i]);
}
}
public String getBestFragment(String content) {
String[] strs = content.replace(".", "黄鳍。").split("推姻。");
char[] chars = null;
TreeSet<Sentence> ts = new TreeSet<Sentence>(new Comparator<Sentence>() {
public int compare(Sentence o1, Sentence o2) {
if (o1.getIndex() < o2.getIndex()) {
return -1;
} else if (o1.getIndex() > o2.getIndex()) {
return 1;
} else
return 0;
}
});
Sentence sentence = null;
int score = 0;
StringBuilder sb = null;
System.out.println("total sentences: " + strs.length);
for (int i = 0; i < strs.length; i++) {
sentence = new Sentence();
sb = new StringBuilder();
sentence.setIndex(i);
sentence.setText(strs[i]);
chars = strs[i].toCharArray();
for (int j = 0; j < chars.length; j++) {
if (set.contains(chars[j])) {
score++;
sb.append(BEGIN);
sb.append(chars[j]);
sb.append(END);
} else {
sb.append(chars[j]);
}
}
sentence.setValue(sb.toString());
sentence.setScore(score);
ts.add(sentence);
score = 0;
sb = new StringBuilder();
}
Iterator<Sentence> it = ts.iterator();
Sentence tmp = null;
int number = 0;
sb = new StringBuilder();
for (; it.hasNext();) {
tmp = it.next();
sb.append(tmp.getValue());
sb.append("。");
System.out.println(tmp);
number++;
}
System.out.println("After : " + number);
return sb.toString();
}
class Sentence {
String value;
int index;
int score;
String text;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String toString() {
return this.index + " " + this.score + " " + this.value;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
}
想了解更多精彩內(nèi)容請關(guān)注我的公眾號