NLP-采集分析考研英语真题高频词汇

代码:https://github.com/Kingsea442/nlp_word/tree/master(target=_blank)
生成词汇top1000:http://www.wanglh.top/post/blog/kao-yan-ying-yu-gao-pin-ci-hui(target=_blank)

使用wordcloud分析前1000个重要词汇生成的词云图。


image.png

核心思想

想法:首先一般阅读中出现的不认识的词汇可能就那么几个不常见的,所以过滤掉常见的词,比如the,and,if,you等,剩下的应该就是比较重要的词汇。所以可以直接采用TF-IDF简单粗暴提取英语阅读中的重要词汇。

TF-IDF算法介绍

TF-IDF(term frequency–inverse document frequency,词频-逆向文件频率)是一种用于信息检索(information retrieval)与文本挖掘(text mining)的常用加权技术。
TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度。字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料库中出现的频率成反比下降。

TF是词频(Term Frequency)

词频(TF)表示词条(关键字)在文本中出现的频率。这个数字通常会被归一化(一般是词频除以文章总词数), 以防止它偏向长的文件。


image.png

image.png

IDF是逆文档评率(Inverse Document Frequency)

逆文档频率 (IDF) :某一特定词语的IDF,可以由总文件数目除以包含该词语的文件的数目,再将得到的商取对数得到。如果包含词条t的文档越少, IDF越大,则说明词条具有很好的类别区分能力。


image.png

image.png

文件集合中的低文件频率,可以产生出高权重的TF-IDF。因此,TF-IDF倾向于过滤掉常见的词语,保留重要的词语。
TF-IDF=TF * IDF

python爬虫题库

用python直接爬取都学网的考研英语题库,2010到2020年共11年的题库。分析都学网网站,可以直接通过接口请求获取到文档数据,不需要解析html,比较方便。

清洗文档数据

爬取后的数据还是会有些特殊符号,标点符号,需要进行清洗,采用下面代码清洗数据。

class TxtUtils :
    def clean_txt(txt):
        import re
        p1 = re.compile(r'-\{.*?(zh-hans|zh-cn):([^;]*?)(;.*?)?\}-')
        p2 = re.compile(r'[(][: @ . , ?!\s][)]')
        p3 = re.compile(r'[「『]')
        p4 = re.compile(r'[\s+\.\!\/_,$%^*(+\"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()0-9 , : ; \-\ \[\ \]\ ]')
        txt = p1.sub(r' ', txt)
        txt = p2.sub(r' ', txt)
        txt = p3.sub(r' ', txt)
        txt = p4.sub(r' ', txt)
        return txt

分词

采用nltk对文档进行分词。

nltk参考文档(target=_blank).

from nltk.tokenize import word_tokenize
tokenize_words = word_tokenize(txt, language='english', preserve_line=True)

自定义停用词库

使用nltk自带的停用词只能过滤掉一些语气词比如emm,还远不能过滤掉我们想过滤掉的词比如 the,and,he,you这些简单词汇。从两方面处理,假设文档出现次数最多的词肯定不是我们不认识的词,可以过滤掉,假设4个字母一下的单词我们也都认识,可以直接过滤掉。这样只保留长度在四个以上,出现次数并不是很多的单词。然后用自己准备的停用词库,再去分词。

分析词的原型

分完词之后,取词的原型进行后续的计算,有待优化。

 lemmatizer = WordNetLemmatizer()
 lemmatizer.lemmatize('cats')
# 输出cat

统计词频和TF和逆文档频率IDF

采用nltk的FreqDist生成词典,进一步分析单词,算出tf-idf,最终得到一个分值。

from __future__ import division

import math
import os

import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize

import constant
from file_utils import FileUitls
from stopwords import PapperStopWords
from txt_utils import TxtUtils


def dist(txt):
    tokenize_words = word_tokenize(txt, language='english', preserve_line=True)
    filter_tokenize_word = [w for w in tokenize_words if not w in stop_words]
    return nltk.FreqDist(filter_tokenize_word)

def count_word_document(word, document_freq_dists):
    count = 0
    for fd in document_freq_dists:
        if fd.get(word) is not None:
            # count = count + 1
            count = count + fd.get(word)
    return count

def top200(word_dict):
    for k, v in word_dict.items():
        if v > 0.01 and v < 0.1:
            print(k, v)


if __name__ == '__main__':
    file_parent_path = constant.paper_data_file_path

    stop_words = set(stopwords.words('english'))
    for word in PapperStopWords.load_stop_words():
        stop_words.add(word)

    dir = os.listdir(file_parent_path)
    document_count = len(dir)
    print(document_count)

    freq_dist = []
    for f in dir:
        f_content = open(file_parent_path + f, 'r').read()
        f_content = TxtUtils.clean_txt(f_content)
        fd = dist(f_content)
        freq_dist.append(fd)

    word_tf_idf = {}
    word_freq = {}
    for fd in freq_dist:
        items = fd.items()
        total_word_count = fd.N()

        for k, v in items:
            tf = v / total_word_count

            word_in_document_count = count_word_document(k, freq_dist)
            idf = math.log((document_count / (word_in_document_count + 1)))

            tf_idf = tf * idf + math.log(min(10, len(k)))

            word_tf_idf[k] = [tf, idf, tf_idf]
    result = sorted(word_tf_idf.items(), key=lambda d: d[1][2])
    result.reverse()

    lemmatizer = WordNetLemmatizer()
    for k, v in result:
        k = lemmatizer.lemmatize(k)
        if len(k) >= 4:
            word_freq[k] = v[2]
            print(k,  v)

    final_result = ''
    for k, v in word_freq.items():
        line = str.format('{} {}\n', k, round(v, 7))
        final_result = final_result + line
    FileUitls.write(final_result, constant.result_file)

一般单词越长,一般会越陌生,所以这里可以把单词的长度作为一个影响因素计算到单词总分值中,这样最后排序,保存结果

生成词云

用wordcloud可以定制生成词语。


image.png
import src.constant
from file_utils import FileUitls

lines = FileUitls.reade_lines(src.constant.result_file)

result_cn = ''
word_freq = {}
for l in lines:
    l = str.replace(l, '\n', '')
    split = l.split(' ')
    word_freq[split[0]] = float(split[1])

w = WordCloud(background_color='white', width=800, height=450, scale=1.5)
w.generate_from_frequencies(word_freq)
w.to_file("top_word.jpg")

根据给定的图片生成词云


image.png
import FileUitls
from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image


lines = FileUitls.reade_lines(src.constant.result_file)

result_cn = ''
word_freq = {}
for l in lines[0:500]:
    l = str.replace(l, '\n', '')
    split = l.split(' ')
    word_freq[split[0]] = float(split[1])
print(word_freq)
color_mask = np.array(Image.open("/Users/wlh/asea/workspace/python/nlp_word/data/wlh.png"))

w = WordCloud(mask=color_mask, background_color='white', width=1000, height=1000)
w.generate_from_frequencies(word_freq)
image_colors = ImageColorGenerator(color_mask)

# 在只设置mask的情况下 会得到一个拥有图片形状的词云 axis默认为on 会开启边框
plt.imshow(w, interpolation="bilinear")
plt.axis("on")
plt.savefig("a.jpg")

# 直接在构造函数中直接给颜色 这种方式词云将会按照给定的图片颜色布局生成字体颜色策略
plt.imshow(w.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("on")
plt.savefig("w_i.jpg")
w.to_file("w.jpg")

单词翻译

直接调用百度api,翻译top n的单词即可。

统计结果

http://www.wanglh.top/post/blog/kao-yan-ying-yu-gao-pin-ci-hui(target=_blank)
overweight 超重 2.4077606
investment 投资 2.3305572
Generation 代 2.3756917
unemployed 失业 2.3750858
newspaper 报纸 2.2356268
involuntary 非自愿的 2.3667842
agricultural 农业的 2.3660215
immigration 移民 2.3633107
emotionally 情感上 2.3603616
immigrant 移民 2.2186755
officially 正式 2.3524406
identity 身份 2.132099
discrimination 歧视 2.3511983
representative 代表 2.3214329
hospitalization 住院治疗 2.3501396
communicating 沟通 2.3500997
classified 分类的 2.3497766
university 大学 2.3212058
globalization 全球化 2.3478006
landowner 地主 2.3478006
surprising 令人惊讶的 2.3474584
democratic 民主的 2.3473287
electrified 带电 2.3467428
unpleasant 不愉快的 2.3467428
graduating 毕业 2.346449
journalist 新闻工作者 2.3255884
revolution 革命 2.3442015
photograph 照片 2.3226091
introductory 引导的 2.3432567
disadvantage 缺点 2.3236484
Competition 竞争 2.3432567
experiment 实验 2.3177455
reputation 名声 2.3422138
affordable 负担得起的 2.3415207
flattering 奉承 2.3414661
attractiveness 吸引力 2.3414661
meritocratic 精英型 2.340512
frustration 挫折感 2.340512
governance 治理 2.340512
millisecond 毫秒 2.3402807
completing 正在完成 2.3400523
progressive 进步的 2.3393832
environmentally 有关环境方面地 2.3393832
Environment 环境 2.3393832
electricity 电 2.3389503
renewables 可再生能源 2.3389503
procedure 程序 2.3383197
homebuying 购房 2.3382681
majority 大多数 2.0992894
difference 差异 2.3182228
environmentalist 环保主义者 2.3358221
electrical 与电有关的 2.3355658
fulfillment 履行 2.3354541
architect 建筑师 2.2157246
architecture 建筑学 2.3352945
incredibly 难以置信地 2.3352627
inadvertently 疏忽地 2.3352627
undoubtedly 毋庸置疑地 2.3352627
interaction 相互作用 2.3223995
technology 技术 2.3347211
interested 感兴趣的 2.3341363
educational 教育的 2.3338604
considered 考虑过的 2.3337168
biography 传记 2.3336186
exhibition 展览 2.3331672
moisturizers 保湿霜 2.3330387
employment 就业 2.3330217
inadequate 不足 2.3329895
methodically 有条不紊 2.3329895
clinically 临床上 2.3329895
sustaining 维持 2.3329895
adjustment 调整 2.3329895
nonparents 非亲子 2.3329895
conventional 传统的 2.3329895
contradicting 矛盾的 2.3329895
Researchers 研究人员 2.3329895
civilization 文明 2.3327274
exhaustion 疲惫 2.3327274
correspondent 记者 2.3327274
personality 人格 2.3239053
Government 政府 2.3327274
abbreviation 缩写 2.3327274
adventurism 冒险主义 2.3327274
foreigner 外国人 2.3327274
intellectual 智力的 2.3327274
counterbalance 平衡 2.3327274
depression 抑郁 2.3327274
purposelessness 无目的性 2.3327274
speculated 推测 2.3327274
connection 连接 2.317904
reportedly 据说 2.3322165
embarrassed 尴尬 2.3322165
summarizes 总结 2.3322165
unthinkable 不可思议 2.3322165
smartphones 智能手机 2.3322165
acquaintance 相识 2.3322165
disruptive 破坏性的 2.3322165
protection 保护 2.3322165

待优化

使用nltk分析单词命名实体,去掉单词中人名,地名等专有名词。

代码

https://github.com/Kingsea442/nlp_word/tree/master

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容