基于Tableau和python的豆瓣热门影评写作中关键词的统计分析研究----2

  1. 可视化作品设计

  1. 研究目标

利用可视化研究手段,从所有影片整体角度及影片个体角度对热评进行分析,分别得出整体热评泛关键词式及各类型影片热评关键词特式,进而了解哪些关键词可以助力影评创作者写出“热评”。

  1. 可视化手段

  1. 数据预处理

  1. 标点符号的处理

由于影评中存在大量的标点符号,而这些标点符号在绘制热评词云图时很明显无法给出相应的实际意义(相较于热评关键词而言,热评标点符号的意义确实过小),因而在绘制词云图前进行词频统计时,需要避免统计影评中的标点符号,从而减少无意义数据的收集。

解决方案:

利用python种re库中findall()功能进行解决。

通过预设模式:

pattern = re.compile(r"[\u4e00-\u9fa5]+")

及正则匹配算法:

re.findall(pattern, data_comment)

共同实现仅匹配中文字符的功能;r”[\u4e00-\u9fa5]+”中,“\u4e00”与“\u9fa5”分别对应unicode编码中中文字符的始末位置,可以保证做到中文字符的全匹配同时略去影评字符串中的标点符号。

  1. 无意义字段的处理

由于影评文本中通常存在大量的虚词,如“一部、从、自从、到、在、当、于、朝、按、按照、经过、以、根据”等,这些虚词在具有较低的价值密度的同时由于在各条影评中大量出现,若不进行适当处理则会出现低价值或无意义字段在词云图中占据大量位置的情况,因此在及逆行词频统计时应当提前过滤此类无意义字段。

本作品通过预设停止符txt文件,通过判断jieba分词后文件中的词语是否处于stopwords.txt文件中,判断该文本字段是否为无意义字符进行过滤。

由于停止符文件集为人为设定并且针对样本集进行了一定针对性调整。例如在本次电影影评分析过程中发现“电影”一词出现频率过高而不具有独特含义,为避免该词高频出现干扰分析结果,故在停止符集合中加入“电影”一词进行人为修正。因此该停止符集的设定可能并非最优、最适合此样本集,可能存在改进空间。

处理无意义字段代码如下:

f = open("stopwords.txt", "r", encoding="utf-8") # 获取虚词等无效词语预设集

word_string = f.read()

f.close()

word_string = word_string.split(" ")

for mem in segment:

if mem not in word_string: # 去虚词等无效词语预设集中词语

for i in range(weight_v):

return_str = return_str + " " + mem

# print(return_str)

  1. 带权词云的映射函数

由于本文研究目的在于研究热评中出现的高频词,进而帮助读者创作影视热评,因而在进行词频统计时需按照评论支持数对有效词进行加权统计。

此处采取两步标准化步骤进行解决。第一步中,实现利用如下代码实现对于提取“支持数”属性下字段的中支持数的数字部分内容:

def split_single(unsplit_str: str, split_value: str): # "0支持" -> ["0","支持"][0]

return unsplit_str.split(split_value)[0]

def split_score(unsplit_value_list: list, split_value: str): # split操作批处理

split_list = []

for mem in unsplit_value_list:

split_list.append(split_single(mem, split_value))

return split_list

而后通过Noramlization/Rescaling方法将对应数据集中支持数归一化至[0,1]内。

list_MinMax.append((mem - min_v) / (max_v - min_v)) # f(x)

最后通过自然对数的映射方式将其映射为该字段对应有效词语应在文本集内出现的频数,进而实现对于评论有效词语的加权统计。

list_MinMax[i] = math.ceil(math.log(list_MinMax[i] * 100 + 1.1, math.e))

  1. 可视化图表选择

  1. 词云图

  1. 树状图

  1. 填充气泡图

  1. 标记和视觉通道

  1. 标记:图形元素面

  1. 通道:位置、大小、色调、方向

绘图代码如下:


def load_dataset(url1: str):  # 数据集载入
    df = pd.read_excel(url1)
    return df


def split_single(unsplit_str: str, split_value: str):  # "0支持" -> ["0","支持"][0]
    return unsplit_str.split(split_value)[0]


def split_score(unsplit_value_list: list, split_value: str):  # split操作批处理
    split_list = []
    for mem in unsplit_value_list:
        split_list.append(split_single(mem, split_value))
    return split_list


def str2int(s):
    def fn(x, y):
        return x * 10 + y

    def char2num(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

    return reduce(fn, map(char2num, s))


def preprocesssing_data(list_data: list):  # 映射函数 映射为[0,1]->[1,101]
    list_score = []
    for mem in list_data:
        list_score.append(str2int(mem))
    # min_max_scaler = preprocessing.MinMaxScaler()
    # list_MinMax = min_max_scaler.fit_transform(list_score)
    list_MinMax = []
    max_v = max(list_score)
    print("max_v = " + str(max_v))
    min_v = min(list_score)
    print("min_v = " + str(min_v))
    for mem in list_score:
        list_MinMax.append((mem - min_v) / (max_v - min_v))  # f(x)
    l = len(list_MinMax)
    for i in range(l):
        list_MinMax[i] = math.ceil(math.log(list_MinMax[i] * 100 + 1.1, math.e))  # g(f(x))
    return list_MinMax


# jieba分词
def jieba_all(data_comment_unsplit: list, weight_v: list):
    write_str = ""
    count = 0
    for mem in data_comment_unsplit:
        write_str = " " + jieba_single(str(mem), weight_v[count])
        # print(write_str)
        with open("word_cloud.txt", "a", encoding="utf-8") as f:
            f.write(write_str)  # 自带文件关闭功能,不需要再写f.close()
        count += 1


def jieba_single(data_comment: str, weight_v: int):
    return_str = " "
    # print(data_comment)
    # print(type(data_comment))
    pattern = re.compile(r"[\u4e00-\u9fa5]+")  # 仅提取汉字,忽略标点符号
    filterdata = re.findall(pattern, data_comment)  # 模式匹配
    cleaned_comments = "".join(filterdata)
    segment = jieba.lcut(cleaned_comments)
    f = open("stopwords.txt", "r", encoding="utf-8")  # 获取虚词等无效词语预设集
    word_string = f.read()
    f.close()
    word_string = word_string.split(" ")
    for mem in segment:
        if mem not in word_string:  # 去虚词等无效词语预设集中词语
            for i in range(weight_v):
                return_str = return_str + " " + mem
                # print(return_str)
    return return_str


if __name__ == '__main__':
    # 跑程序前请先把旧的word_cloud.txt删掉!避免数据重复插入
    url_1 = "豆瓣影评-长津湖.xlsx"  # 此处更改文件
    url_2 = "评分更新.xlsx"
    dataset = load_dataset(url_1)
    score = load_dataset(url_2)
    # dataset.iloc[i,2]为影评列;dataset.iloc[i,4]为支持数列;dataset.iloc[i,5]为电影名列
    # 对“支持数”列进行切割,获取具体支持数
    data_score_unsplit = dataset.iloc[:, 4]
    data_score_unsplit = data_score_unsplit.tolist()
    data_score_split = split_score(data_score_unsplit, "有用")
    # 对"支持数"列进行映射
    list_num = preprocesssing_data(data_score_split)
    # 对热评数进行分词,并对每条评论的分词结果,按照对应的支持数映射结果进行多重插入
    data_comment_unsplit = dataset.iloc[:, 2]
    jieba_all(data_comment_unsplit, list_num)
    # 画相应词云图
    f = open("word_cloud.txt", "r", encoding="utf-8")
    word_string = f.read()
    f.close()

    w = wordcloud.WordCloud(width=700, height=500, background_color="white", min_font_size=15,
                            collocations=False, font_path="simfang.ttf", relative_scaling=0.5)
    w.generate(word_string)
    plt.imshow(w)
    plt.axis("off")
    # 在此处更改词云图名称
    plt.savefig("长津湖词云图.png", bbox_inches='tight', dpi=600)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值