def get_sentiment(data): sentiment_results = { 'pos': 0, 'neg': 0, } title = data.get('title') if title: if sentiment(title) == 'pos': sentiment_results['pos'] += 1 else: sentiment_results['neg'] += 1 comments = data.get('comments') if comments: for comment in comments: if sentiment(comment.get('message')) == 'pos': sentiment_results['pos'] += 1 else: sentiment_results['neg'] += 1 total = sentiment_results['pos'] + sentiment_results['neg'] if total: sentiment_results['pos'] /= total sentiment_results['neg'] /= total return sentiment_results
def trackingTweet(hashtag): r = api.request('statuses/filter', {'track': hashtag}) # Track by Hashtag only print('SUCCESS' if r.status_code == 200 else 'FAILURE') dicts_track = [] for i, item in enumerate(r): print(i, item['text'] if 'text' in item else item) print(item['created_at']) date_spilt = item['created_at'].split() dict_item_track = { "created_at": date_spilt[0] + ' ' + date_spilt[1] + ' ' + date_spilt[2], "id": item['id'], "text": item['text'], "hashtag": item['entities']['hashtags'], "user_id": item['user']['screen_name'], "user_name": item['user']['name'], "retweet_count": item['retweet_count'], "fav_count": item['favorite_count'], "sentiment": sentiment(item['text']), "sum": 0, "pos": 0, "neg": 0, "nat": 0 } dicts_track.append(dict_item_track) emp.insert({ "created_at": dicts_track[i]['created_at'], "id": dicts_track[i]['id'], "text": dicts_track[i]['text'], "hashtag": dicts_track[i]['hashtag'], "user_id": dicts_track[i]['user_id'], "user_name": dicts_track[i]['user_name'], "retweet_count": dicts_track[i]['retweet_count'], "fav_count": dicts_track[i]['fav_count'], "sentiment": sentiment(item['text']), "sum": 0, "pos": 0, "neg": 0, "nat": 0 }) with open('trackTwt.json', 'w') as outfile: json.dump(dicts_track, outfile, ensure_ascii=False) time.sleep(2)
def pythaiSentiment(sentence): stopword = stopwords.words('thai') for word in stopword: if word in sentence: sentence = sentence.replace(word, "") # Remove stopword result = sentiment(sentence) return result
def searchTweet(keywords): chk = os.stat("searchTwt.json").st_size == 0 r = api.request('search/tweets', {'q': keywords}) # Search by Hashtag only print('SUCCESS' if r.status_code == 200 else 'FAILURE') dicts_search = [] proc = [] for i, item in enumerate(r): print(i, item['text'] + '\n' if 'text' in item else item) date_spilt = item['created_at'].split() dict_item_search = { "created_at": date_spilt[0] + ' ' + date_spilt[1] + ' ' + date_spilt[2], "id": item['id'], "text": item['text'], "hashtag": item['entities']['hashtags'], "user_id": item['user']['screen_name'], "user_name": item['user']['name'], "retweet_count": item['retweet_count'], "fav_count": item['favorite_count'], "sentiment": sentiment(item['text']), "sum": 0, "pos": 0, "neg": 0, "nat": 0 } dicts_search.append(dict_item_search) emp.insert({ "created_at": dicts_search[i]['created_at'], "id": dicts_search[i]['id'], "text": dicts_search[i]['text'], "hashtag": dicts_search[i]['hashtag'], "user_id": dicts_search[i]['user_id'], "user_name": dicts_search[i]['user_name'], "retweet_count": dicts_search[i]['retweet_count'], "fav_count": dicts_search[i]['fav_count'], "sentiment": sentiment(item['text']), "sum": 0, "pos": 0, "neg": 0, "nat": 0 }) with open('searchTwt.json', 'w') as outfile: json.dump(dicts_search, outfile, ensure_ascii=False)
def main(party_name): unique_tweets = set() with open('csv/%s.csv' % party_name, encoding='utf8') as csvfile: data = csv.DictReader(csvfile, delimiter=';') for row in data: tweet = row['text'] tweet = remove_newline(tweet) tweet = keep_only_thai(tweet) tweet = convert_multiple_space_to_single(tweet) tweet = remove_start_space(tweet) unique_tweets.add(tweet) count = {'pos': 0, 'neg': 0} for tweet in unique_tweets: count[sentiment(tweet, engine='old')] += 1 return count
p.Lang = 'th' twint.run.Search(c) twint.run.Search(p) # def translate(x): # return TextBlob(x).translate(to="th") print("\nImporting Data\n") df = pd.read_csv("./data/raw_depressed_data.csv") pos = pd.read_csv("./data/raw_everyday_data.csv") print("\nDone Importing Data\n") label = [] analysed = 0 print("\n Sentiment Analysing the tweets\n") for tweets in df['tweet']: if sentiment(tweets) == 'pos': label.append(0) else: label.append(1) analysed = analysed + 1 if analysed % 100 == 0: print(f"Analysed {analysed} so far...") pos_label = [] for tweets in pos['tweet']: if sentiment(tweets) == 'pos': pos_label.append(0) else: pos_label.append(1) analysed = analysed + 1 if analysed % 100 == 0: