Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //installing core nlp and running it on port 9000
- wget http://nlp.stanford.edu/software/stanford-corenlp-full-2017-06-09.zip
- unzip stanford-corenlp-full-2017-06-09.zip
- cd stanford-corenlp-full-2017-06-09
- java -mx5g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -timeout 10000
- // install python module for nlp
- pip install pycorenlp
- //python code starts here
- import requests
- from pycorenlp import StanfordCoreNLP
- nlp = StanfordCoreNLP('http://localhost:9000')
- 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'}
- 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"
- from bs4 import BeautifulSoup
- response=requests.get(p_url,headers=headers)
- print response.status_code
- soup=BeautifulSoup(response.content,"html5lib")
- a=soup.find_all("div",{"class":"a-expander-content"})
- def do_stuff(data_string):
- res=nlp.annotate(data_string,
- properties={
- 'annotators': 'sentiment',
- 'outputFormat': 'json',
- 'timeout': 1000,
- })
- for s in res["sentences"]:
- print "%d: '%s': %s %s" % (
- s["index"],
- " ".join([t["word"] for t in s["tokens"]]),
- s["sentimentValue"], s["sentiment"])
- return True
- for i in a:
- try:
- comment_string=str(i.getText())
- # print comment_string
- # print type(comment_string),"text_type"
- do_stuff(comment_string)
- except:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement