首页 > 解决方案 > 如何绑定绝对路径而不是相对于其中使用 Node 和 Perl 调用的下标?

问题描述

我有一个脚本,它的代码如下:

def tokenize(latex,kind='normalize'):
    output_file = './out.lst'
    input_file = './input_file.lst'

    cmd = "perl -pe 's|hskip(.*?)(cm\\|in\\|pt\\|mm\\|em)|hspace{\\1\\2}|g' %s > %s"%(input_file, output_file)
    ret = subprocess.call(cmd, shell=True)

    temp_file = output_file + '.tmp' # Create Temporary file
    with open(temp_file, 'w') as fout:  
        with open(output_file) as fin:
            for line in fin:
                fout.write(line.replace('\r', ' ').strip() + '\n')  # delete \r

    cmd = "cat %s | node ./preprocess_latex.js %s > %s "%(temp_file, kind, output_file)
    ret = subprocess.call(cmd, shell=True)
    os.remove(temp_file)

tokenize函数位于: main -> latex -> scripts -> preprocessing -> utils.py -> tokenize() 并且它使用一个名为的文件,该文件preprocess_latex.js位于同一目录中。我__init__.py在每个目录中都有文件,因此我可以tokenize()从主目录调用:

from latex_utils.scripts.preprocessing.filter_tokenize import *

问题:我无法更改目录结构,也无法移动preprocess.js,因为它使用了一些KaTex和一些复杂的 JavaScript(我不知道它是什么以及它做什么)并从../../some_file/some_script.js

错误是这样的:

>>> from latex_utils.scripts.preprocessing.filter_tokenize import *
module.js:549
    throw err;
    ^

Error: Cannot find module '/home/admin1/Desktop/Work/Search Improve/Latex/latex_simialarity/preprocess_latex.js'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
None

但是当我把里面的脚本main/latex_utils/scripts/preprocessing称为:

from filter_tokenize import *

它运行得很好。问题是行中的这个相对路径

cmd = "cat %s | node ./preprocess_latex.js %s > %s "%(temp_file, kind, output_file)

我不知道该怎么做。有人可以帮忙吗?

请参阅此原始 Repo 的第 68 行和代码

标签: javascriptpythonnode.jsbashshell

解决方案


发生这种情况是因为您在https://github.com/harvardnlp/im2markup/blob/master/scripts/preprocessing/preprocess_latex.js中的 JS 代码使用相对路径,因此一旦将其移动到不同的文件夹,您将看到错误。

如果您只想移动文件,则需要更改相对路径,例如 require("../../third_party/katex/katex.js")-> require("../third_party/katex/katex.js")如果您将其上移一级。


推荐阅读