NLP

内容

  1. 基本文本处理技能
    1.1 分词的概念(分词的正向最大、逆向最大、双向最大匹配法);
    1.2 词、字符频率统计;(可以使用Python中的collections.Counter模块,也可以自己寻找其他好用的库)
  2. 2.1 语言模型中unigram、bigram、trigram的概念;
    2.2 unigram、bigram频率统计;(可以使用Python中的collections.Counter模块,也可以自己寻找其他好用的库)
  3. 文本矩阵化:要求采用词袋模型且是词级别的矩阵化
    步骤有:
    3.1 分词(可采用结巴分词来进行分词操作,其他库也可以);
    3.2 去停用词;构造词表。
    3.3 每篇文档的向量化。

实现

  1. 中文分词:汉语自然语言处理的第一部分是中文分词。因为中文不像英文那样,实行分词连写,即词与词之间用空格分隔。
    在分析中文文本之前必须将一个汉字序列切分成一个一个单独的词,这个过程称为中文分词。

中文分词主要分为机械师分词法(基于词典),基于语法和规则分词法,基于统计分词法。

最大匹配法:最大匹配是指以词典为依据,取词典中最长单词为第一个次取字数量的扫描串,在词典中进行扫描(为提升扫描效率,还可以跟据字数多少设计多个字典,然后根据字数分别从不同字典中进行扫描)。例如:词典中最长词为“中华人民共和国”共7个汉字,则最大匹配起始字数为7个汉字。然后逐字递减,在对应的词典中进行查找。
(1)正向最大匹配法:正向即从前往后取词,从7->1,每次减一个字,直到词典命中或剩下1个单字。
(2)逆向最大匹配法:逆向即从后往前取词,其他逻辑和正向相同。
(3)双向最大匹配法:两种算法都切一遍,然后根据大颗粒度词越多越好,非词典词和单字词越少越好的原则,选取其中一种分词结果输出。
2. 词、字符频率统计:

import jieba
txt = open("红楼梦.txt", "r", encoding="gb18030").read()

import collections

txt1 = txt
txt1 = txt1.replace('\n', '')  # 删掉换行符
txt1 = txt1.replace(',', '')  # 删掉逗号
txt1 = txt1.replace('。', '')  # 删掉句号
mylist = list(txt1)
mycount = collections.Counter(mylist)
for key, val in mycount.most_common(10):  # 有序(返回前10个)
    print(key, val)

  1. 语言模型中unigram、bigram、trigram的概念:
    unigram 一元分词,把句子分成一个一个的汉字
    bigram 二元分词,把句子从头到尾每两个字组成一个词语
    trigram 三元分词,把句子从头到尾每三个字组成一个词语.

  2. unigram、bigram频率统计:


#!/usr/bin/env python

class NGram(object):

    def __init__(self, n):

        # n is the order of n-gram language model

        self.n = n

        self.unigram = {
   }

        self.bigram = {
   }

 

    # scan a sentence, extract the ngram and update their

    # frequence.

    #

    # @param sentence list{str}

    # @return none

    def scan(self, sentence):

        # file your code here

        for line in sentence:

            self.ngram(line.split())

        #unigram

        if self.n == 1:

            try:

                fip = open("data.uni","w")

            except:

                print >> sys.stderr ,"failed to open data.uni"

            for i in self.unigram:

                fip.write("%s %d\n" % (i,self.unigram[i]))

        if self.n == 2:

            try:

                fip = open("data.bi","w")

            except:

                print >> sys.stderr ,"failed to open data.bi"

            for i in self.bigram:

                fip.write("%s %d\n" % (i,self.bigram[i]))

    # caluclate the ngram of the words

    #

    # @param words list{str}

    # @return none

    def ngram(self, words):

        # unigram

        if self.n == 1:

            for word in words:

                if word not in self.unigram:

                    self.unigram[word] = 1

                else:

                    self.unigram[word] = self.unigram[word] + 1

 

        # bigram

        if self.n == 2:

            num = 0

            stri = ''

            for i in words:

                num = num + 1

                if num == 2:

                    stri  = stri + " "

                stri = stri + i

                if num == 2:

                    if stri not in self.bigram:

                        self.bigram[stri] = 1

                    else:

                        self.bigram[stri] = self.bigram[stri] + 1

                    num = 0

                    stri = ''

 

if __name__=="__main__":

    import sys

    try:

        fip = open(sys.argv[1],"r")

    except:

        print >> sys.stderr, "failed to open input file"

    sentence = []

    for line in fip:

        if len(line.strip())!=0:

            sentence.append(line.strip())

    uni = NGram(1)

    bi = NGram(2)

    uni.scan(sentence)

    bi.scan(sentence)

  1. 文本矩阵化:要求采用词袋模型且是词级别的矩阵化
    步骤有:
    1) 分词(可采用结巴分词来进行分词操作,其他库也可以);
    2) 去停用词;构造词表。
    3) 每篇文档的向量化。

参考文献

https://blog.csdn.net/weixin_41781408/article/details/88146030#3__270
https://blog.csdn.net/niuox/article/details/11395397