中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Python無監(jiān)督抽詞

2019-03-22    來源:夜息博客

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

如何快速正確分詞,對(duì)于SEO來說,是提取tags聚合,信息關(guān)聯(lián)的好幫手。

目前很多分詞工具都是基于一元的分詞法,需要詞庫來輔助。

通過對(duì)Google黑板報(bào)第一章的學(xué)習(xí),如何利用統(tǒng)計(jì)模型進(jìn)行分詞。

本方法考慮了3個(gè)維度:

凝聚程度:兩個(gè)字連續(xù)出現(xiàn)的概率并不是各自獨(dú)立的程度。例如“上”出現(xiàn)的概率是1×10^-5,”床”出現(xiàn)的概率是1×10^-10,如果這兩個(gè)字的凝聚程度低,則”上床”出現(xiàn)的概率應(yīng)該和1×10^-15接近,但是事實(shí)上”上床”出現(xiàn)的概率在1×10^-11次方,遠(yuǎn)高于各自獨(dú)立概率之積。所以我們可以認(rèn)為“上床”是一個(gè)詞。

左鄰字聚合熵:分出的詞左邊一個(gè)字的信息量,比如”巴掌”,基本只能用于”打巴掌”,“一巴掌”,“拍巴掌”,反之”過去”這個(gè)詞,前面可以用“走過去”,“跑過去”,“爬過去”,“打過去”,“混過去”,“睡過去”,“死過去”,“飛過去”等等,信息熵就非常高。

右鄰字聚合熵:分出的詞右邊一個(gè)詞的信息量,同上。

下面是一個(gè)利用Python實(shí)現(xiàn)的demo(轉(zhuǎn)自:http://www.webinfoextract.com/forum.php?mod=viewthread&tid=20)

#!/bin/sh
 
python ./splitstr.py > substr.freq
 
python ./cntfreq.py > word.freq
 
python ./findwords.py > result
 
sort -t"    " -r -n -k 2 result > result.sort

splitstr.py,切分出字?jǐn)?shù)在10以內(nèi)的子字符串,計(jì)算詞頻,左鄰字集合熵,右鄰字集合熵,并輸出出現(xiàn)10次以上的子字符串:

import math
 
def compute_entropy(word_list):
        wdict={}
        tot_cnt=0
        for w in word_list:
                if w not in wdict:
                        wdict[w] = 0
                wdict[w] += 1
                tot_cnt+=1
        ent=0.0
        for k,v in wdict.items():
                p=1.0*v/tot_cnt
                ent -= p * math.log(p)
        return ent
 
def count_substr_freq():
        fp = open("./video.corpus")
        str_freq={}
        str_left_word={}
        str_right_word={}
        tot_cnt=0
        for line in fp:
                line=line.strip('\n')
                st = line.decode('utf-8')
                l=len(st)
                for i in range(l):
                        for j in range(i+1,l):
                                if j - i  0:
                                                left_word=st[i-1]
                                        else:
                                                left_word='^'
                                        if j < l-1:                                                 right_word=st[j+1]                                         else:                                                 right_word='%'                                         str_left_word[w].append(left_word)                                         str_right_word[w].append(right_word)                                         tot_cnt+=1         for k,v in str_freq.items():                 if v >= 10:
                        left_ent=compute_entropy(str_left_word[k])
                        right_ent=compute_entropy(str_right_word[k])
                        print "%s\t%f\t%f\t%f"%(k,v*1.0/tot_cnt,left_ent,right_ent)
 
if __name__ == "__main__":
        count_substr_freq()

cntfreq.sh,統(tǒng)計(jì)每個(gè)字的字頻:

def count_freq():
    word_freq={}
    fp = open("./substr.freq")
    tot_cnt=0.0
    for line in fp:
        line=line.split('\t')
        if len(line) < 2:
            continue
        st = line[0].decode('utf-8')
        freq = float(line[1])
        for w in st:
            if w not in word_freq:
                word_freq[w]=0.0
            word_freq[w]+=freq
            tot_cnt+=freq
    while True:
        try:
            x,y = word_freq.popitem()
            if x:
                freq=y*1.0/tot_cnt
                print "%s\t%f"%(x.encode('utf-8'),freq)
            else:
                break
        except:
            break
 
if __name__ == "__main__":
    count_freq()

findwords.py,輸出凝合程度高,且左右鄰字集合熵都較高的字符串:

def load_dict(filename):
        dict={}
        fp=open(filename)
        for line in fp:
                line=line.strip('\n')
                item=line.split('\t')
                if len(item) == 2:
                        dict[item[0]] = float(item[1])
        return dict
 
def compute_prob(str,dict):
        p=1.0
        for w in str:
                w = w.encode('utf-8')
                if w in dict:
                        p *= dict[w]
        return p
 
def is_ascii(s):
        return all(ord(c) < 128 for c in s)
 
def find_compact_substr(dict):
        fp = open("./substr.freq")
        str_freq={}
        for line in fp:
                line = line.decode('utf-8')
                items = line.split('\t')
                if len(items) < 4:
                        continue
                substr = items[0]
                freq = float(items[1])
                left_ent = float(items[2])
                right_ent = float(items[3])
                p=compute_prob(substr,dict)
                freq_ratio=freq/p
                if freq_ratio > 5.0 and left_ent > 2.5 and right_ent > 2.5 and len(substr) >= 2 and not is_ascii(substr):
                        print "%s\t%f"%(substr.encode('utf-8'),freq)
 
if __name__ == "__main__":
        dict=load_dict('./word.freq')
        find_compact_substr(dict)

對(duì)3萬條視頻的標(biāo)題,抽出的頻率最高的50個(gè)詞如下:

50視頻 0.000237

軸承 0.000184

北京 0.000150

中國 0.000134

高清 0.000109

搞笑 0.000101

新聞 0.000100

上海 0.000100

美女 0.000092

演唱 0.000085

音樂 0.000082

—— 0.000082

第二 0.000080

少女 0.000078

最新 0.000074

廣場 0.000070

世界 0.000070

現(xiàn)場 0.000066

娛樂 0.000066

大學(xué) 0.000064

公司 0.000064

舞蹈 0.000063

電視 0.000063

教學(xué) 0.000060

我們 0.000060

國語 0.000059

經(jīng)典 0.000056

字幕 0.000055

宣傳 0.000053

鋼管 0.000051

游戲 0.000050

電影 0.000049

演唱會(huì) 0.000046

日本 0.000045

小學(xué) 0.000045

快樂 0.000044

超級(jí) 0.000043

第三 0.000042

寶寶 0.000042

學(xué)生 0.000042

廣告 0.000041

培訓(xùn) 0.000041

視頻 0.000040

美國 0.000040

愛情 0.000039

老師 0.000038

動(dòng)畫 0.000038

教程 0.000037

廣州 0.000037

學(xué)院 0.000035

文章來源:http://www.imyexi.com/?p=682

標(biāo)簽: Python 分詞工具 中文分詞 

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:分享站長應(yīng)該避免的5大優(yōu)化借口

下一篇:總結(jié)讓訪客為你創(chuàng)造內(nèi)容的六大理由