天天看點

python 用字元串生成詞雲圖

python 用字元串生成詞雲圖

更多jieba相關内容: jieba中文處理

1.大緻步驟

1. 從mysql讀取資料

2. 分詞分

3. 生成詞雲圖

2.用到的庫

- jieba 分詞

- wordcloud 詞雲生成和儲存

3.代碼如下:

#連接配接資料庫
def connect_mysql():
    conn = pymysql.connect(
        host='localhost',
        db='TESTDB',
        user='pymysql',
        passwd='123123',
        charset='utf8',           
        )
    cursor = conn.cursor()
    return cursor
           
#拿到标題和新聞内容
def process(cursor):
    sql="SELECT news,title FROM southcity where title<>'廣告'"
    length=cursor.execute(sql) #傳回一個int資料為記錄數目
    result=cursor.fetchall()  #傳回一個tuple
    news=result
    return news,length
           
#分詞并傳回以空格分隔的分詞字元串
def cut_word(news,length):
    blacklist=[' ','  ','(',')','(',')','']
    contents=[]
    for i in range(length):
        seg_list = jieba.cut(str(news[i][]),cut_all=False)
        seg_list=list(seg_list)
        # for j in range(len(seg_list)):
        #     word=str(seg_list[j]
        #     # if len(word)<=1 or word in blacklist:
        #     #     del seg_list[j]
        content= " ".join(seg_list) #精确模式
        contents.append(content)
    return contents
           
#生成詞雲圖
def wordcloud(contents,news):
    print(len(contents))
    for i in range(len(contents)):
        wc = WordCloud(font_path=u"static/fonts/simhei.ttf",
               max_words=,
               width=,
               height=,
               background_color="black",
               margin=)
        try:
            wc.generate(contents[i])
            wc.to_file('result2/%s.png'%news[i][])
        except:
            print("四BAI")
           

3.可參考内容:

  1. NLP可視化: 用Python生成詞雲(偏向預處理、主題詞提取)
  2. Python + worldcloud + jieba 十分鐘學會用任意中文文本生成詞雲

    (沒用上,自定義部分介紹的比較多)

  3. 結巴分詞