1

I am trying to input the text to the ots summerizer on Ubuntu. I am using the python2 subprocess library. But consistently getting the following error:

text = "the shards that cut me the deepest were the ones that intended to cut , obama to melania trump as first lady 's largest public appearance since the 2016 election - speaking in front of more than 8,000 people at the women 's foundation of colorado 's 30th anniversary celebration - and she touched on personal attacks that she faced again and again"
>>> sum = subprocess.check_output('ots --ratio=30 <' + text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 567, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
    raise child_exception
OSError: [Errno 36] File name too long

Even I have read that the ots takes input from the file or the stdin. Hence I tried to put input as:

sum = subprocess.check_output(['echo',text,'> stdin'])

But getting the following output:

"the shards that cut me the deepest were the ones that intended to cut , obama to melania trump as first lady 's largest public appearance since the 2016 election - speaking in front of more than 8,000 people at the women 's foundation of colorado 's 30th anniversary celebration - and she touched on personal attacks that she faced again and again > stdin\n"

But it is not what I am trying to achieve. I just want to input the text to the ots library using the subprocess of python. Is theer a way to do that and get the summary from the ots faster? Kindly, help me.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • `'ots --ratio=30 <' + text` uses the string in `text` as a file name to read from. I don't know `ots`, maybe it has a way to accept a string from the command line. If not, you could pass it a command line string as if it were coming from a file by using a [here string](http://tldp.org/LDP/abs/html/x17837.html): `'ots --ratio=30 <<<' + text`, but you'll have to tell `subprocess` to use `bash` instead of `sh` since `sh` doesn't support here strings. – PM 2Ring Jul 27 '17 at 09:32

1 Answers1

0

I believe you need the | pipe operator.

sum = subprocess.check_output('echo {} | ots --ratio=30'.format(text), shell=True)

The < symbol in bash is used take input from another stream.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
cs95
  • 379,657
  • 97
  • 704
  • 746
  • getting the following error: `Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/subprocess.py", line 567, in check_output process = Popen(stdout=PIPE, *popenargs, **kwargs) File "/usr/lib/python2.7/subprocess.py", line 711, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory ` – Jaffer Wilson Jul 27 '17 at 10:07
  • 1
    @JafferWilson Try now? I think I missed `shell=True` – cs95 Jul 27 '17 at 10:11