前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NLP文本表示之实战

NLP文本表示之实战

作者头像
伍六七AI编程
发布2020-05-11 17:31:06
2990
发布2020-05-11 17:31:06
举报
文章被收录于专栏:preparedpreparedprepared

在上一篇文章介绍了文本表示《NLP之文本表示》https://blog.csdn.net/Prepare...

但是没有代码。在这篇博客中,我们在实践一下!

中文分词常用模型:Jieba模型、百度的LAC模型,这里使用 Jieba 模型进行中文分词。

数据集使用:人民日报1946年05月的数据。数据集地址:https://github.com/fangj/rmrb/tree/master/example/1946年05月

Jieba模型:

Jieba 基本上目前最好的 Python 中文分词组件,它主要有以下 3 种特性:

支持 3 种分词模式:

  • 精确模式 全模式 搜索引擎模式 支持繁体分词 支持自定义词典

第一步:读取所有文件,形成数据集corpus

def getCorpus(self, rootDir):
    corpus = []
    r1 = u'[a-zA-Z0-9’!"#$%&\'()*+,-./::;<=>?@,。?★、…【】《》?“”‘’![\\]^_`{|}~]+'  # 用户也可以在此进行自定义过滤字符
    for file in os.listdir(rootDir):
        path  = os.path.join(rootDir, file)
        if os.path.isfile(path):
            # 打印文件地址
            #print(os.path.abspath(path))
            # 获取文章内容,fromfile主要用来处理数组 pass
            # filecontext = np.fromfile(os.path.abspath(path))
            with open(os.path.abspath(path), "r", encoding='utf-8') as file:
                filecontext = file.read();
                #print(filecontext)
                # 分词 去掉符号
                filecontext = re.sub(r1, '', filecontext)
                filecontext = filecontext.replace("\n", '')
                filecontext = filecontext.replace(" ", '')
                seg_list = jieba.lcut_for_search(filecontext)
                corpus += seg_list
                #print(seg_list)
                #print("[精确模式]:" + "/".join(seg_list))
        elif os.path.isdir(path):
            TraversalFun.AllFiles(self, path)
    return corpus

1、循环读取文件夹下的文件;

2、使用numpy读取文件获取文件内容;

3、去掉特殊符号,获取中文内容;

4、使用Jieba分词进行分词,大家可以尝试各种方式。

第二步:计算信息熵

信息熵的公式:

img
img
#构造词典,统计每个词的频率,并计算信息熵
def calc_tf(corpus):
    #   统计每个词出现的频率
    word_freq_dict = dict()
    for word in corpus:
        if word not in word_freq_dict:
            word_freq_dict[word] = 1
        word_freq_dict[word] += 1
    # 将这个词典中的词,按照出现次数排序,出现次数越高,排序越靠前
    word_freq_dict = sorted(word_freq_dict.items(), key=lambda x:x[1], reverse=True)
    # 计算TF概率
    word_tf = dict()
    # 信息熵
    shannoEnt = 0.0
    # 按照频率,从高到低,开始遍历,并未每个词构造一个id
    for word, freq in word_freq_dict:
        # 计算p(xi)
        prob = freq / len(corpus)
        word_tf[word] = prob
        shannoEnt -= prob*log(prob, 2)
    return word_tf, shannoEnt

思路:

1、循环,计算每个词的频率,循环一个词,如果有则加1,没有则等于1

2、计算信息熵:按照公式循环计算信息熵。

结果:

数据集大小,size: 163039信息熵:14.047837802885464

Jieba模型:https://github.com/fxsjy/jieba

源码地址:https://github.com/zhongsb/NL...

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档