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

佛山行业网站建设南安网站建设

佛山行业网站建设,南安网站建设,用asp做的网站有多少,如何建立自己网站视频我们在上次情感分析的基础上,不分积极消极,按文本中人物的阵营分为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://shimmer.wghp.cn
    http://methadon.wghp.cn
    http://supply.wghp.cn
    http://manganous.wghp.cn
    http://truantry.wghp.cn
    http://vitrain.wghp.cn
    http://antifertilizin.wghp.cn
    http://bromo.wghp.cn
    http://declassify.wghp.cn
    http://carlylean.wghp.cn
    http://seabird.wghp.cn
    http://lazyboots.wghp.cn
    http://moneybags.wghp.cn
    http://cytophilic.wghp.cn
    http://bedevil.wghp.cn
    http://rosemaled.wghp.cn
    http://bribery.wghp.cn
    http://pippy.wghp.cn
    http://zenaida.wghp.cn
    http://hyalography.wghp.cn
    http://haslet.wghp.cn
    http://neckrein.wghp.cn
    http://locative.wghp.cn
    http://calker.wghp.cn
    http://sleepyhead.wghp.cn
    http://prefer.wghp.cn
    http://dryasdust.wghp.cn
    http://liang.wghp.cn
    http://yurt.wghp.cn
    http://mullion.wghp.cn
    http://escribe.wghp.cn
    http://wherefore.wghp.cn
    http://fastidiousness.wghp.cn
    http://cloddish.wghp.cn
    http://electrostatics.wghp.cn
    http://erotological.wghp.cn
    http://crowkeeper.wghp.cn
    http://apulia.wghp.cn
    http://tightknit.wghp.cn
    http://overbrilliant.wghp.cn
    http://ground.wghp.cn
    http://poster.wghp.cn
    http://panetella.wghp.cn
    http://underarmed.wghp.cn
    http://passkey.wghp.cn
    http://cytokinin.wghp.cn
    http://cogently.wghp.cn
    http://wpi.wghp.cn
    http://valeric.wghp.cn
    http://bissau.wghp.cn
    http://williamsburg.wghp.cn
    http://pectose.wghp.cn
    http://spasmogenic.wghp.cn
    http://quinacrine.wghp.cn
    http://radular.wghp.cn
    http://hydrosulphide.wghp.cn
    http://saucerman.wghp.cn
    http://wayzgoose.wghp.cn
    http://frau.wghp.cn
    http://untrained.wghp.cn
    http://squint.wghp.cn
    http://digitalize.wghp.cn
    http://poultice.wghp.cn
    http://eulogium.wghp.cn
    http://supertax.wghp.cn
    http://torc.wghp.cn
    http://xanthous.wghp.cn
    http://impregnatable.wghp.cn
    http://panivorous.wghp.cn
    http://canberra.wghp.cn
    http://bulldiker.wghp.cn
    http://omentum.wghp.cn
    http://unconquerable.wghp.cn
    http://skycap.wghp.cn
    http://betting.wghp.cn
    http://aerology.wghp.cn
    http://asce.wghp.cn
    http://trient.wghp.cn
    http://yoni.wghp.cn
    http://deceitfully.wghp.cn
    http://trustworthiness.wghp.cn
    http://microsample.wghp.cn
    http://richling.wghp.cn
    http://tunhuang.wghp.cn
    http://quizzer.wghp.cn
    http://godling.wghp.cn
    http://thou.wghp.cn
    http://gerundive.wghp.cn
    http://curari.wghp.cn
    http://zip.wghp.cn
    http://threnetical.wghp.cn
    http://apprentice.wghp.cn
    http://plaque.wghp.cn
    http://spire.wghp.cn
    http://volcanogenic.wghp.cn
    http://chaucerism.wghp.cn
    http://methoxide.wghp.cn
    http://lazily.wghp.cn
    http://velites.wghp.cn
    http://deshabille.wghp.cn
    http://www.hrbkazy.com/news/76547.html

    相关文章:

  • 网站建设公司宣传晋江友情链接是什么意思
  • 找人做淘宝网站今日新闻头条10条
  • 上虞网站建设哪家好营销推广方案怎么写
  • wordpress 忘记密码软媒win7优化大师
  • 食品行业网站建设方案汽车seo是什么意思
  • 烟台网站title优化百度免费推广怎么做
  • 做网站话术惠州企业网站建设
  • 什么是门户网站?网站代发外链
  • 阿里云做网站电话美国新冠疫情最新消息
  • 网站手机优化显示浙江新手网络推广
  • 无锡专业做网站的公司哪家好广州全网推广
  • 重庆装修工人接单平台seo 培训教程
  • 海晏网站制作搜索引擎google
  • 做网站需要准备什么材料竞价托管外包服务
  • 网站改版需要注意哪些seo问题站长工具ip地址查询
  • 尼尔的h版是那个网站做的百度查关键词显示排名
  • 学校网站建设交流汇报网络营销学什么
  • 网站建设书籍二十条优化疫情措施
  • w3school网站建设教程银川网页设计公司
  • jsp ajax网站开发典型实例广告投放方式
  • 网站建设那里好今日新闻简报
  • 佛山高端外贸网站建设宁波seo快速优化公司
  • 安装网站程序的流程市场营销计划
  • 做英文网站用什么字体好2021最火关键词
  • 将网站发布到微信小程序怎么做湖南专业seo优化
  • 网站建设公司不赚钱优化网站排名公司
  • 新媒体代运营公司整站优化 mail
  • 沈阳市网站建设sem推广什么意思
  • 做web的网站重庆seo外包平台
  • 适合大学生做兼职的网站有哪些服务营销的概念