楼主: Nicolle
3755 43

Python Social Media Analytics [推广有奖]

巨擘

0%

还不是VIP/贵宾

-

TA的文库  其他...

Python(Must-Read Books)

SAS Programming

Must-Read Books

威望
16
论坛币
12402323 个
通用积分
1620.8615
学术水平
3305 点
热心指数
3329 点
信用等级
3095 点
经验
477211 点
帖子
23879
精华
91
在线时间
9878 小时
注册时间
2005-4-23
最后登录
2022-3-6

楼主
Nicolle 学生认证  发表于 2018-6-15 08:08:55 |只看作者 |坛友微信交流群|倒序 |AI写论文
提示: 作者被禁止或删除 内容自动屏蔽

本帖被以下文库推荐

沙发
cszcszcsz 发表于 2018-6-15 08:16:35 |只看作者 |坛友微信交流群
  1. Stream API
  2. In [ ]:
  3. from pymongo import MongoClient
  4. from requests_oauthlib import OAuth1

  5. client = MongoClient('mongodb://localhost:27017/')
  6. db = client['test']
  7. collection = db['test']

  8. url = 'https://stream.Twitter.com/1.1/statuses/filter.json'
  9. auth = OAuth1(consumer_key, consumer_secret, access_token, access_token_secret)

  10. pms = {'track' : 'premier league -filter:retweets AND -filter:replies', 'lang': 'en'}

  11. res = requests.post(url, auth=auth, params = pms, stream = True)

  12. for line in res.iter_lines():   
  13.     if line:
  14.         tweet = json.loads(line)
  15.         try:
  16.            collection.insert(tweet)
  17.         except:
  18.             pass
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

藤椅
suzhzh 发表于 2018-6-15 08:39:35 |只看作者 |坛友微信交流群
  1. Customized sentiment analysis
  2. In [75]:
  3. #You need to tag the dataset before
  4. dataset  = pd.read_pickle('tagged.pickle')

  5. classes = ['pos', 'neu', 'neg']
  6. train_data = dataset['final'][0:80]
  7. train_labels = dataset['label'][0:80]
  8. test_data = dataset['final'][80:96]
  9. test_labels = dataset['label'][80:96]
  10. train_data = list(train_data.apply(' '.join))
  11. test_data = list(test_data.apply(' '.join))
  12. In [77]:
  13. from sklearn.feature_extraction.text import TfidfVectorizer
  14. from sklearn.naive_bayes import MultinomialNB

  15. vectorizer = TfidfVectorizer(min_df=5, max_df = 0.8, sublinear_tf=True, use_idf=True)

  16. train_vectors = vectorizer.fit_transform(train_data)
  17. test_vectors = vectorizer.transform(test_data)

  18. ### Perform a logistic regression model, and fit with X and y

  19. nb = MultinomialNB()

  20. nb.fit(train_vectors, train_labels).score(test_vectors, test_labels)
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

板凳
suzhzh 发表于 2018-6-15 08:39:36 |只看作者 |坛友微信交流群
Many thanks

使用道具

报纸
HappyAndy_Lo 发表于 2018-6-15 08:48:27 |只看作者 |坛友微信交流群
  1. NER Recognition
  2. In [ ]:
  3. from nltk.tag import StanfordNERTagger
  4. from collections import Counter
  5. import numpy as np

  6. st = StanfordNERTagger('path_to_your_folder/english.all.3class.distsim.crf.ser.gz')
  7. st.tag(sentence.split())

  8. for r in tweets:
  9.     lst_tags = st.tag(r.split())

  10. for tup in lst_tags:
  11.     if(tup[1] != 'O'):
  12.         entities.append(tup)
  13. In [ ]:
  14. organizations = df_entities[df_entities['ner'].str.contains("ORGANIZATION")]

  15. cnt = Counter(organizations['word'])
  16. cnt.most_common(10)
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

地板
albertwishedu 发表于 2018-6-15 08:55:59 |只看作者 |坛友微信交流群
  1. Getting the data
  2. In [1]:
  3. def get_statistics(video_id):

  4.     url = "https://www.googleapis.com/youtube/v3/videos"
  5.     pms = {'key': api_key, 'id': video_id, 'part':'contentDetails,statistics'}
  6.     res = requests.get(url, params = pms)
  7.     data = res.json()

  8.     return(data)
  9. In [3]:
  10. def get_channel_videos(channel_id='UC-2Y8dQb0S6DtpxNgAKoJKA'):

  11.         url = "https://www.googleapis.com/youtube/v3/search"

  12.         pms = {'type': 'video', 'id' : id, 'key': api_key, 'channelId': channel_id, 'part':'snippet', 'order':'viewCount','maxResults':50}

  13.         res = requests.get(url, params = pms)

  14.         print("Connection status: %s" % res)

  15.         data = res.json()

  16.         #print(data)

  17.         lst = []

  18.         for video in data['items']:
  19.                 video_stats = get_statistics(video['id']['videoId'])
  20.                 #print(video['snippet']['title'])
  21.                 results_json = {
  22.                         'channelTitle' : video['snippet']['channelTitle'],
  23.                         'title' : video['snippet']['title'],
  24.                         'publishedAt' : video['snippet']['publishedAt'],
  25.                         'videoId' : video['id']['videoId'],
  26.                         'viewCount' : video_stats['items'][0]['statistics']['viewCount'],
  27.                         'commentCount' : video_stats['items'][0]['statistics']['commentCount'],
  28.                         'likeCount' : video_stats['items'][0]['statistics']['likeCount'],
  29.                         'dislikeCount' : video_stats['items'][0]['statistics']['dislikeCount'],
  30.                 }

  31.                 lst.append(results_json)

  32.         df = pd.read_json(json.dumps(lst))

  33.         return(df)
  34. In [5]:
  35. import requests

  36. url = 'https://www.googleapis.com/youtube/v3/videos'

  37. pms = {'part': 'snippet, statistics', 'id' : 'YQUpg795iBo', 'key': 'xxx'}

  38. res = requests.get(url, params = pms)

  39. data = res.json()
  40. In [6]:
  41. url = 'https://www.googleapis.com/youtube/v3/commentThreads'

  42. full_data = []                        # return list
  43. page = ''                                # init paging

  44. while True:

  45.         pms = {'part': 'snippet', 'videoId' : id, 'maxResults' : 100, 'key': api_key, 'pageToken': page}
  46.         res = requests.get(url, params = pms)

  47.         print("Connection status: %s" % res)

  48.         data = res.json()
  49.         full_data.extend(data['items'])

  50.         print("Just downloaded: %s, Total: %s" % (len(data['items']), len(full_data)))

  51.         try:
  52.                 page = data['nextPageToken']
  53.         except:
  54.                 break
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

7
20115326 学生认证  发表于 2018-6-15 09:51:41 |只看作者 |坛友微信交流群
好书,学习了

使用道具

8
小陆家嘴 发表于 2018-6-15 10:15:29 |只看作者 |坛友微信交流群
谢谢分享

使用道具

9
啸傲江弧 发表于 2018-6-15 10:30:44 |只看作者 |坛友微信交流群

使用道具

10
lhf8059 发表于 2018-6-15 10:42:23 |只看作者 |坛友微信交流群
看看看

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 14:32