Ejemplo n.º 1
0
def main():  
    test = 1111111   
    try:
        stats_word.stats_text(test)
    except ValueError as error :
        print('输入值:',test)
        print('错误信息:',error)
        return False
    finally:
        print('请再输入!')
Ejemplo n.º 2
0
def auto_reply(msg):
    r = requests.get(msg.url)
    document = pq(r.text)
    content = document('#js_content').text()
    #统计词频
    result = stats_word.stats_text(content,0,10)
    #画布
    plt.rcdefaults()
    fig, ax = plt.subplots()
    #处理数据
    people = list(dict(result).keys())
    y_pos = np.arange(len(people))
    performance = list(dict(result).values())
    #设置字体
    plt.rcParams['font.sans-serif']=['SimHei']
    #画图
    ax.barh(people, performance, align='center')
    ax.set_yticks(y_pos)
    ax.set_yticklabels(people)
    ax.invert_yaxis()  # labels read top-to-bottom
    ax.set_xlabel('Performance')
    ax.set_title('TOP10词频统计')
    plt.savefig('day13.png')
    #回复图片
    msg.reply_image('day13.png')
Ejemplo n.º 3
0
def process_url(msg):
    content_url = msg.url
    html_code = requests.get(content_url).text
    document = PyQuery(html_code)
    content = document("#js_content").text().replace("\n", "")

    try:
        en_result, cn_result = stats_text("", content)
        # print(cn_result)
        msg.reply(cn_result)

        np.random.seed(20190828)
        plt.rcdefaults()
        fig, ax = plt.subplots()

        words = tuple([word for word, count in cn_result])
        y_pos = np.arange(len(words))
        performance = np.array([count for word, count in cn_result])
        error = np.random.rand(len(words))

        ax.barh(y_pos, performance, xerr=error, align="center")
        ax.set_yticks(y_pos)
        ax.set_yticklabels(words, fontproperties='SimHei')
        ax.invert_yaxis()
        ax.set_xlabel("Frequency")
        ax.set_title("Top 20 Chinese words in an article")

        plt.show()

    except ValueError as e:
        print("Exception catched.")
        print(e)
Ejemplo n.º 4
0
def reply_my_friend(msg):
    response = requests.get(msg.url)
    document = PyQuery(response.text)
    content = document('#js_content').text()
    #URL信息提取
    reply = stats_word.stats_text(content, 100)
    return reply
Ejemplo n.º 5
0
def return_msg(msg):
    if msg.type == 'Sharing':
        r = requests.get(msg.url)
        document = PyQuery(r.text)
        content = document('#js_content').text()
        r_list = stats_word.stats_text(content, 10)
        r_dic = dict(r_list)
        keys, values = zip(*r_dic.items())

        plt.rcdefaults()
        fig, ax = plt.subplots()

        people = r_list
        y_pos = np.arange(len(people))
        error = np.random.rand(len(people))
        word_frequency = values

        plt.rcParams['font.sans_self'] = ['SimHei']
        ax.barh(y_pos,
                word_frequency,
                xerr=error,
                align='center',
                color='green',
                ecolor='black')
        ax.set_yticks(y_pos)
        ax.set_yticklabels(keys)
        ax.invert_yaxis()
        ax.set_xlabel('次数')
        ax.set_title('文章词频统计如下')

        plt.savefig('d13.png')
        msg.reply_image('d13.png')
Ejemplo n.º 6
0
def auto_reply(msg):
    response = requests.get(msg.url)
    document = PyQuery(response.text)
    content = document('#js_content').text()
    text=content
    print(text)
    str_text=str(stats_word.stats_text(text,100))
Ejemplo n.º 7
0
def auto_reply(msg):
    if Message.type == 'sharing':
        r = requests.get(msg.url)
        document = PyQuery(r.text)
        content = document('#js_content').text()
        reply = stats_word.stats_text(content, count=100)
        return reply  #将词频自动发给好友
Ejemplo n.º 8
0
def auto_reply(msg):
    r = requests.get(msg.url)
    document = pq(r.text)
    content = document('#js_content').text()
    #统计词频
    result = stats_word.stats_text(content, 0, 100)
    return result
Ejemplo n.º 9
0
def my_friend_sharing(msg):
    msg=msg.url
    r = requests.get(msg)
    document=PyQuery(r.text)
    content = document('#js_content').text()
    a=stats_word.stats_text(content,10)
    b=dict(a)
    keys,values=zip(*b.items())


    plt.rcdefaults()#从Matplotlib的内部默认样式恢复rc参数
    fig, ax = plt.subplots()#创建一个图形和一组子图

    word=a
    y_pos = np.arange(len(word))#在给定间隔内返回均匀间隔的值。
    performance = 3 + 10 * np.random.rand(len(word))#给定形状的随机值。
    error = np.random.rand(len(word))
    
    plt.rcParams['font.sans-serif']=['SimHei'] #不用这行会出现中文字变框框,windows平台可用
    
    
    ax.barh(y_pos, values, xerr=error, align='center',color='green', ecolor='black')
    ax.set_yticks(y_pos)
    ax.set_yticklabels(keys)
    ax.invert_yaxis()  # labels read top-to-bottom
    ax.set_xlabel('词频数目')
    ax.set_title('统计词频图表')

    plt.savefig("day13.png")
    my_friend.send_image("day13.png")
Ejemplo n.º 10
0
def print_others(msg):
    if msg.type == 'Sharing':
        r = requests.get(msg.url)
        document = PyQuery(r.text)
        content = document('#js_content').text()
        r_list = stats_word.stats_text(content, 100)
        r_str = str(r_list)
        msg.reply(r_str)
Ejemplo n.º 11
0
def print_message(msg):
    if msg.type == 'Sharing':  #sharing 分享的公众号文章
        r = requests.get(msg.url)  #解析获取公众号文章的url
        document = PyQuery(r.text)
        content = document('#js_content').text()
        r_list = stats_word.stats_text(content, 100)
        r_str = str(r_list)
        msg.reply(r_str)  # 自动回复返回的参数
Ejemplo n.º 12
0
 def handler(msg):
     try:
         logging.info('sharing url=%s', msg.url)
         article = get_article(msg.url)
         result = stats_word.stats_text(article, 100)
         msg.repley(str(result))
     except Exception as e:
         logging.exception(e)
Ejemplo n.º 13
0
def my_friend_sharing(msg):
    msg=msg.url
    r = requests.get(msg)
    document=PyQuery(r.text)
    content = document('#js_content').text()
    a=stats_word.stats_text(content,100)
    b=str(a)
    my_friend.send(b)
 def SHARING_Msg(msg):
     if msg.type == 'Sharing':
         response = requests.get(msg.url)
         document = PyQuery(response.text)
         contect = document('#js_content').text()
         c = str(stats_word.stats_text(content, 10))
         msg.reply(c)
     else:
         print('waiting......')
Ejemplo n.º 15
0
def auto_reply(msg):
    import requests
    from pyquery import PyQuery
    import stats_word
    response = requests.get(msg.url)
    document = PyQuery(response.text)
    content = document('#js_content').text()
    list1 = stats_word.stats_text(content, 100)
    str1 = "".join([str(i) for i in list1])
    return str1
Ejemplo n.º 16
0
def auto_reply(msg):
    import requests
    from pyquery import PyQuery
    import stats_word
    response = requests.get(
        'https://mp.weixin.qq.com/s/pLmuGoc4bZrMNl7MSoWgiA')
    document = PyQuery(response.text)
    content = document('#js_content').text()
    list1 = stats_word.stats_text(content, 100)
    str1 = "".join([str(i) for i in list1])
    return str1
def reply_my_friend(msg):
    # 如果消息为 分享(SHARING)类型,则获取消息的网页链接(msg.ugl)
    if msg.type == 'Sharing':
        response = requests.get(msg.url)
        document = PyQuery(response.text)
        content = document('#js_content').text()
        c = str(stats_word.stats_text(content, 100))
        # 将获得的信息回复好友
        msg.reply(c)
    else:
        pass
def word_frequency(meg):

    response = requests.get(meg.url)  #请求连接,并读取内容

    document = PyQuery(response.text)
    content = document('#js_content').text()  #将内容提取出来

    list_a = sw.stats_text(content, 10)  #对内容进行分词,取其前100个频率的词汇

    meg.reply_image(dt.map_making(list_a))

    return
def stats(url):
    import requests
    from pyquery import PyQuery
    import stats_word
    response = requests.get(url)
    document = PyQuery(response.text)
    content = document('#js_content').text()
    count = 100

    contents = stats_word.stats_text(content, count)
    result = contents.__str__()
    return result
Ejemplo n.º 20
0
def process_url(msg):
    content_url = msg.url
    html_code = requests.get(content_url).text
    document = PyQuery(html_code)
    content = document("#js_content").text().replace("\n", "")

    try:
        en_result, cn_result = stats_text("", content)
        # print(cn_result)
        msg.reply(cn_result)

    except ValueError as e:
        print("Exception catched.")
        print(e)
Ejemplo n.º 21
0
def word_frequency(meg):

    response = requests.get(meg.url)  #请求连接,并读取内容

    document = PyQuery(response.text)
    content = document('#js_content').text()  #将内容提取出来

    list_a = sw.stats_text(content, 100)  #对内容进行分词,取其前100个频率的词汇
    a = ''
    for i in range(len(list_a)):
        a = a + str(list_a[i][0]) + ' ' + str(list_a[i][1]) + ', '
    a = '以上文章的词频统计结果为:' + a

    return a
def reply_my_friend(msg): 
	#如果消息为分享(sharing)类型的消息,则获取消息的网页链接(msg.url)
	if msg.type== 'Sharing' :
		my_friend_msg_url=msg.url
		#将获取的链接通过网络请求获得网页内容
		text_str=(requests_weixin(my_friend_msg_url))
		
		#调用stats_word模块中的stats_text函数,统计词频
		#使用分词工具对中文字符串进行分词,统计词频,得出结果,	
		int_num=10
		text_num_10 = stats_word.stats_text(text_str,int_num)

		#生成词频图表,并保存为图片
		plot_text_num(text_num_10)		
		
		# 将处理结果返回给发消息的好友
		msg.reply_image("NA.png") #回复图片
Ejemplo n.º 23
0
def main():
    s = stats_word.stats_text(content, 100)
    print(type(s))
    newtype = str(s)
    type(newtype)
    print(type(s))
    print(s)
    import getpass
    sender = input('输入发件人邮箱:')
    password = getpass.getpass('输⼊发件⼈邮箱密码(可复制粘贴):')
    recipients = input('输入收件人邮箱:')
    yag = yagmail.SMTP(sender, password, 'smtp.qq.com')
    yag.send(recipients, '自学训练营学习5群 Day11 williswill', str(s))

    # 自学训练营学习5群 Day11 williswill
    # 结果如下:
    print(s)
Ejemplo n.º 24
0
def forward_msg(msg):
	print('you have a new message from %s:' % (msg.sender))

	# 获取分享文章内容
	content = wxdownload.download_article(msg.url)
	
	# 统计词频
	word_list = stats_word.stats_text(content,10)
	print("word:",word_list)

	# 转译成图表,保存为图片于默认路径
	x,y = [],[]
	for w in word_list:
		x.append(w[1]) # 词语y轴
		y.append(w[0]) # 频率x轴

	img_path = wxplot.plot(x,y)

	# 把图片回复给发消息的人
	msg.reply_image(img_path)
def reply_my_friend(msg):
    #如果消息为分享(sharing)类型的消息,则获取消息的网页链接(msg.url)
    print(msg)
    if msg.type == 'Sharing':
        my_friend_msg_url = msg.url
        #将获取的链接通过网络请求获得网页内容
        #print(my_friend_msg_url)
        text_str = (requests_weixin(my_friend_msg_url))
        #print(text_str)
        #调用stats_word模块中的stats_text函数,统计词频
        # int _num词频统计数
        int_num = 100
        #print(int_num)
        #使用分词工具对中文字符串进行分词,统计词频,得出结果,
        #print('微信分享文章的词频分析,前100个词和词频数为:')
        text_num_100 = stats_word.stats_text(text_str, int_num)
        print(text_num_100)

        # 将处理结果返回给发消息的好友
        #msg.reply_my_friend
        return '微信分享文章的词频分析,前100个词和词频数为: {} '.format(text_num_100)
Ejemplo n.º 26
0
def new_message(msg):
    if msg.type == 'Sharing':  #判断分享类型的消息

        ###下面是分析网络文章的代码。
        response = requests.get(msg.url)  #获取文章的连接的内容
        document = PyQuery(response.text)
        content = document('#js_content').text()  #获取文本
        count = 100  #限制输出元素的变量
        try:
            text_list = stats_word.stats_text(content, count)  #调用统计汉字的函数
            text_list2 = []
            for i in text_list:
                text_list2.append(i[0] + ':' + str(i[1]) +
                                  ';')  #把text_list里面的元素处理成str存在text_list2里
            text_str = ''.join(text_list2)  #把列表转换成str
            print(text_str)
        except ValueError as identifier:
            print('请输入字符串', identifier)

        #回复消息
        msg.reply('此文章高频用词')
        msg.reply(text_str)
Ejemplo n.º 27
0
So Yugong, his sons, and his grandsons started to break up rocks and
remove the earth. They transported the earth and rubble to the Sea of
Bohai.
Now Yugong’s neighbour was a widow who had an only child eight years
old. Evening the young boy offered his help eagerly.
Summer went by and winter came. It took Yugong and his crew a full
year to travel back and forth once.
On the bank of the Yellow River dwelled an old man much respected for
his wisdom. When he saw their back-breaking labour, he ridiculed
Yugong saying,”Aren’t you foolish, my friend? You are very old now,
and with whatever remains of your waning strength, you won’t be able
to remove even a corner of the mountain.”
Yugong uttered a sigh and said,”A biased person like you will never
understand. You can’t even compare with the widow’s little boy!”
“Even if I were dead, there will still be my children, my
grandchildren, my great grandchildren, my great great grandchildren.
They descendants will go on forever. But these mountains will not
grow any taler. We shall level them one day!” he declared with
confidence.
The wise old man was totally silenced.
When the guardian gods of the mountains saw how determined Yugong and
his crew were, they were struck with fear and reported the incident
to the Emperor of Heavens.
Filled with admiration for Yugong, the Emperor of Heavens ordered two
mighty gods to carry the mountains away.
'''

# 通过模块导入stats_word,调用stats_text统计字符串样本中中文汉字和英文单词出现的次数
import stats_word
stats_word.stats_text(text)
Ejemplo n.º 28
0
import stats_word

#text = 'abc123你是谁'
text = 2345

if __name__ == '__main__':
    print('合并统计中英文频次')
    try:
        list1 = stats_word.stats_text(text)
        print(list1)
    except ValueError:
        print('请传入正确的字符串给stats_text()函数!')
Ejemplo n.º 29
0
太行,王屋二山的北面,住了一個九十歲的老翁,名叫愚公。二山佔地廣闊,擋住去路,使他和家人往來極為不便。
一天,愚公召集家人說:「讓我們各盡其力,剷平二山,開條道路,直通豫州,你們認為怎樣?」
大家都異口同聲贊成,只有他的妻子表示懷疑,並說:「你連開鑿一個小丘的力量都沒有,怎可能剷平太行、王屋二山呢?況且,鑿出的土石又丟到哪裏去呢?」
大家都熱烈地說:「把土石丟進渤海裏。」
於是愚公就和兒孫,一起開挖土,把土石搬運到渤海去。
愚公的鄰居是個寡婦,有個兒子八歲也興致勃勃地走來幫忙。
寒來暑往,他們要一年才能往返渤海一次。
住在黃河河畔的智叟,看見他們這樣辛苦,取笑愚公說:「你不是很愚蠢嗎?你已一把年紀了,就是用盡你的氣力,也不能挖去山的一角呢?」
愚公歎息道:「你有這樣的成見,是不會明白的。你比那寡婦的小兒子還不如呢!就算我死了,還有我的兒子,我的孫子,我的曾孫子,他們一直傳下去。而這二山是不會加大的,總有一天,我們會把它們剷平。」
智叟聽了,無話可說:
二山的守護神被愚公的堅毅精神嚇倒,便把此事奏知天帝。天帝佩服愚公的精神,就命兩位大力神揹走二山。
How The Foolish Old Man Moved Mountains
Yugong was a ninety-year-old man who lived at the north of two high mountains, Mount Taixing and Mount Wangwu.
Stretching over a wide expanse of land, the mountains blocked yugong’s way making it inconvenient for him and his family to get around.
One day yugong gathered his family together and said,”Let’s do our best to level these two mountains. We shall open a road that leads to Yuzhou. What do you think?”
All but his wife agreed with him.
“You don’t have the strength to cut even a small mound,” muttered his wife. “How on earth do you suppose you can level Mount Taixin and Mount Wanwu? Moreover, where will all the earth and rubble go?”
“Dump them into the Sea of Bohai!” said everyone.
So Yugong, his sons, and his grandsons started to break up rocks and remove the earth. They transported the earth and rubble to the Sea of Bohai.
Now Yugong’s neighbour was a widow who had an only child eight years old. Evening the young boy offered his help eagerly.
Summer went by and winter came. It took Yugong and his crew a full year to travel back and forth once.
On the bank of the Yellow River dwelled an old man much respected for his wisdom. When he saw their back-breaking labour, he ridiculed Yugong saying,”Aren’t you foolish, my friend? You are very old now, and with whatever remains of your waning strength, you won’t be able to remove even a corner of the mountain.”
Yugong uttered a sigh and said,”A biased person like you will never understand. You can’t even compare with the widow’s little boy!”
“Even if I were dead, there will still be my children, my grandchildren, my great grandchildren, my great great grandchildren. They descendants will go on forever. But these mountains will not grow any taler. We shall level them one day!” he declared with confidence.
The wise old man was totally silenced.
When the guardian gods of the mountains saw how determined Yugong and his crew were, they were struck with fear and reported the incident to the Emperor of Heavens.
Filled with admiration for Yugong, the Emperor of Heavens ordered two mighty gods to carry the mountains away.
'''
#运行导入的函数
stats_text(text)
Ejemplo n.º 30
0
Mount Wanwu? Moreover, where will all the earth and rubble go?”
“Dump them into the Sea of Bohai!” said everyone.
So Yugong, his sons, and his grandsons started to break up rocks and
remove the earth. They transported the earth and rubble to the Sea of
Bohai.
Now Yugong’s neighbour was a widow who had an only child eight years
old. Evening the young boy offered his help eagerly.
Summer went by and winter came. It took Yugong and his crew a full
year to travel back and forth once.
On the bank of the Yellow River dwelled an old man much respected for
his wisdom. When he saw their back-breaking labour, he ridiculed
Yugong saying,”Aren’t you foolish, my friend? You are very old now,
and with whatever remains of your waning strength, you won’t be able
to remove even a corner of the mountain.”
Yugong uttered a sigh and said,”A biased person like you will never
understand. You can’t even compare with the widow’s little boy!”
“Even if I were dead, there will still be my children, my
grandchildren, my great grandchildren, my great great grandchildren.
They descendants will go on forever. But these mountains will not
grow any taler. We shall level them one day!” he declared with
confidence.
The wise old man was totally silenced.
When the guardian gods of the mountains saw how determined Yugong and
his crew were, they were struck with fear and reported the incident
to the Emperor of Heavens.
Filled with admiration for Yugong, the Emperor of Heavens ordered two
mighty gods to carry the mountains away.
'''

print(stats_word.stats_text(text))