当前位置: 首页 > wzjs >正文

精通网站建设 100wordpress 文字背景颜色

精通网站建设 100,wordpress 文字背景颜色,网站优化建设深圳,wordpress调用栏目我们在上次情感分析的基础上,不分积极消极,按文本中人物的阵营分为3队。可以猜想按照积极消极分类是有现成的feeling可以分析,但人物阵营却是没有现成资料,需要额外给出信息的。 图1 图2 上面两图的文字大小和数量有区别&#xf…

    我们在上次情感分析的基础上,不分积极消极,按文本中人物的阵营分为3队。可以猜想按照积极消极分类是有现成的feeling可以分析,但人物阵营却是没有现成资料,需要额外给出信息的。

图1

 图2

上面两图的文字大小和数量有区别,关键在于调整了下图数据。这些参数都和导入的底图mk有关

(1)整段代码

from wordcloud import (WordCloud, get_single_color_func)
import imageio
import jiebaclass SimpleGroupedColorFunc(object):"""Create a color function object which assigns EXACT colorsto certain words based on the color to words mappingParameters----------color_to_words : dict(str -> list(str))A dictionary that maps a color to the list of words.default_color : strColor that will be assigned to a word that's not a memberof any value from color_to_words."""def __init__(self, color_to_words, default_color):self.word_to_color = {word: colorfor (color, words) in color_to_words.items()for word in words}self.default_color = default_colordef __call__(self, word, **kwargs):return self.word_to_color.get(word, self.default_color)class GroupedColorFunc(object):"""Create a color function object which assigns DIFFERENT SHADES ofspecified colors to certain words based on the color to words mapping.Uses wordcloud.get_single_color_funcParameters----------color_to_words : dict(str -> list(str))A dictionary that maps a color to the list of words.default_color : strColor that will be assigned to a word that's not a memberof any value from color_to_words."""def __init__(self, color_to_words, default_color):self.color_func_to_words = [(get_single_color_func(color), set(words))for (color, words) in color_to_words.items()]self.default_color_func = get_single_color_func(default_color)def get_color_func(self, word):"""Returns a single_color_func associated with the word"""try:color_func = next(color_func for (color_func, words) in self.color_func_to_wordsif word in words)except StopIteration:color_func = self.default_color_funcreturn color_funcdef __call__(self, word, **kwargs):return self.get_color_func(word)(word, **kwargs)mk = imageio.v2.imread("chinamap.jpg")w = WordCloud(width=1000,height=700,background_color='white',font_path='msyh.ttc',mask=mk,scale=15,max_font_size=60,max_words=20000,font_step=1)f = open('三国演义.txt', encoding='utf-8')
txt = f.read()
txtlist = jieba.lcut(txt)
string = " ".join(txtlist)w.generate(string)color_to_words = {'green': ['刘备', '刘玄德', '孔明', '诸葛孔明', '玄德', '关公', '玄德曰', '孔明曰','张飞', '赵云', '后主', '黄忠', '马超', '姜维', '魏延', '孟获','关兴', '诸葛亮', '云长', '孟达', '庞统', '廖化', '马岱'],'red': ['曹操', '司马懿', '夏侯', '荀彧', '郭嘉', '邓艾', '许褚','徐晃', '许诸', '曹仁', '司马昭', '庞德', '于禁', '夏侯渊', '曹真', '钟会'],'purple': ['孙权', '周瑜', '东吴', '孙策', '吕蒙', '陆逊', '鲁肃', '黄盖', '太史慈'],'pink': ['董卓', '袁术', '袁绍', '吕布', '刘璋', '刘表', '貂蝉']
}default_color = 'gray'grouped_color_func = GroupedColorFunc(color_to_words, default_color)w.recolor(color_func=grouped_color_func)w.to_file('output13-三国.png')

     让我们把代码中没见过的用蓝框圈出来~

具体代码解读:

(2)get_single_color_func为词云中的所有词语分配单一颜色

from wordcloud import (WordCloud, get_single_color_func)

这次从wordcloud里我们导入了WordCloud和get_single_color_func

  • WordCloud 类是用来生成词云的
  • get_single_color_func 函数用于创建一个颜色函数,为词云中的所有词语分配单一颜色

我们来看一下这段代码:

class SimpleGroupedColorFunc(object):"""Create a color function object which assigns EXACT colorsto certain words based on the color to words mappingParameters----------color_to_words : dict(str -> list(str))A dictionary that maps a color to the list of words.default_color : strColor that will be assigned to a word that's not a memberof any value from color_to_words."""def __init__(self, color_to_words, default_color):self.word_to_color = {word: colorfor (color, words) in color_to_words.items()for word in words}self.default_color = default_colordef __call__(self, word, **kwargs):return self.word_to_color.get(word, self.default_color)

(3)class SimpleGroupedColorFunc(object)定义了一个映射颜色类

   SimpleGroupedColorFunc 类根据颜色到单词的映射关系为特定的单词分配精确的颜色。如果某个单词不在映射关系中,则会为其分配默认颜色

(4) “class” 定义类

     “class” 用于定义类。类是一种用户自定义的数据类型。

#定义一个类的基本语法如下:
class ClassName:# 类的文档字符串(可选)"""类的描述信息"""# 类的属性和方法定义def __init__(self, 参数列表):# 构造方法,用于初始化对象的属性self.属性名 = 参数def 方法名(self, 参数列表):# 类的方法定义pass
  • ClassName类的名称,通常遵循每个单词的首字母大写,其余字母小写。
  • __init__:称作构造方法,在创建类的实例时会自动调用,用于初始化对象的属性
  • self:是一个约定俗成的参数名,代表类的实例本身

所以我们可以对这段代码的结构进行梳理:

(5)字典dictkey-value)键值对

    字典(dict)是一种无序、可变可哈希集合数据类型,它以键值对(key-value)的形式存储数据。

    键(Key)在一个字典中,键必须是唯一的。如果在创建字典或更新字典时使用了重复的键,后面的键值对会覆盖前面的。

    值(Value)字典的值可以是任意 Python 对象,包括数字、字符串、列表、元组、集合、字典等,甚至可以是自定义的类实例。值不需要是唯一的,不同的键可以对应相同的值。

(6)哈希值Hash Value:对数据计算后得到的一个固定长度的输出值

特点

  • 确定性:相同输入相同的值。
  • 高效性:过程非常快。
  • 固定长度:无论输入数据的长度,输出长度固定。常见16 字节/32 字节的哈希值。
  • 雪崩效应:输入的微小变化会导致哈希值发生很大的改变。
  • 不可逆性:很难反向推导出原始输入数据。

    在 Python 中,可变对象(如列表、字典、集合等)是不可哈希的,因为它们的值可以被修改。只有不可变对象(如整数、浮点数、字符串、元组等)才是可哈希的。

color_to_words : dict(str -> list(str))

color_to_word这里涉及到字典dict的用法,键值对key-value对应:

    • 键(key)是字符串str)类型,表示color
    • 值(value)是字符串列表list(str))类型,表示words
    self.word_to_color = {word: colorfor (color, words) in color_to_words.items()for word in words}

    self.word_to_color通过字典推导式创建的一个新字典,键值对key-value对应:

    • 键(key)是word
    • 值(value)是color

        这里我们可能会疑惑怎么会有两个字典?为了解释清楚我用蓝橙两个框代表了两个字典,可以看到两者就是顺序互换的关系。让我给大家打个比方,我们用英汉字典来代表color_to_word字典,并简记为色词字典。用汉英字典来代表self.word_to_color,简记为词色字典。比如一个英国人和一个中国人想要聊天,他们手里分别有一本字典。那英国人要让对方理解就要用英汉词典,反之亦然。

        我们现在的情景就很类似,给的是绿色代表蜀中阵营,红色代表曹操阵营,紫色代表江东阵营。也就是我们现有了色词字典,但是任务是要把三国演义的文本进行颜色对应,这就要用到词色字典了。所以需要把给出的色词字典用py生成词色字典。这一处理方式是为了简化我们输入的工作量,同时加快结果输出的速度。

     有了以上理解后本段代码就可理解为:

    我们再来看下一段定义:

    class GroupedColorFunc(object):"""Create a color function object which assigns DIFFERENT SHADES ofspecified colors to certain words based on the color to words mapping.Uses wordcloud.get_single_color_funcParameters----------color_to_words : dict(str -> list(str))A dictionary that maps a color to the list of words.default_color : strColor that will be assigned to a word that's not a memberof any value from color_to_words."""def __init__(self, color_to_words, default_color):self.color_func_to_words = [(get_single_color_func(color), set(words))for (color, words) in color_to_words.items()]self.default_color_func = get_single_color_func(default_color)def get_color_func(self, word):"""Returns a single_color_func associated with the word"""try:color_func = next(color_func for (color_func, words) in self.color_func_to_wordsif word in words)except StopIteration:color_func = self.default_color_funcreturn color_funcdef __call__(self, word, **kwargs):return self.get_color_func(word)(word, **kwargs)

     从代码上看和刚刚解读的class SimpleGroupedColorFunc很像,但当我们观察生成词云的时候会发现同为蜀中阵营的刘关张虽然都是绿色,却绿的各不相同。如果只有刚刚解读的代码是无法实现这一特点的,所以这段class GroupedColorFunc代码实现的就是在整体色调中再进行颜色区分。

     这段定义也很长,本次已经解读了很多内容了。这个我们就下次再解读~大家先尝试运行一下吧~

    (7)总结:

    • get_single_color_func为词云中的所有词语分配单一颜色
    • “class” 定义类
    • 字典dictkey-value)键值对
    • 哈希值Hash Value:对数据计算后得到的一个固定长度的输出值

    文章转载自:

    http://cdWVofTA.hpxxq.cn
    http://1KS07Jfv.hpxxq.cn
    http://C9LapIox.hpxxq.cn
    http://Rnxe9KqS.hpxxq.cn
    http://tHd4DNZs.hpxxq.cn
    http://Nbt6wQz1.hpxxq.cn
    http://J9vfp2zE.hpxxq.cn
    http://cCatjeZc.hpxxq.cn
    http://2GO7OmaV.hpxxq.cn
    http://hdm9j3oo.hpxxq.cn
    http://XbluEXtz.hpxxq.cn
    http://kknRss79.hpxxq.cn
    http://WyU1tQ6J.hpxxq.cn
    http://JQcqBAKp.hpxxq.cn
    http://BKh8WSNU.hpxxq.cn
    http://rewI3X4V.hpxxq.cn
    http://WHclL3f0.hpxxq.cn
    http://ZjwfARVn.hpxxq.cn
    http://TjjNBlgy.hpxxq.cn
    http://VWsZpo6F.hpxxq.cn
    http://wzO4pG9x.hpxxq.cn
    http://T5ViX6gp.hpxxq.cn
    http://9H8fH7tP.hpxxq.cn
    http://hbpmcXj0.hpxxq.cn
    http://rXFFlvwW.hpxxq.cn
    http://lQpz1orm.hpxxq.cn
    http://Y1Atwxkv.hpxxq.cn
    http://AhjYHqnD.hpxxq.cn
    http://fmEryy7y.hpxxq.cn
    http://1sgH9Nbr.hpxxq.cn
    http://www.dtcms.com/wzjs/702150.html

    相关文章:

  • 吉安高端网站建设公司营销策划与运营方案怎么写
  • 成都网站推广如何5188站长平台
  • 自助建站免费网站大兴安岭网站建设
  • 建设网站issseo策略推广什么意思
  • 甘肃省建设厅网站资质升级公示教学网站开发背景及意义
  • 彩票网站建设柏网站域名删除时间查询
  • 模板网站优化网站建设岗位廉政风险防控
  • 旅游景点网站设计论文百度seo排名优化价格
  • 网站右侧固定标题怎么做wordpress更新缓存的插件
  • 东莞谷歌推广公司国外网站seo
  • 广州有专做网站windows优化大师和360哪个好
  • 做网站入门自动添加内部链接的wordpress插件
  • 东莞整站优化排名附近电脑培训班零基础
  • 主体负责人和网站负责人不能是同一个人平时发现同学做的ppt找的材料图片不错_不知道从哪些网站可以获得
  • php网站如何编辑深圳网站设计招聘信息
  • 商务网站规划设计要点网站优化应该怎么做
  • pc网站建设哪个好企业信息公示网查询
  • 富阳做网站方式网站前台做好之后再怎么做
  • 响应式网站建站价格网站如何做会员登录页面
  • 上海大型网站开发公司dede网站地图地睛
  • 温州个人网站建设js网站开发视频
  • 网站建设专用术语产品网络营销策划方案
  • 达州住房和城乡建设厅网站今天重大新闻摘抄
  • 机械 网站源码如何查看网站开发者
  • 深圳好蜘蛛网站建设公司查网站域名
  • 企业网站建设课程体会网站开发公司的推广费用
  • 宜昌模板网站建设淘宝店招图片大全免费
  • 网站运营推广怎么做建立主题网站的顺序一般是
  • 网上下载的网站后台安全吗松松软文平台
  • 网站建设工作组怎么注册自己的网站域名