@@ -29,28 +29,35 @@ sourceFiles.forEach(file => {
29
29
const mdPath = path . join ( __dirname , 'md' , newName + '.md' ) ;
30
30
31
31
// 1. pdf to xml
32
- console . log ( `Transforming ${ newName } . pdf to ${ newName } .xml ...` ) ;
32
+ console . log ( `Transforming pdf to xml ( ${ newName } ) ...` ) ;
33
33
child_process . execSync ( `python ${ pythonTool } -o ${ xmlPath } ${ pdfPath } ` ) ;
34
34
35
35
// 2. xml to json
36
- console . log ( `Transforming ${ newName } . xml to ${ newName } .json ...` ) ;
36
+ console . log ( `Transforming xml to json ( ${ newName } ) ...` ) ;
37
37
child_process . execSync ( `node ./scripts/xml2json.js ${ xmlPath } ${ jsonPath } ` ) ;
38
38
39
39
// 3. json to txt
40
- console . log ( `Transforming ${ newName } . json to ${ newName } .txt ...` ) ;
40
+ console . log ( `Transforming json to txt ( ${ newName } ) ...` ) ;
41
41
child_process . execSync ( `node ./scripts/json2txt.js ${ jsonPath } ${ txtPath } ` ) ;
42
42
43
- // 4. 提取摘要和结论
44
- console . log ( `Extracting structural information from ${ newName } . txt to ${ newName } .md ...` ) ;
43
+ // 4. extract useful information
44
+ console . log ( `Extracting structural information from txt to md ( ${ newName } ) ...` ) ;
45
45
const info = child_process . execSync ( `node ./scripts/txt2md.js ${ txtPath } ${ mdPath } ` , { encoding : 'utf8' } ) ;
46
46
console . log ( info ) ;
47
47
48
48
// 5. translation,两种写法均可
49
- // 要实时显示程序输出,得用异步版的.spawn()
50
- // 但异步版的.spawn()堵塞不住主进程,如果有多个文件需要翻译,循环中,主进程继续运行到.spawn()就会再开一个不堵塞的异步子进程。这样就可以出现多个子进程同时访问服务器,虽然翻译地快,但容易被封杀,特别是文件多的时候。
49
+
50
+ /**
51
+ * 要实时显示程序输出,得用异步版的.spawn(),但异步版的.spawn()堵塞不住主进程。
52
+ * 如果有多个文件需要处理,程序便会继续进行下一次迭代,运行到下一个.spawn()时,就会再开一个异步子进程。如果前面文件的翻译工作尚未完成,便会有多个子进程同时访问服务器。
53
+ * 好处是并行翻译多个文件,翻译得快;坏处是每个子进程的访问频率虽然被限定了,但多个子进程同时访问事实上突破了设定的频率,容易被封杀,特别是文件多的时候。
54
+ */
55
+
56
+ // (1). 堵塞主进程的同步版,同一时间段只有一个子进程,只翻译一个文件
51
57
// const output = child_process.execSync(`node ./scripts/translate.js ${mdPath}`, { encoding: 'utf8' });
52
58
// console.log(output);
53
59
60
+ // (2). 不堵塞主进程的异步版,同一时间段可能存在多个子进程,在翻译多个文件
54
61
const stream = child_process . spawn ( 'node' , [ './scripts/translate.js' , mdPath ] , { shell : true } ) ;
55
62
stream . stdout . on ( 'data' , data => {
56
63
console . log ( `Translating: ${ data } ` ) ;
0 commit comments