我需要从文本生成特征。我在下面使用的脚本可以在线使用,但实际上我不知道如何运行它,因为我根本不懂python。 我有一个名为(火车.txt)包含以下内容
He PRP B-NP
reckons VBZ B-VP
the DT B-NP
current JJ I-NP
account NN I-NP
deficit NN I-NP
will MD B-VP
narrow VB I-VP
to TO B-PP
only RB B-NP
# # I-NP
1.8 CD I-NP
billion CD I-NP
in IN B-PP
September NNP B-NP
. . O
我有一个python脚本,可以将上述文本转换为以下功能:
^{pr2}$python脚本是
# Separator of field values.
separator = ' '
# Field names of the input data.
fields = 'w pos y'
# Attribute templates.
templates = (
(('w', -2), ),
(('w', -1), ),
(('w', 0), ),
(('w', 1), ),
(('w', 2), ),
(('w', -1), ('w', 0)),
(('w', 0), ('w', 1)),
(('pos', -2), ),
(('pos', -1), ),
(('pos', 0), ),
(('pos', 1), ),
(('pos', 2), ),
(('pos', -2), ('pos', -1)),
(('pos', -1), ('pos', 0)),
(('pos', 0), ('pos', 1)),
(('pos', 1), ('pos', 2)),
(('pos', -2), ('pos', -1), ('pos', 0)),
(('pos', -1), ('pos', 0), ('pos', 1)),
(('pos', 0), ('pos', 1), ('pos', 2)),
)
import crfutils
def feature_extractor(X):
# Apply attribute templates to obtain features (in fact, attributes)
crfutils.apply_templates(X, templates)
if X:
# append BOS and EOS features manually
X[0]['F'].append('__BOS__') # BOS feature
X[-1]['F'].append('__EOS__') # EOS feature
if __name__ == '__main__':
crfutils.main(feature_extractor, fields=fields, sep=separator)
两者兼而有之脚本.py以及crfutils.py存在于同一文件夹中 我在Windows 7上从cmd运行上述脚本,如下所示:
C:\>Python script.py train.txt > train.result.txt
我有一个空文件train.result.txt文件 因为我对python还不熟悉(实际上只是开始学习它)。我不知道是什么问题?我提供论点的顺序有错吗?是的格式火车.txt文件错误?在
你得进去火车.txt在stdin上,不是作为命令行参数:
相关问题 更多 >
编程相关推荐