B4J Question jshell WriteToInputStream does not get stdout

xulihang

Active Member
Licensed User
Longtime User
Hi there,

I am trying to interact with Fiskmo Machine Translation Engine through jshell.

Basically, I run the Fiskmo MT, input some text and I can get the translated text:

B4X:
spm_encode.exe --model source.spm | marian.exe decode --log-level=warn -c decoder.yml
我爱 中国!
▁I ▁love ▁China !

This works under Cygwin's Terminal. But when I am trying to use jShell, I cannot get the stdout.

Here is my code:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
    TextArea1.Text="我爱中国!"
    RunMarian

End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub RunMarian
    sh.Initialize("sh","cmd",Array("/c","StartSentencePieceMtPipe.bat","C:\Users\xulihang\AppData\Local\fiskmo\models\zh-en\opus-2020-07-17-zh2en"))
    sh.InputStreamEnabled=True
    sh.Encoding="UTF8"
    sh.WorkingDirectory="E:\Download\FiskmoMTEngine"
    sh.RunWithOutputEvents(-1)
    'sh.Run(-1)
End Sub

Sub Button1_MouseClicked (EventData As MouseEvent)
    sh.WriteToInputStream(TextArea1.Text.GetBytes("UTF8"))
End Sub

Sub sh_StdOut (Buffer() As Byte, Length As Int)
    Log("out")
    Dim translation As String=BytesToString(Buffer,0,Length,"UTF8").Replace(" ","").Replace("▁"," ").Trim
    Log(translation)
    TextArea2.Text=translation
End Sub

Sub sh_StdErr (Buffer() As Byte, Length As Int)
    Log("err")
    Log(BytesToString(Buffer,0,Length,"UTF8"))
End Sub

Here is how to test this:

1. Download and install Fiskmo MT Engine from here: https://github.com/Helsinki-NLP/OPUS-CAT
2. Install one machine translation model like Chinese to English which can be downloaded from here: https://object.pouta.csc.fi/Tatoeba-MT-models/zho-eng/opus-2020-07-17.zip
3. Use my test code and modify the path.
 

xulihang

Active Member
Licensed User
Longtime User
I think that the batch file is using pipe may be the reason.
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
C:
#include<stdio.h>

int main(){
    int i=0;
    while(i<5)
    {
        i=i+1;
        float f;
        printf("Enter a number: ");
        scanf("%f",&f);
        printf("Value = %f\n", f);
        
    }
    
    return 0;
}

I wrote a simple test program in C which receives an input and prints the input.

I found the StdOut event is not raised until the program exits
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
I tested again. I can get newdata now. But for the c program I write, I can only get err. Strange.
 
Upvote 0
Top