
import json, requests
import six

class StanfordCoreNLP:

	def __init__(self, server_url):
		self.server_url = server_url

	def annotate(self, text, properties={}):
		assert isinstance(text, six.string_types),"text must be a string"
		assert isinstance(properties, dict)

		data = text.encode('utf8')
		r = requests.post(
			self.server_url, params={
				'properties': str(properties)
			}, data=data, headers={'Connection': 'close'})
		
		assert 'outputFormat' in properties and properties['outputFormat'] == 'json'
		output = json.loads(r.text, encoding='utf-8', strict=False)

		return output



import json, requests
import six

class StanfordCoreNLP:

	useSessions = True
	sessions = {}

	def __init__(self, server_url):
		self.server_url = server_url
		if StanfordCoreNLP.useSessions:
			if not server_url in StanfordCoreNLP.sessions:
				StanfordCoreNLP.sessions[server_url] = requests.Session()
			self.session = StanfordCoreNLP.sessions[server_url]

	def annotate(self, text, properties={}):
		assert isinstance(text, six.string_types),"text must be a string, received %s" % (str(type(text)))
		assert isinstance(properties, dict)
		#print('X',text)

		data = text.encode('utf8')
		
		if StanfordCoreNLP.useSessions:
			r = self.session.post(
				self.server_url, params={
					'properties': str(properties)
				}, data=data, headers={'Connection': 'close'})
		else:
			r = requests.post(
				self.server_url, params={
					'properties': str(properties)
				}, data=data, headers={'Connection': 'close'})
		
		assert 'outputFormat' in properties and properties['outputFormat'] == 'json'
		output = json.loads(r.text, encoding='utf-8', strict=False)

		return output


Add experimental sessions code for CoreNLP requests
Add experimental sessions code for CoreNLP requests

  
  import json, requests
  import six
  
  class StanfordCoreNLP:
  
+ 	useSessions = True
+ 	sessions = {}
+ 
  	def __init__(self, server_url):
  		self.server_url = server_url
+ 		if StanfordCoreNLP.useSessions:
+ 			if not server_url in StanfordCoreNLP.sessions:
+ 				StanfordCoreNLP.sessions[server_url] = requests.Session()
+ 			self.session = StanfordCoreNLP.sessions[server_url]
  
  	def annotate(self, text, properties={}):
- 		assert isinstance(text, six.string_types),"text must be a string"
+ 		assert isinstance(text, six.string_types),"text must be a string, received %s" % (str(type(text)))
  		assert isinstance(properties, dict)
+ 		#print('X',text)
  
  		data = text.encode('utf8')
- 		r = requests.post(
+ 		
+ 		if StanfordCoreNLP.useSessions:
+ 			r = self.session.post(
- 			self.server_url, params={
+ 				self.server_url, params={
- 				'properties': str(properties)
+ 					'properties': str(properties)
- 			}, data=data, headers={'Connection': 'close'})
+ 				}, data=data, headers={'Connection': 'close'})
+ 		else:
+ 			r = requests.post(
+ 				self.server_url, params={
+ 					'properties': str(properties)
+ 				}, data=data, headers={'Connection': 'close'})
  		
  		assert 'outputFormat' in properties and properties['outputFormat'] == 'json'
  		output = json.loads(r.text, encoding='utf-8', strict=False)
  
  		return output
  
  
Add experimental sessions code for CoreNLP requests
## Code Before:

import json, requests
import six

class StanfordCoreNLP:

	def __init__(self, server_url):
		self.server_url = server_url

	def annotate(self, text, properties={}):
		assert isinstance(text, six.string_types),"text must be a string"
		assert isinstance(properties, dict)

		data = text.encode('utf8')
		r = requests.post(
			self.server_url, params={
				'properties': str(properties)
			}, data=data, headers={'Connection': 'close'})
		
		assert 'outputFormat' in properties and properties['outputFormat'] == 'json'
		output = json.loads(r.text, encoding='utf-8', strict=False)

		return output


## Instruction:
Add experimental sessions code for CoreNLP requests
## Code After:

import json, requests
import six

class StanfordCoreNLP:

	useSessions = True
	sessions = {}

	def __init__(self, server_url):
		self.server_url = server_url
		if StanfordCoreNLP.useSessions:
			if not server_url in StanfordCoreNLP.sessions:
				StanfordCoreNLP.sessions[server_url] = requests.Session()
			self.session = StanfordCoreNLP.sessions[server_url]

	def annotate(self, text, properties={}):
		assert isinstance(text, six.string_types),"text must be a string, received %s" % (str(type(text)))
		assert isinstance(properties, dict)
		#print('X',text)

		data = text.encode('utf8')
		
		if StanfordCoreNLP.useSessions:
			r = self.session.post(
				self.server_url, params={
					'properties': str(properties)
				}, data=data, headers={'Connection': 'close'})
		else:
			r = requests.post(
				self.server_url, params={
					'properties': str(properties)
				}, data=data, headers={'Connection': 'close'})
		
		assert 'outputFormat' in properties and properties['outputFormat'] == 'json'
		output = json.loads(r.text, encoding='utf-8', strict=False)

		return output

