用整数 ID 替换字符串标签并返回

Substituting string labels by integer IDs and back

我的数据文件包含第一个实体是字符串标签的行,后跟特征。例如:

MEMO |f write down this note
CALL |f call jim's cell

问题是 Vowpal Wabbit 只接受整数标签。如何快速从字符串标签更改为唯一整数 ID 并返回?即快速修改数据文件为:

1 |f write down this note
2 |f call jim's cell

...需要时返回。

对于我的样本数据集,我使用 ``sed'' 为每个 class 手动完成,但这严重破坏了我的工作流程。

cat input.data | perl -nale '$i=$m{$F[0]}; $i or $i=$m{$F[0]}=++$n; $F[0]=$i; print "@F"; END{warn "$_ $m{$_}\n" for sort {$m{$a}<=>$m{$b}} keys %m}' > output.data 2> mapping.txt