自然語言處理有一種ROUGE的評測方法,使用這種評測方法時有時需要將帶評測文本每個漢字之間用空格分開。
原版說明如下:
The recommended ROUGE metrics are Recall and F scores of
Character-based ROUGE-1, ROUGE-2 and ROUGE-SU4. Characterbased
evaluation means that we do not need to perform Chinese word
segmentation when running the ROUGE toolkit. (too long no look:D)Instead, we only need to
separate each Chinese character by using a blank space.
使用正則表達式有一種非常便捷的方法可以解決這個問題:
replaceAll("(.{1})","$1 ")
其中.{1}表示任意一個字符,()表示一組,$1表示第一組
因此字符串ABCDEF匹配過程就是將ABCDEF的每一個字符.{1}都替換成每一個字符.{1}+空格。即是A B C D E F。
拓展:
String str = "abcdefghijklmn";
String str_next = str.replaceAll("(.{1})(.{2})(.{1})","$2$3");
結果是什么呢?
abcdefghijklmn
機智的你一定已經知道答案了吧:bcdfghjklmn~~~~
(.{1})(.{2})(.{1})對應a、bc、d; e、fg、h; i、jk、l;
$2對應bc; fg; jk;
$3對應d; h; l;