javascript - 如何绑定绝对路径而不是相对于其中使用 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)
我不知道该怎么做。有人可以帮忙吗?
解决方案
发生这种情况是因为您在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")
如果您将其上移一级。
推荐阅读
- sql - 如何将字符串中的多个变量传递到查询中
- javascript - 我无法从表单返回正确的对象数组
- laravel - 如何导入带有关系ID的excel文件?
- regex - 正则表达式用连续数字替换所有字符实例?
- html - 带条件的Html图片媒体查询
- python - 根据小数点后的值从 Pandas DataFrame 中删除行
- javascript - Mirage JS 向响应中添加分页数据
- sql - 如何比较sqlite中的版本字符串?
- c# - 我应该使用 OnAfterRenderAsync 而不是 OnInitializedAsync 吗?
- html - 模态文本区域编辑光标到最后一行