利用Python读取SQLite数据
import sqlite3 # 导入SQLite3数据库模块
import pandas as pd # 导入pandas模块
conn = sqlite3.connect('douban_comment_data.db') # 连接数据库
comment_data = pd.read_sql_query('select * from comment;',conn) # 读取数据
print(comment_data) # 输出数据
统计各个电影的评论数
import sqlite3 # 导入SQLite3数据库模块
import pandas as pd # 导入pandas模块
conn = sqlite3.connect('douban_comment_data.db') # 连接数据库
comment_data = pd.read_sql_query('select * from comment;',conn) # 读取数据
movie_list = comment_data['MOVIEID'].value_counts() # 统计各个电影的评论数
print(movie_list)
筛选评论数大于100的电影
import sqlite3 # 导入SQLite3数据库模块
import pandas as pd # 导入pandas模块
conn = sqlite3.connect('douban_comment_data.db') # 连接数据库
comment_data = pd.read_sql_query('select * from comment;',conn) # 读取数据
movie_list = comment_data['MOVIEID'].value_counts() # 统计各个电影的评论数
movie_list = movie_list[movie_list.values>100] # 筛选评论数大于100的电影
print(movie_list)
设计为函数,方便调用
def get_movie_id_list(min_comment_count): # 设计函数,输入参数:最小评论数量
movie_list = comment_data['MOVIEID'].value_counts() # 统计各个电影的评论数
movie_list = movie_list[movie_list.values>min_comment_count] # 筛选评论数大于最小评论数量的电影
return movie_list.index # 返回数据:高于最小评论数量的电影ID列表
统计各个电影的评论数
import sqlite3 # 导入SQLite3数据库模块
import pandas as pd # 导入pandas模块
def get_movie_id_list(min_comment_count): # 设计函数,输入参数:最小评论数量
movie_list = comment_data['MOVIEID'].value_counts() # 统计各个电影的评论数
movie_list = movie_list[movie_list.values>min_comment_count] # 筛选评论数大于最小评论数量的电影
return movie_list.index # 返回数据:高于最小评论数量的电影ID列表
conn = sqlite3.connect('douban_comment_data.db') # 连接数据库
comment_data = pd.read_sql_query('select * from comment;',conn) # 读取数据
print(get_movie_id_list(1000))
安装jieba中文分词模块
pip install jieba
获取某个电影的全部评论内容
movie_id = '1292052'
comment_list = comment_data[comment_data['MOVIEID']==movie_id]['CONTENT'] # 根据ID筛选评论
# print(comment_list)
comment_str_all = ''
for comment in comment_list:
comment_str_all += comment + '\n' # 整合全部评论为一个字符串,以换行隔开
获取分词后的列表
seg_list = list(jieba.cut(comment_str_all)) # 获取分词后的列表
keywords_counts = pd.Series(seg_list) # 转换成Pandas的Series类型数据
keywords_counts = keywords_counts.value_counts() # 统计各个关键词的出现次数
print(keywords_counts)
利用str.len()筛选字符串长度
keywords_counts = pd.Series(seg_list) # 转换成Pandas的Series类型数据
keywords_counts = keywords_counts[keywords_counts.str.len()>1] # 利用str.len()筛选字符串长度
对于一个储存了字符串的Series数据
可以使用.str.len()来获取各个字符串数据的长度
建立过滤词列表
FILTER_WORDS = ['知道','影评','电影','影片','这么','那么','怎么','如果','这个','一个','这种','时候','什么','一部','没有','还有','是','的','...','\n','is','觉得']
需要根据实际情况来调整过滤词列表
具体内容具体判断
筛除包含过滤词列表中词语的数据
利用str. contains()筛选数据
keywords_counts.str.contains('|'.join(FILTER_WORDS))
.str.contains()里填一个字符串或者正则表达式
反向选择不包含过滤词列表中词语的数据
keywords_counts = keywords_counts[~keywords_counts.str.contains('|'.join(FILTER_WORDS))] # 反向选择不包含过滤词列表中词语的数据
波浪号~
表示取反
列表中True的变成False
列表中False的变成True
汇总
seg_list = list(jieba.cut(comment_str_all)) # 获取分词后的列表
keywords_counts = pd.Series(seg_list) # 转换成Pandas的Series类型数据
keywords_counts = keywords_counts[keywords_counts.str.len()>1] # 利用str.len()筛选字符串长度
FILTER_WORDS = ['知道','影评','电影','影片','这么','那么','怎么','如果','这个','一个','这种','时候',
'什么','一部','没有','还有','是','的','...','\n','is','觉得']
keywords_counts = keywords_counts[~keywords_counts.str.contains('|'.join(FILTER_WORDS))] # 反向选择不包含过滤词列表中词语的数据
keywords_counts = keywords_counts.value_counts()[:30] # 统计各个关键词的出现次数
print(keywords_counts)
汇总为一个函数,可以设置电影ID和要获取的评论数量
def get_comment_keywords_counts(movie_id,count):
# movie_id = '1292052'
comment_list = comment_data[comment_data['MOVIEID']==movie_id]['CONTENT'] # 根据ID筛选评论
# print(comment_list)
comment_str_all = ''
for comment in comment_list:
comment_str_all += comment + '\n' # 整合全部评论为一个字符串,以换行隔开
seg_list = list(jieba.cut(comment_str_all)) # 获取分词后的列表
keywords_counts = pd.Series(seg_list) # 转换成Pandas的Series类型数据
keywords_counts = keywords_counts[keywords_counts.str.len()>1] # 利用str.len()筛选字符串长度
keywords_counts = keywords_counts[~keywords_counts.str.contains('|'.join(FILTER_WORDS))] # 反向选择不包含过滤词列表中词语的数据
keywords_counts = keywords_counts.value_counts()[:count] # 统计各个关键词的出现次数
return keywords_counts
导入pyecharts并绘制词云图
import sqlite3 # 导入SQLite3数据库模块
import pandas as pd # 导入pandas模块
import jieba # 导入‘结巴’模块
import pyecharts.options as opts
from pyecharts.charts import WordCloud
FILTER_WORDS = ['知道','影评','电影','影片','这么','那么','怎么','如果','这个','一个','这种','时候',
'什么','一部','没有','还有','是','的','...','\n','is','觉得']
def get_movie_id_list(min_comment_count): # 设计函数,输入参数:最小评论数量
movie_list = comment_data['MOVIEID'].value_counts() # 统计各个电影的评论数
movie_list = movie_list[movie_list.values>min_comment_count] # 筛选评论数大于最小评论数量的电影
return movie_list.index # 返回数据:高于最小评论数量的电影ID列表
def get_comment_keywords_counts(movie_id,count):
comment_list = comment_data[comment_data['MOVIEID']==movie_id]['CONTENT'] # 根据ID筛选评论
# print(comment_list)
comment_str_all = ''
for comment in comment_list:
comment_str_all += comment + '\n' # 整合全部评论为一个字符串,以换行隔开
seg_list = list(jieba.cut(comment_str_all)) # 获取分词后的列表
keywords_counts = pd.Series(seg_list) # 转换成Pandas的Series类型数据
keywords_counts = keywords_counts[keywords_counts.str.len()>1] # 利用str.len()筛选字符串长度
keywords_counts = keywords_counts[~keywords_counts.str.contains('|'.join(FILTER_WORDS))] # 反向选择不包含过滤词列表中词语的数据
keywords_counts = keywords_counts.value_counts()[:count] # 统计各个关键词的出现次数
return keywords_counts
conn = sqlite3.connect('douban_comment_data.db') # 连接数据库
comment_data = pd.read_sql_query('select * from comment;',conn) # 读取数据
movie_id = '1292052'
keywords_counts = get_comment_keywords_counts(movie_id,30)
wordcloud = WordCloud()
wordcloud.add("", tuple(zip(keywords_counts.index,keywords_counts)), word_size_range=[20, 100])
wordcloud.render()