Advertisement
galactus03

somya-core-nlp

Sep 16th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. //installing core nlp and running it on port 9000
  2. wget http://nlp.stanford.edu/software/stanford-corenlp-full-2017-06-09.zip
  3. unzip stanford-corenlp-full-2017-06-09.zip
  4. cd stanford-corenlp-full-2017-06-09
  5. java -mx5g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -timeout 10000
  6.  
  7. // install python module for nlp
  8. pip install pycorenlp
  9.  
  10. //python code starts here
  11. import requests
  12. from pycorenlp import StanfordCoreNLP
  13. nlp = StanfordCoreNLP('http://localhost:9000')
  14. headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
  15. p_url="https://www.amazon.in/gp/product/9311122521/ref=s9u_ri_gw_i1?ie=UTF8&pd_rd_i=9311122521&pd_rd_r=KDGDTZQ3BHQ7DSQZ0Z60&pd_rd_w=uU6yd&pd_rd_wg=KoSrl&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=&pf_rd_r=F1YP5DR0PCJS3DS99C2Z&pf_rd_t=36701&pf_rd_p=3c777619-829a-489c-8bef-17dc6cebf439&pf_rd_i=desktop"
  16. from bs4 import BeautifulSoup
  17. response=requests.get(p_url,headers=headers)
  18. print response.status_code
  19. soup=BeautifulSoup(response.content,"html5lib")
  20. a=soup.find_all("div",{"class":"a-expander-content"})
  21.  
  22. def do_stuff(data_string):
  23. res=nlp.annotate(data_string,
  24. properties={
  25. 'annotators': 'sentiment',
  26. 'outputFormat': 'json',
  27. 'timeout': 1000,
  28. })
  29. for s in res["sentences"]:
  30. print "%d: '%s': %s %s" % (
  31. s["index"],
  32. " ".join([t["word"] for t in s["tokens"]]),
  33. s["sentimentValue"], s["sentiment"])
  34. return True
  35.  
  36. for i in a:
  37. try:
  38. comment_string=str(i.getText())
  39. # print comment_string
  40. # print type(comment_string),"text_type"
  41. do_stuff(comment_string)
  42. except:
  43. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement