import subprocess import shlex def RateSentiment(sentiString): #open a subprocess using shlex to get the command line string into the correct args list format #Modify the location of SentiStrength.jar and D:/SentiStrength_Data/ if necessary p = subprocess.Popen(shlex.split("java -jar D:/Downloads/SentiStrength.jar stdin sentidata D:/SentiStrength_Data/"),stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE) #communicate via stdin the string to be rated. Note that all spaces are replaced with + #Can't send string in Python 3, must send bytes b = bytes(sentiString.replace(" ","+"), 'utf-8') stdout_byte, stderr_text = p.communicate(b) #convert from byte stdout_text = stdout_byte.decode("utf-8") #replace the tab with a space between the positive and negative ratings. e.g. 1 -5 -> 1 -5 stdout_text = stdout_text.rstrip().replace("\t"," ") return stdout_text + " " + sentiString #An example to illustrate calling the process. print(RateSentiment("lovely day")) #The above is OK for one text but inefficient to repeatedly call for many texts. Try instead: # either modify the above to submit a file # or modify the above to send multiple lines through multiple calls of p.communicate(b)