You might be right, but I believe the main sentiment still holds about hypocritical support of one vs the other.
#!/usr/bin/env python3
import nltk
import nltk.sentiment.vader
import json
import urllib.request
companies = [
"apple",
"google",
"microsoft",
"amazon",
"facebook",
]
sample_size = 10
def get_json(url):
request = urllib.request.Request(url)
request.add_header("User-Agent", "Python")
return json.load(urllib.request.urlopen(request))
def allcomments(json):
comment = json["text"]
comments = comment if comment else ""
for child in json["children"]:
comments += allcomments(child)
return comments
if __name__ == "__main__":
for company in companies:
sentiments = []
print(company)
for item in get_json("https://hn.algolia.com/api/v1/search?query=" + company + "&tags=story")["hits"][:sample_size]:
tokens = nltk.tokenize.sent_tokenize(allcomments(get_json("https://hn.algolia.com/api/v1/items/" + item["objectID"])))
sia = nltk.sentiment.vader.SentimentIntensityAnalyzer()
scores = sum([sia.polarity_scores(token)["compound"] for token in tokens])
sentiment = scores / len(tokens)
print("\t" + item["title"] + ": " + str(sentiment))
sentiments.append(sentiment)
print("Average: " + str(sum(sentiments) / len(sentiments)))
Apple gets low scores because of China, mainly. Plus they lack the many open source projects that buoy up companies like Microsoft.