Search code examples
pythonpytorchopennmt

how convert string to path in opemnmt-py


I use opennmt-py for MT and in the code any time I want to set a path I have to write all directory and it's not good looking when I have long directory. is there any way to set a string as the main directory and just add the file name to the end. I use google colab to train the model The code is like:

!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//

and the code I want to be like:

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//'

or maybe just can write all path in a variable and use it like:

path_train ='//content//dri`ve//My\ Drive//Colab\ Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//'

!onmt_preprocess  \\
-train_src path_train \\

Solution

  • You may use a mere concatenation:

    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//'
    

    Notes:

    • The variable path must be followed with =, not a space. There must be no spaces around =. The path = 'text' is wrong, path ='text' is wrong, path= 'text' is also wrong.
    • When you use a variable, prepend it with $: !echo $path'train.en' will print //content//drive//My Drive//Colab Notebooks//NLP//spring99//CA6//Corpora//En2Fa-Translation//train.en
    • Concatenation means just glueing string literals to variables no need using +, &, etc.