2016-04-27 55 views
0

我想在Haskell中編寫一個程序,它將輸入作爲一串句子,調用一個帶有該輸入的JavaScript文件,並將該javascript文件的輸出作爲輸出返回Haskell文件。現在,javascript文件的輸出不會被打印。目前尚不清楚JavaScript文件是否被調用。Haskell調用Node.js文件不工作

這是在Haskell腳本:

main :: IO() 
main = 
    do 
     putStrLn "Give me the paragraphs \n" 
     paragraphs <- getLine 
     output <- readCreateProcess (shell "node try2.js") paragraphs 
     putStrLn output 

腳本在Node.js的期望的輸出是toplines:

var lexrank = require('./lexrank'); 
const readline = require('readline'); 

const rl = readline.createInterface({ 
    input: process.stdin, 
    output: process.stdout 
}); 

rl.question('Hi', (answer) => { 

    var originalText = answer; 

    var topLines = lexrank.summarize(originalText, 5, function (err, toplines, text) { 
     if (err) { 
     console.log(err); 
     } 

     rl.write(toplines); 
     // console.log(toplines); 

     }); 

    rl.close(); 
}); 

我猜我的做stdin的方式有一些問題。我是新來Node.js加載

+1

難道你讓你的問題更具體。它是Haskell程序嗎?這是否適用於一個簡單的地獄世界js程序。你的計劃是你期望的嗎? – epsilonhalbe

+0

您可能想查看[process.stdin](https://nodejs.org/api/process.html#process_process_stdin)而不是readline。如果你想在stdout中取回東西,你可以通過pipe到'process.stdout',或者'process.stdout.write'或者只是'console.log()',如果你想要一個快速的解決方案。 – dvlsg

+0

究竟是不是按預期工作? – Kwarrtz

回答

0

與你有Haskell的程序的問題是,段落不是輸入的線路,只是一個字符串,所以修復可以追加一個換行符的問題,是這樣的:

output <- readCreateProcess (shell "node try2.js") $ paragraphs ++ "\n" 

爲了找到這個問題,我試着用墊片更換question

rl.question = function(prompt, cb) { 
    rl.on('line', function(thing) { 
    console.log(prompt); 
    cb(thing); 
    }) 
} 

和起作用的,所以我知道這是事做如何處理question標準輸入。所以在此之後,我嘗試追加一個換行符,並且工作。這意味着question需要一個「行」的輸入,不只是任何字符串,不像on('line'),奇怪的是。

+0

我嘗試在代碼中添加「\ n」,但它仍然沒有打印任何內容。你如何檢查JavaScript是否收到任何東西? – user3700129

1

我花了很長的時間,但下面的代碼工作:

哈斯克爾文件:

import System.Process 

main :: IO() 
main = 
    do 
     putStrLn "Give me the paragraphs \n" 
     paragraphs <- getLine 
     output <- readCreateProcess (shell "node lexrankReceiver.js") (paragraphs ++ "\n") 
     putStrLn output 

文件的NodeJS:

// Getting this to work took almost a full day. Javascript gets really freaky 
// when using it on terminal. 


/* Import necessary modules. */ 
var lexrank = require('./Lexrank/lexrank.js'); 
const readline = require('readline'); 
// var Type = require('type-of-is'); 
// var utf8 = require('utf8'); 


// Create readline interface, which needs to be closed in the end. 
const rl = readline.createInterface({ 
    input: process.stdin, 
    output: process.stdout 
}); 

// Set stdin and stdout to be encoded in utf8. Haskell passes string as basic 
// 8-bit unsigned integer array. The output also needs to be encoded so that 
// Haskell can read them 
process.stdin.setEncoding('utf8'); 
process.stdout.setEncoding('utf8'); 

// If a string is readable, start reading. 
process.stdin.on('readable',() => { 
    var chunk = process.stdin.read(); 

    if (chunk !== null) { 

    var originalText = chunk; 

    var topLines = lexrank.summarize(originalText, 5, function (err, toplines, text) { 
     if (err) { 
     console.log(err); 
     } 

     // Loop through the result to form a new paragraph consisted of most 
     // important sentences in ascending order. Had to split the 0 index and 
     // the rest indices otherwise the first thing in newParagraphs will be 
     // undefined. 
     var newParagraphs = (toplines[0])['text']; 

     for (var i = 1; i < toplines.length; i++) { 
     newParagraphs += (toplines[i])['text']; 
     } 

     console.log(newParagraphs); 

    }); 
    } 
}); 

// After the output is finished, set end of file. 
// TODO: write a handler for end of writing output. 
process.stdin.on('end',() => { 
    process.stdout.write('\n'); 
}); 

// It is incredibly important to close readline. Otherwise, input doesn't 
// get sent out. 
rl.close();