⽤正则表达式给字符串添加空格
⾃然语⾔处理有⼀种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 = placeAll("(.{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;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。