"""
Wrapper around antske/coref_draft.
Input should be NAF files parsed by alpino (e.g. alpinonerc)
"""

from KafNafParserPy import KafNafParser
from multisieve_coreference import process_coreference
from io import BytesIO
import logging
from nlpipe.module import Module

log = logging.getLogger(__name__)


class CorefNL(Module):
    name = "corefnl"

    def process(self, text):
        inb = BytesIO(text.encode("utf-8"))
        naf = KafNafParser(inb)
        naf = process_coreference(naf)
        b = BytesIO()
        naf.dump(b)
        return b.getvalue().decode("utf-8")

CorefNL.register()

@@ -102,126 +102,8 @@
 %22%22%0A%0A
-from KafNafParserPy import KafNafParser%0Afrom multisieve_coreference import process_coreference%0Afrom io import BytesIO%0A
 impo
@@ -145,16 +145,44 @@
 t Module
+%0Aimport subprocess%0Aimport os
 %0A%0Alog = 
@@ -268,126 +268,511 @@
 def 
-process(self, text):%0A        inb = BytesIO(text.encode(%22utf-8%22))%0A        naf = KafNafParser(inb)%0A        naf = process
+check_status(self):%0A        if 'COREF_HOME' not in os.environ:%0A            raise Exception(%22COREF_HOME not set!%22)%0A        coref_home = os.environ%5B'COREF_HOME'%5D%0A        if not os.path.exists(coref_home):%0A            raise Exception(%22Coref not found at COREF_HOME=%7Bcoref_home%7D%22.format(**locals()))%0A            %0A    def process(self, text):%0A        coref_home = os.environ%5B'COREF_HOME'%5D%0A        command = %5Bos.path.join(coref_home, %22env/bin/python%22),%0A                   %22-m%22, %22multisieve_coreference.resolve
 _cor
@@ -783,13 +783,11 @@
 ence
-(naf)
+%22%5D%0A
 %0A   
@@ -795,86 +795,412 @@
     
-b = BytesIO()%0A        naf.dump(b)%0A        return b.getvalue().decode(%22utf-8%22)%0A
+p = subprocess.Popen(command, shell=False, stdin=subprocess.PIPE,%0A                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)%0A%0A        out, err = %5Bx.decode(%22utf-8%22) for x in p.communicate(text.encode(%22utf-8%22))%5D%0A        if err:%0A            raise Exception(err)%0A        if not out:%0A            raise Exception(%22No output from coreference and no error message%22)%0A%0A        return out%0A        
 %0ACor

Change coref to external call