3

我将 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 \\
4

1 回答 1

2

您可以仅使用串联:

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后面必须跟=,而不是空格。周围不得有空格=path = 'text'是错的,path ='text'是错的,path= 'text'也是错的。
  • 当你使用一个变量时,在它前面加上$:!echo $path'train.en'将打印//content//drive//My Drive//Colab Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//train.en
  • 连接意味着只需将字符串文字粘合到不需要使用 , 等的变量+&
于 2020-05-30T14:40:18.893 回答