Skip to content

Commit

Permalink
convert python strings to java string first + some error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
khashab2 committed May 16, 2018
1 parent dd6924b commit 3090fb9
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions ccg_nlpy/local_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,46 @@

logger = logging.getLogger(__name__)


class LocalPipeline(PipelineBase):
def __init__(self):
"""
Constructor to set up local pipeline
@param: file_name, the file name of the custom config file
"""
super(LocalPipeline,self).__init__()
super(LocalPipeline, self).__init__()

self.PipelineFactory = None
self.pipeline = None
self.ProtobufSerializer = None

pipeline_config.log_current_config(self.config, False )
pipeline_config.log_current_config(self.config, False)

# set up JVM and load classes needed
try:
import jnius_config
jnius_config.add_options('-Xmx16G')
jnius_config.add_classpath(get_model_path()+'/*')
except:
logger.warn("Couldn't set JVM config; this might be because you're setting up the multiple instances of the local pipeline.")
jnius_config.add_classpath(get_model_path() + '/*')
except Exception as e:
logger.warning(
"Couldn't set JVM config; this might be because you're setting up the multiple instances of the local pipeline.")
logger.warning(str(e))
try:
from jnius import autoclass
self.PipelineFactory = autoclass('edu.illinois.cs.cogcomp.pipeline.main.PipelineFactory')
self.SerializationHelper = autoclass('edu.illinois.cs.cogcomp.core.utilities.SerializationHelper')
self.ProtobufSerializer = autoclass('edu.illinois.cs.cogcomp.core.utilities.protobuf.ProtobufSerializer')
self.Boolean = autoclass('java.lang.Boolean')
self.JString = autoclass('java.lang.String')
except Exception as e:
logger.error('Fail to load models, please check if your Java version is up to date.')
logger.error(str(e))
return None
self.pipeline = self.PipelineFactory.buildPipelineWithAllViews(self.Boolean(True))

logger.info("pipeline has been set up")


def call_server(self, text, views):
"""
Funtion to get preprocess text annotation from local pipeline
Expand All @@ -60,14 +64,21 @@ def call_server(self, text, views):
@return: raw text of the response from local pipeline
"""
view_list = views.split(',')
text_annotation = self.pipeline.createBasicTextAnnotation("", "", text)
text_annotation = self.pipeline.createBasicTextAnnotation(self.JString(""), self.JString(""),
self.JString(text))
for view in view_list:
self.pipeline.addView(text_annotation, view.strip())
#json = SerializationHelper.serializeToJson(text_annotation)
if (len(view.strip()) > 0):
try:
self.pipeline.addView(text_annotation, self.JString(view.strip()))
except Exception as e:
logger.error('Failed to add view ' + view.strip())
logger.error(str(e))

# json = SerializationHelper.serializeToJson(text_annotation)

path = os.path.expanduser('~') + "{0}.ccg_nlpy{0}".format(os.path.sep) + 'temp.temp'

self.ProtobufSerializer.writeToFile(text_annotation,path)
self.ProtobufSerializer.writeToFile(text_annotation, self.JString(path))
proto_data = None
with open(path, 'rb') as f:
proto_data = f.read()
Expand All @@ -77,4 +88,3 @@ def call_server(self, text, views):
proto_to_json = json_format.MessageToJson(message)

return proto_to_json

0 comments on commit 3090fb9

Please sign in to comment.