from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
import sys
import random
import logging
try:
    from kqml import KQMLModule, KQMLPerformative, KQMLList
    have_kqml = True
except ImportError:
    KQMLModule = object
    have_kqml = False

logger = logging.getLogger('drum_reader')

class DrumReader(KQMLModule):
    def __init__(self, **kwargs):
        if not have_kqml:
            raise ImportError('Install the `pykqml` package to use ' +
                              'the DrumReader')
        self.to_read = kwargs.pop('to_read', None)
        super(DrumReader, self).__init__(name='DrumReader')
        self.msg_counter = random.randint(1, 100000)
        self.ready()
        self.extractions = []
        self.reply_counter = len(self.to_read)
        for text in self.to_read:
            self.read_text(text)

    def read_text(self, text):
        print('Reading %s' % text)
        msg_id = 'RT000%s' % self.msg_counter
        kqml_perf = _get_perf(text, msg_id)
        self.send(kqml_perf)
        self.msg_counter += 1

    def receive_reply(self, msg, content):
        extractions = content.gets(':extractions')
        self.extractions.append(extractions)
        self.reply_counter -= 1
        if self.reply_counter == 0:
            self.exit(0)

def _get_perf(text, msg_id):
    text = text.encode('utf-8')
    msg = KQMLPerformative('REQUEST')
    msg.set('receiver', 'DRUM')
    content = KQMLList('run-text')
    content.sets('text', text)
    msg.set('content', content)
    msg.set('reply-with', msg_id)
    return msg

if __name__ == '__main__':
    # NOTE: drum/bin/trips-drum needs to be running
    to_read = ['MEK phosphorylates ERK1.', 'BRAF phosphorylates MEK1.']
    dr = DrumReader(to_read=to_read)
    dr.start()

@@ -97,16 +97,26 @@
 ct, str%0A
+import os%0A
 import s
@@ -118,16 +118,16 @@
 ort sys%0A
-
 import r
@@ -373,16 +373,1282 @@
 odule):%0A
+    %22%22%22Agent which processes text through a local TRIPS/DRUM instance.%0A%0A    This class is implemented as a communicative agent which sends and receives%0A    KQML messages through a socket. It sends text (ideally in small blocks%0A    like one sentence at a time) to the running DRUM instance and receives%0A    extraction knowledge base (EKB) XML responses asynchronously through%0A    the socket. To install DRUM and its dependencies locally, follow%0A    instructions at: https://github.com/wdebeaum/drum%0A    Once installed, run %60drum/bin/trips-drum -nouser%60 to run DRUM without%0A    a GUI. Once DRUM is running, this class can be instantiated as%0A    %60dr = DrumReader(to_read=text_list)%60, at which point it attempts to%0A    connect to DRUM via the socket and send the texts for reading.%0A    Receiving responses can be started as %60dr.start()%60 which waits for%0A    responses from the reader and returns when all responses were received.%0A    Once finished, the list of EKB XML extractions can be accessed via%0A    %60dr.extractions%60.%0A%0A    Parameters%0A    ----------%0A    to_read : list%5Bstr%5D%0A        A list of text strings to read with DRUM.%0A%0A    Attributes%0A    ----------%0A    extractions : list%5Bstr%5D%0A        A list of EKB XML extractions corresponding to the input text list.%0A    %22%22%22%0A
     def 
@@ -2876,16 +2876,16 @@
 rn msg%0A%0A
+
 if __nam
@@ -2907,60 +2907,8 @@
 _':%0A
-    # NOTE: drum/bin/trips-drum needs to be running%0A
     

Document DRUM reader