0

I am trying to use SENNA with python. I get the following IOError:

Traceback (most recent call last):
  File "C:\Python27\senna_test.py", line 18, in <module>
    tagged = StringIO(p.communicate(sentence)[0])
  File "C:\Python27\lib\subprocess.py", line 479, in communicate
    return self._communicate(input)
  File "C:\Python27\lib\subprocess.py", line 718, in _communicate
    self.stdin.write(input)
IOError: [Errno 22] Invalid argument

My code is as follows:

import os
import csv
from StringIO import StringIO
import subprocess as sp
from nltk.draw.tree import Tree, TreeWidget
from nltk.draw.util import CanvasFrame

senna_path="C:/Python27/senna/"
sentence = 'My brother has a dog'

# read senna output
p = sp.Popen(['blabla', '-path',  senna_path],
             executable=os.path.join(senna_path, 'senna-win32.exe'),
             stdin=sp.PIPE,
             stdout=sp.PIPE,
             stderr=sp.PIPE)
tagged = StringIO(p.communicate(sentence)[0])
table = csv.reader(tagged, dialect='excel-tab')
martineau
  • 119,623
  • 25
  • 170
  • 301
Anu
  • 119
  • 2
  • 11
  • 1
    Did you try `'My brother has a dog\n'` ? Also, why isn't your command `['senna-Win32.exe', '-path', senna_path]` ? Can you explain the `blabla` and the executable argument ? – Gribouillis Dec 11 '16 at 07:27
  • Hi, I am trying to use the following github code: https://gist.github.com/dedan/1275662 – Anu Dec 11 '16 at 07:34
  • Then perhaps the author of the gist has the answer. The command to pass to `sp.Popen()` is a command that works in a Cmd console. Open a cmd console and call the senna program until it works with your example sentence. Once that works, it will work in `sp.Popen()`. – Gribouillis Dec 11 '16 at 07:42
  • @Gribouillis: The meaning of `blabla` can be determined by reading the [`Popen`](https://docs.python.org/2.7/library/subprocess.html#popen-constructor) documentation. See the paragraph that starts "The _executable_ argument specifies a...". – martineau Dec 11 '16 at 22:01
  • Anu: Actually, the `'blabla'` _could_ be causing the problem—perhaps because the git code looks like it was written for OSX, which might be why it doesn't work on Windows. Try changing the call to `p = sp.Popen([os.path.join(senna_path, 'senna-win32.exe'), '-path', senna_path], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)`. – martineau Dec 11 '16 at 22:07
  • @martineau Still getting the same error – Anu Dec 12 '16 at 01:59
  • Anu: Oh, well, was worth a try anyway. Given that, I suggest you follow @Gribouillis' advice and see if you can determine the proper way to call `senna` from a console command line because doing so will likely tell you what needs to be passed as arguments in the `Popen` call. With luck there's some `senna` documentation you can consult... I also suggest you avoid using the `executable=` argument if possible—it shouldn't be required for this usage. – martineau Dec 12 '16 at 02:38
  • P.S. Are you sure there's nothing following the `IOError: [Errno 22] Invalid argument` in the traceback, such as some indication of _which_ argument is invalid? – martineau Dec 12 '16 at 02:49
  • @martineau Now i am getting this error instead. WindowsError: [Error 2] The system cannot find the file specified – Anu Dec 12 '16 at 03:19
  • That's a clue. Try using `senna_path="C:/Python27/senna"` and again with `senna_path=r"C:\Python27\senna"` if that doesn't help. – martineau Dec 12 '16 at 15:29
  • @martineau Still getting the same original error. Is there any problem with the communicate argument? Is the main process exiting first – Anu Dec 12 '16 at 15:58
  • Not sure if the end of your last comment is a statement or a question. Try using `sentence = b'My brother has a dog'`. – martineau Dec 12 '16 at 16:15

1 Answers1

0

instead you can use this approach

Download senna from https://ronan.collobert.com/senna/download.html

if you are using windows then:

python run .exe app with argument

use senna-win32.exe directly

import subprocess
myinput = open('in.txt')
myoutput = open('out.txt', 'w')
p = subprocess.Popen('senna-win32.exe', stdin=myinput, stdout=myoutput)
p.wait()
myoutput.flush()

Now parse out.txt to get your results.

Saurabh Yadav
  • 365
  • 4
  • 13