0

I am trying to write a program in Haskell that takes in the input as a string of sentences, calls a javascript file with that input, and return the output of that javascript file as the output of the Haskell file. Right now, the output of the javascript file is not printed. It is not clear whether javascript file is called or not.

Here is the script in Haskell:

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

Script in Node.js. The desired output is 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();
});

I am guessing there is some problem with my way of doing stdin. I am new to Node.js

3
  • 1
    Could you make your problem more concrete. Is it the Haskell program? Does this work with a simple hell world js program. Does your is program what you expect it to do? Commented Apr 27, 2016 at 22:57
  • You probably want to take a look at process.stdin instead of readline. If you want to get stuff back in stdout, you can either pipe to process.stdout, or process.stdout.write or just console.log() if you wanted a quick solution. Commented Apr 27, 2016 at 22:58
  • What exactly is not working as expected? Commented Apr 27, 2016 at 23:26

2 Answers 2

1

It took me a really long time but following code works:

Haskell File:

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 File:

// 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();
Sign up to request clarification or add additional context in comments.

Comments

0

The problem with the Haskell program you have is that paragraphs is not a line of input, just a string, so to fix the issue you can append a newline, something like:

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

To find this issue I tried replacing question with a shim:

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

And that worked, so I knew it was something to do with how question handles stdin. So after this I tried appending a newline and that worked. Which means question requires a 'line' of input, not just any string, unlike on('line'), oddly enough.

1 Comment

I tried adding "\n" to the code but it still doesn't print anything. How do you check if the javascript received anything?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.