如何在 opennmt-py 中将字符串转换为路径
how convert string to path in opemnmt-py
我将 opennmt-py 用于 MT 并且在代码中任何时候我想设置一个路径我必须写所有目录并且当我有长目录时它不好看。有什么方法可以将字符串设置为主目录,只需将文件名添加到末尾即可。
我使用 google colab 来训练模型
代码如下:
!onmt_preprocess \
-train_src //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//train.en \
-train_tgt //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//train.fa \
-valid_src //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//dev.en \
-valid_tgt //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//dev.fa \
-save_data //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//demo//
以及我想要的代码:
path ='//content//dri`ve//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//'
!onmt_preprocess \
-train_src path+'train.en' \
-train_tgt path+'train.fa' \
-valid_src path+'dev.en' \
-valid_tgt path++'dev.fa' \
-save_data path+'demo//'
或者也许可以将所有路径写入变量并像这样使用它:
path_train ='//content//dri`ve//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//'
!onmt_preprocess \
-train_src path_train \
您可以仅使用串联:
path='//content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//'
!onmt_preprocess \
-train_src $path'train.en' \
-train_tgt $path'train.fa' \
-valid_src $path'dev.en' \
-valid_tgt $path'dev.fa' \
-save_data $path'demo//'
备注:
- 变量
path
必须跟在 =
之后,而不是 space。 =
周围不能有 space。 path = 'text'
错了,path ='text'
错了,path= 'text'
也错了。
- 当你使用一个变量时,在它前面加上
$
:!echo $path'train.en'
将打印//content//drive//My Drive//Colab Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//train.en
- 串联意味着只需将字符串文字粘合到变量,无需使用
+
、&
等
我将 opennmt-py 用于 MT 并且在代码中任何时候我想设置一个路径我必须写所有目录并且当我有长目录时它不好看。有什么方法可以将字符串设置为主目录,只需将文件名添加到末尾即可。 我使用 google colab 来训练模型 代码如下:
!onmt_preprocess \
-train_src //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//train.en \
-train_tgt //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//train.fa \
-valid_src //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//dev.en \
-valid_tgt //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//dev.fa \
-save_data //content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//demo//
以及我想要的代码:
path ='//content//dri`ve//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//'
!onmt_preprocess \
-train_src path+'train.en' \
-train_tgt path+'train.fa' \
-valid_src path+'dev.en' \
-valid_tgt path++'dev.fa' \
-save_data path+'demo//'
或者也许可以将所有路径写入变量并像这样使用它:
path_train ='//content//dri`ve//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//'
!onmt_preprocess \
-train_src path_train \
您可以仅使用串联:
path='//content//drive//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//'
!onmt_preprocess \
-train_src $path'train.en' \
-train_tgt $path'train.fa' \
-valid_src $path'dev.en' \
-valid_tgt $path'dev.fa' \
-save_data $path'demo//'
备注:
- 变量
path
必须跟在=
之后,而不是 space。=
周围不能有 space。path = 'text'
错了,path ='text'
错了,path= 'text'
也错了。 - 当你使用一个变量时,在它前面加上
$
:!echo $path'train.en'
将打印//content//drive//My Drive//Colab Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//train.en
- 串联意味着只需将字符串文字粘合到变量,无需使用
+
、&
等