from google.cloud import language
from google.cloud import language_v1
import six
import pandas as pd
data = pd.read_csv("all-data.csv", encoding="ISO-8859-1")
data.columns = ['sentiment', 'text']
data2 = data.columns[1]
#This format data.loc[1].at['text'] can be used to access the data at that element
def analyze_text_sentiment(text):
client = language.LanguageServiceClient()
document = language.Document(content=text, type_=language.Document.Type.PLAIN_TEXT)
response = client.analyze_sentiment(document=document)
sentiment = response.document_sentiment
results = dict(
text=text,
score=f"{sentiment.score:.1%}",
magnitude=f"{sentiment.magnitude:.1%}",
)
for k, v in results.items():
print(f"{k:10}: {v}")
def sample_analyze_sentiment(content):
client = language_v1.LanguageServiceClient()
# content = 'Your text to analyze, e.g. Hello, world!'
if isinstance(content, six.binary_type):
content = content.decode("utf-8")
type_ = language_v1.Document.Type.PLAIN_TEXT
document = {"type_": type_, "content": content}
response = client.analyze_sentiment(request={"document": document})
sentiment = response.document_sentiment
print("Score: {}".format(sentiment.score))
print("Magnitude: {}".format(sentiment.magnitude))
text = "During the last quarter of 2016, the loans outpaced leases for the first time. Despite the significant market share, SolarCity’s solar panel installations during the first quarter declined around 39% year over year during the first quarter of 2017. The company has revealed sleek new solar roof panels that it claims are more efficient and cost-competitive compared to regular roofing products."
analyze_text_sentiment(text)
#for x in range(20):
# val = data.loc[x].at['text'] #stores the data at that point in val
# analyze_text_sentiment(val)
#text = "Guido van Rossum is great!"
#analyze_text_sentiment(text)
cat sentiment-out.txt
SolarCity's Dominance in U.S. Residential Market
SolarCity commands a significant 41% share of the residential solar installation market, and the company also operates in the commercial and utilities space. The company's business model of offering to lease solar panels with no upfront costs has helped it gain a big slice of the residential solar market in the past.
Score: 0.30000001192092896, Magnitude: 0.699999988079071
However, the model has put a burden on the company to constantly raise capital from investors to cover the upfront cost of installing the leased solar systems. Following its purchase by Tesla late last year and owing to the solar market dynamics, the company has moved on to allow consumers to take out loans and own their solar arrays outright. It now puts SolarCity in line with practices of other industry players.
Score: -0.20000000298023224, Magnitude: 1.2000000476837158
During the last quarter of 2016, the loans outpaced leases for the first time. Despite the significant market share, SolarCity’s solar panel installations during the first quarter declined around 39% year over year during the first quarter of 2017. The company has revealed sleek new solar roof panels that it claims are more efficient and cost-competitive compared to regular roofing products.
Score: 0.20000000298023224, Magnitude: 1.2000000476837158
First Solar has done things differently since its inception and managed to cut its own road to success. Right from developing cadmium-telluride (CdTe) based panels to developing advanced grid integration, plant control, forecasting and energy scheduling capabilities, the company has been able to sustain and succeed in the highly competitive solar energy market for decades.
Score: 0.5, Magnitude: 1.100000023841858
First Solar is hopeful about its new-age Series 6 solar modules and is on course to ramp up the production of its new 400-watt-plus form factor, which will keep it competitive in the coming times, reports GTM. The company operates as a vertically integrated business, which puts it in control of everything starting from sourcing of raw materials to recycling of solar panels.
Score: 0.30000001192092896, Magnitude: 0.6000000238418579
admin@instance-2:/home/public/Sentiment-gcp-v1/file_/home/danteoconnor3$ cat version-2.py
from google.cloud import language
from google.cloud import language_v1
import six
import pandas as pd
# for python3
import sys
data = pd.read_excel("solar-city.xlsx")
data.columns = ['text']
def analyze_sentiment(content):
client = language_v1.LanguageServiceClient()
if isinstance(content, six.binary_type):
content = content.decode("utf-8")
type_ = language_v1.Document.Type.PLAIN_TEXT
document = {"type_": type_, "content": content}
response = client.analyze_sentiment(request={"document": document})
sentiment = response.document_sentiment
print("Score: {}, Magnitude: {}".format(sentiment.score, sentiment.magnitude))
#print("Magnitude: {}".format(sentiment.magnitude))
print(data.shape)
with open('sentiment-out.txt', 'w') as sys.stdout:
for x in range(32):
val = data.loc[x].at['text'] #stores the data at that point in val
print(val)
analyze_sentiment(val)
print('\n')
#f= open("sentiment-out.txt","w+")
#for x in range(1,32):
# val = data.loc[x].at['text'] #stores the data at that point in val
# f.write(val)
# f.write(str(analyze_sentiment(val)))
#f.write(analyze_sentiment(val))
#print('\n')
No comments:
Post a Comment