├── positive.txt ├── tradition2simple.py ├── fasttext.py ├── separate.py ├── word2vec.py ├── remove.py ├── xml2txt.py ├── negative.txt ├── orientation.py ├── stopwords.txt └── readme.md /positive.txt: -------------------------------------------------------------------------------- 1 | 盈利 2 | 上市 3 | 投产 4 | 创新 5 | 上涨 6 | 大涨 7 | 上升 8 | 成功 9 | 新高 10 | 亮相 11 | 收购 12 | 并购 13 | 兼并 14 | 慈善 15 | 公益 16 | 积极 17 | 进军 18 | 投资 19 | 扩张 20 | 开创 21 | 良性 22 | 增长 23 | 开拓 24 | 拓展 25 | 重振 26 | 开辟 27 | 和解 28 | 改革 29 | 利好 30 | 革新 31 | 巅峰 32 | 畅销 33 | 平衡 34 | 先驱 35 | 突破 36 | 稳定 37 | 良好 38 | 获利 39 | 收益 40 | 升值 41 | 攀升 42 | 吸纳 43 | 佳绩 44 | 增进 45 | 联盟 46 | 结盟 47 | 合作 48 | 洽谈 49 | 热门 50 | 热销 51 | 共识 52 | 深化 53 | 壮大 54 | 巩固 55 | 重回 56 | 强盛 57 | 高涨 58 | 重拾 59 | 发展 60 | 信心 61 | 自信 62 | 诚意 63 | 期望 64 | 加入 65 | 升级 66 | 领先 67 | 携手 68 | 成立 69 | 入股 70 | 合作 71 | 推出 72 | 举办 73 | 文化 74 | 体育 75 | 科技 76 | 环保 77 | 教育 78 | 领衔 79 | 抢眼 80 | 好评 81 | 健康 -------------------------------------------------------------------------------- /tradition2simple.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import zhconv 3 | 4 | print('主程序执行开始...') 5 | 6 | input_file_name = 'wiki.cn.txt' 7 | output_file_name = 'wiki.cn.simple.txt' 8 | input_file = open(input_file_name, 'r', encoding='utf-8') 9 | output_file = open(output_file_name, 'w', encoding='utf-8') 10 | 11 | print('开始读入繁体文件...') 12 | lines = input_file.readlines() 13 | print('读入繁体文件结束!') 14 | 15 | print('转换程序执行开始...') 16 | count = 1 17 | for line in lines: 18 | output_file.write(zhconv.convert(line, 'zh-hans')) 19 | count += 1 20 | if count % 10000 == 0: 21 | print('目前已转换%d条数据' % count) 22 | print('转换程序执行结束!') 23 | 24 | print('主程序执行结束!') 25 | -------------------------------------------------------------------------------- /fasttext.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import multiprocessing 3 | from gensim.models import FastText 4 | from gensim.models.word2vec import LineSentence 5 | 6 | if __name__ == "__main__": 7 | print('主程序开始执行...') 8 | 9 | input_file_name = 'wiki.txt' 10 | model_file_name = 'wiki.model.fasttext' 11 | 12 | print('转换过程开始...') 13 | model = FastText(LineSentence(input_file_name), 14 | size=100, # 词向量长度为100 15 | window=5, 16 | min_count=5, 17 | workers=multiprocessing.cpu_count()) 18 | print('转换过程结束!') 19 | 20 | print('开始保存模型...') 21 | model.save(model_file_name) 22 | print('模型保存结束!') 23 | 24 | print('主程序执行结束!') 25 | -------------------------------------------------------------------------------- /separate.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import jieba 3 | 4 | print('主程序执行开始...') 5 | 6 | input_file_name = 'wiki.cn.simple.txt' 7 | output_file_name = 'wiki.cn.simple.separate.txt' 8 | input_file = open(input_file_name, 'r', encoding='utf-8') 9 | output_file = open(output_file_name, 'w', encoding='utf-8') 10 | 11 | print('开始读入数据文件...') 12 | lines = input_file.readlines() 13 | print('读入数据文件结束!') 14 | 15 | print('分词程序执行开始...') 16 | count = 1 17 | for line in lines: 18 | # jieba分词的结果是一个list,需要拼接,但是jieba把空格回车都当成一个字符处理 19 | output_file.write(' '.join(jieba.cut(line.split('\n')[0].replace(' ', ''))) + '\n') 20 | count += 1 21 | if count % 10000 == 0: 22 | print('目前已分词%d条数据' % count) 23 | print('分词程序执行结束!') 24 | 25 | print('主程序执行结束!') 26 | -------------------------------------------------------------------------------- /word2vec.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import multiprocessing 3 | from gensim.models import Word2Vec 4 | from gensim.models.word2vec import LineSentence 5 | 6 | if __name__ == "__main__": 7 | print('主程序开始执行...') 8 | 9 | input_file_name = 'wiki.txt' 10 | model_file_name = 'wiki.model' 11 | 12 | print('转换过程开始...') 13 | model = Word2Vec(LineSentence(input_file_name), 14 | size=100, # 词向量长度为100 15 | window=5, 16 | min_count=5, 17 | sg=1, 18 | workers=multiprocessing.cpu_count()) 19 | print('转换过程结束!') 20 | 21 | print('开始保存模型...') 22 | model.save(model_file_name) 23 | print('模型保存结束!') 24 | 25 | print('主程序执行结束!') 26 | -------------------------------------------------------------------------------- /remove.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import re 3 | 4 | print('主程序执行开始...') 5 | 6 | input_file_name = 'wiki.cn.simple.separate.txt' 7 | output_file_name = 'wiki.txt' 8 | input_file = open(input_file_name, 'r', encoding='utf-8') 9 | output_file = open(output_file_name, 'w', encoding='utf-8') 10 | 11 | print('开始读入数据文件...') 12 | lines = input_file.readlines() 13 | print('读入数据文件结束!') 14 | 15 | print('正则程序执行开始...') 16 | count = 1 17 | cn_reg = '^[\u4e00-\u9fa5]+$' 18 | 19 | for line in lines: 20 | line_list = line.split('\n')[0].split(' ') 21 | line_list_new = [] 22 | for word in line_list: 23 | if re.search(cn_reg, word): 24 | line_list_new.append(word) 25 | # print(line_list_new) 26 | output_file.write(' '.join(line_list_new) + '\n') 27 | count += 1 28 | if count % 10000 == 0: 29 | print('目前已正则%d条数据' % count) 30 | print('正则程序执行结束!') 31 | 32 | print('主程序执行结束!') 33 | -------------------------------------------------------------------------------- /xml2txt.py: -------------------------------------------------------------------------------- 1 | # coding; utf-8 2 | """ 3 | 这个代码是将从网络上下载的xml格式的wiki百科训练语料转为txt格式 4 | wiki百科训练语料 5 | 链接:https://pan.baidu.com/s/1eLkybiYOE_aVxsN0pALATg 6 | 密码:hmtn 7 | """ 8 | 9 | from gensim.corpora import WikiCorpus 10 | 11 | if __name__ == '__main__': 12 | 13 | print('主程序开始...') 14 | 15 | input_file_name = 'zhwiki-latest-pages-articles.xml.bz2' 16 | output_file_name = 'wiki.cn.txt' 17 | print('开始读入wiki数据...') 18 | input_file = WikiCorpus(input_file_name, lemmatize=False, dictionary={}) 19 | print('wiki数据读入完成!') 20 | output_file = open(output_file_name, 'w', encoding="utf-8") 21 | 22 | print('处理程序开始...') 23 | count = 0 24 | for text in input_file.get_texts(): 25 | output_file.write(' '.join(text) + '\n') 26 | count = count + 1 27 | if count % 10000 == 0: 28 | print('目前已处理%d条数据' % count) 29 | print('处理程序结束!') 30 | 31 | output_file.close() 32 | print('主程序结束!') 33 | -------------------------------------------------------------------------------- /negative.txt: -------------------------------------------------------------------------------- 1 | 上诉 2 | 纠纷 3 | 骗局 4 | 债务 5 | 逃税 6 | 偷税 7 | 漏税 8 | 破产 9 | 倒闭 10 | 诉讼 11 | 起诉 12 | 丑闻 13 | 索赔 14 | 赔偿 15 | 跳水 16 | 下滑 17 | 损失 18 | 骤降 19 | 造假 20 | 召回 21 | 翻新 22 | 盗版 23 | 违法 24 | 诽谤 25 | 衰退 26 | 衰败 27 | 重挫 28 | 抄袭 29 | 失败 30 | 惨败 31 | 损坏 32 | 盗窃 33 | 漏洞 34 | 争议 35 | 吐槽 36 | 糟糕 37 | 侵权 38 | 判决 39 | 抵制 40 | 造假 41 | 问题 42 | 乱象 43 | 打击 44 | 重创 45 | 停产 46 | 裁员 47 | 危机 48 | 炒作 49 | 故障 50 | 违约 51 | 大跌 52 | 警告 53 | 亏损 54 | 查封 55 | 恶性 56 | 恶意 57 | 暴利 58 | 假货 59 | 假冒 60 | 缺陷 61 | 违规 62 | 伪造 63 | 隐患 64 | 短缺 65 | 失衡 66 | 贬值 67 | 下跌 68 | 暴跌 69 | 急跌 70 | 下挫 71 | 风波 72 | 抹黑 73 | 污蔑 74 | 诬陷 75 | 造谣 76 | 欺诈 77 | 诈骗 78 | 非法 79 | 受贿 80 | 控告 81 | 不法 82 | 决裂 83 | 削弱 84 | 侵权 85 | 枯竭 86 | 匮乏 87 | 过剩 88 | 不符 89 | 雷同 90 | 抱怨 91 | 曝光 92 | 无缘 93 | 缩水 94 | 批评 95 | 泡沫 96 | 破灭 97 | 否认 98 | 违反 99 | 调查 100 | 疑云 101 | 变卦 102 | 变相 103 | 致命 104 | 差评 105 | 惨重 106 | 涉嫌 107 | 罚 108 | 惊吓 109 | 垄断 110 | 侵权 111 | 罚款 112 | 处罚 113 | 下滑 114 | 萎缩 115 | 抛弃 116 | 猥琐 117 | 下架 118 | 隐忧 119 | 水货 120 | 泡沫 121 | 负债 122 | 低迷 123 | 质疑 124 | 停工 125 | 停产 126 | 走私 127 | 无耻 128 | 缺乏 129 | 贪污 130 | 贪财 131 | 敛财 132 | 荒唐 133 | 阻碍 134 | 破烂 135 | 泛滥 136 | 混乱 137 | 心寒 138 | 严峻 139 | 减产 140 | 通货膨胀 141 | 膨胀 142 | 鄙视 143 | 疲惫 144 | 炒作 145 | 悲哀 146 | 愚蠢 147 | 可惜 148 | 卑劣 149 | 无情 150 | 悲哀 151 | 不安 152 | 停滞 153 | 崩溃 154 | 溃败 -------------------------------------------------------------------------------- /orientation.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import re 3 | import gensim 4 | import jieba.analyse 5 | 6 | print('主程序开始...') 7 | 8 | print('读取word2vec...') 9 | word2vec = gensim.models.Word2Vec.load('wiki.model') 10 | print('读取word2vec结束!') 11 | 12 | print('读取停用词...') 13 | stopwords = [] 14 | for word in open('stopwords.txt', 'r', encoding='utf-8'): 15 | if word.strip(): 16 | stopwords.append(word.strip()) 17 | jieba.analyse.set_stop_words('stopwords.txt') 18 | print(stopwords) 19 | print('停用词读取结束!') 20 | 21 | print('读取正向词...') 22 | positives = [] 23 | for word in open('positive.txt', 'r', encoding='utf-8'): 24 | if word.strip(): 25 | positives.append(word.strip()) 26 | print(positives) 27 | print('正向词读取结束!') 28 | 29 | print('读取负向词...') 30 | negatives = [] 31 | for word in open('negative.txt', 'r', encoding='utf-8'): 32 | if word.strip(): 33 | negatives.append(word.strip()) 34 | print(negatives) 35 | print('负向词读取结束!') 36 | 37 | print('加载自定义词库...') 38 | jieba.load_userdict('dict.txt') 39 | print('自定义词库加载结束!') 40 | 41 | print('关键词抽取...') 42 | title = '天津武清的三星工厂疑似发生火灾' 43 | content = '昨天,位于天津武清的三星工厂冒出滚滚浓烟,疑似发生火灾。\n三星方面回应称,目前已确认发生火灾的工厂为天津三星视界有限公司,初步推断是堆放废弃物的地方着火。\n昨天早上,有网友拍摄视频爆料位于天津武清的三星工厂冒出滚滚浓烟,疑似发生火灾。随后,天津消防武清支队发布公告称,起火的是三星视界有限公司,起火物质为生产车间内锂电池及部分半成品,现场已经报灭,无人员伤亡。不过这一消息随后被删除。三星方面表示,“锂电池起火”这一说法并不准确,初步推断是堆放废弃物的地方着火,具体原因和损失还在调查之中。消防称三星工厂内锂电池起火网友爆料的视频及图片显示,昨天早上,位于天津市武清区的三星工厂内冒出滚滚浓烟,数公里外的居民楼可见浓烟。视频中有网友介绍称,此前有过一波大的浓烟,后来减弱,不过随后浓烟又增大。由于网友多是远距离拍摄,因此从视频中无法确定火势及具体起火点。\n昨天下午15时许,天津市武清公安局公安消防支队证实了起火位置是位于天津市武清区开发区庆龄大道的三星视界有限公司的生产车间。武清消防支队称,起火物质是车间内锂电池及部分半成品。武清消防支队表示,起火时间为2月8日早上6时许,截止到昨天下午15时,现场已经报灭,正在组织排烟,无人员伤亡。此次火灾武清消防支队共出动19部消防车及110余名消防官兵前往现场处置。昨天晚间,天津市公安局武清分局发布消息,称2017年2月8日6时17分许,坐落于武清区的天津三星视界有限公司一废弃品仓库发生火灾。接到报警后,公安消防和属地武清分局警力迅速赶到现场,展开处置工作。7时55分火被扑灭,无人员伤亡,起火原因正在调查中。' 44 | keywords = jieba.analyse.textrank(content, topK=10, withWeight=True, allowPOS=('nt', 'ns', 'n', 'vn', 'v')) 45 | print(keywords) 46 | print('关键词抽取结束!') 47 | 48 | print('分句处理...') 49 | contents = re.split(u'。|\n', content) 50 | contents = filter(lambda s: (s if s != '' else None), contents) 51 | contents = list(contents) 52 | print(contents) 53 | print('分句处理结束!') 54 | 55 | print('停用词去除...') 56 | contents.append(title) 57 | results = [] 58 | for content in contents: 59 | seg = jieba.cut(content) 60 | results.append([]) 61 | for c in seg: 62 | if c.strip() != '' and c not in stopwords: 63 | results[-1].append(c) 64 | contents = results[:-1] 65 | title = results[-1] 66 | print(contents) 67 | print('停用词去除结束!') 68 | 69 | print('关键句抽取...') 70 | sims = [] 71 | for i in range(len(contents)): 72 | content = contents[i] 73 | content_sim = 0 74 | for word in content: 75 | max_sim = 0 76 | for keyword in keywords: 77 | try: 78 | temp_sim = word2vec.similarity(word, keyword[0]) 79 | if temp_sim > max_sim: 80 | max_sim = temp_sim 81 | content_sim += max_sim * float(keyword[1]) 82 | except: 83 | continue 84 | sims.append([content_sim / len(content), content]) 85 | sims.sort(key=(lambda x: x[0]), reverse=True) 86 | key_contents = [title, sims[0][1], sims[1][1]] 87 | print(key_contents) 88 | print('关键句抽取结束!') 89 | 90 | print('倾向性判断...') 91 | orientations = [] 92 | for content in key_contents: 93 | pos_num = 0 94 | pos_value = 0 95 | neg_num = 0 96 | neg_value = 0 97 | for word in content: 98 | neg = 0 99 | pos = 0 100 | for positive in positives: 101 | try: 102 | temp = word2vec.similarity(word, positive) 103 | if temp > pos: 104 | pos = temp 105 | except: 106 | continue 107 | for negative in negatives: 108 | try: 109 | temp = word2vec.similarity(word, negative) 110 | if temp > neg: 111 | neg = temp 112 | except: 113 | continue 114 | if pos > neg and pos > 0.5: 115 | pos_num += 1 116 | pos_value += pos 117 | if neg > pos and neg > 0.5: 118 | neg_num += 1 119 | neg_value += neg 120 | pos_ave = (pos_value / pos_num) if pos_num != 0 else 0 121 | neg_ave = (neg_value / neg_num) if neg_num != 0 else 0 122 | orientations.append(pos_ave if pos_ave > neg_ave else -neg_ave) 123 | print(orientations) 124 | pos_num = 0 125 | pos_sum = 0 126 | neg_num = 0 127 | neg_sum = 0 128 | for orientation in orientations: 129 | if orientation > 0: 130 | pos_num += 1 131 | pos_sum += orientation 132 | else: 133 | neg_num += 1 134 | neg_sum += orientation 135 | pos_orientation = pos_sum / pos_num if pos_num != 0 else 0 136 | neg_orientation = neg_sum / neg_num if neg_num != 0 else 0 137 | print('%lf' % (pos_orientation if pos_orientation > -neg_orientation else neg_orientation)) 138 | print('倾向性判断结束!') 139 | -------------------------------------------------------------------------------- /stopwords.txt: -------------------------------------------------------------------------------- 1 | $ 2 | 0 3 | 1 4 | 2 5 | 3 6 | 4 7 | 5 8 | 6 9 | 7 10 | 8 11 | 9 12 | ? 13 | _ 14 | “ 15 | ” 16 | 、 17 | 。 18 | 《 19 | 》 20 | ! 21 | , 22 | : 23 | ; 24 | ? 25 | , 26 | : 27 | ; 28 | " 29 | - 30 | { 31 | } 32 | ( 33 | ) 34 | [ 35 | ] 36 | ( 37 | ) 38 | 【 39 | 】 40 | 一 41 | 一些 42 | 一何 43 | 一切 44 | 一则 45 | 一方面 46 | 一旦 47 | 一来 48 | 一样 49 | 一般 50 | 一转眼 51 | 万一 52 | 上 53 | 上下 54 | 下 55 | 不 56 | 不仅 57 | 不但 58 | 不光 59 | 不单 60 | 不只 61 | 不外乎 62 | 不如 63 | 不妨 64 | 不尽 65 | 不尽然 66 | 不得 67 | 不怕 68 | 不惟 69 | 不成 70 | 不拘 71 | 不料 72 | 不是 73 | 不比 74 | 不然 75 | 不特 76 | 不独 77 | 不管 78 | 不至于 79 | 不若 80 | 不论 81 | 不过 82 | 不问 83 | 与 84 | 与其 85 | 与其说 86 | 与否 87 | 与此同时 88 | 且 89 | 且不说 90 | 且说 91 | 两者 92 | 个 93 | 个别 94 | 临 95 | 为 96 | 为了 97 | 为什么 98 | 为何 99 | 为止 100 | 为此 101 | 为着 102 | 乃 103 | 乃至 104 | 乃至于 105 | 么 106 | 之 107 | 之一 108 | 之所以 109 | 之类 110 | 乌乎 111 | 乎 112 | 乘 113 | 也 114 | 也好 115 | 也罢 116 | 了 117 | 二来 118 | 于 119 | 于是 120 | 于是乎 121 | 云云 122 | 云尔 123 | 些 124 | 亦 125 | 人 126 | 人们 127 | 人家 128 | 什么 129 | 什么样 130 | 今 131 | 介于 132 | 仍 133 | 仍旧 134 | 从 135 | 从此 136 | 从而 137 | 他 138 | 他人 139 | 他们 140 | 以 141 | 以上 142 | 以为 143 | 以便 144 | 以免 145 | 以及 146 | 以故 147 | 以期 148 | 以来 149 | 以至 150 | 以至于 151 | 以致 152 | 们 153 | 任 154 | 任何 155 | 任凭 156 | 似的 157 | 但 158 | 但凡 159 | 但是 160 | 何 161 | 何以 162 | 何况 163 | 何处 164 | 何时 165 | 余外 166 | 作为 167 | 你 168 | 你们 169 | 使 170 | 使得 171 | 例如 172 | 依 173 | 依据 174 | 依照 175 | 便于 176 | 俺 177 | 俺们 178 | 倘 179 | 倘使 180 | 倘或 181 | 倘然 182 | 倘若 183 | 借 184 | 假使 185 | 假如 186 | 假若 187 | 傥然 188 | 像 189 | 儿 190 | 先不先 191 | 光是 192 | 全体 193 | 全部 194 | 兮 195 | 关于 196 | 其 197 | 其一 198 | 其中 199 | 其二 200 | 其他 201 | 其余 202 | 其它 203 | 其次 204 | 具体地说 205 | 具体说来 206 | 兼之 207 | 内 208 | 再 209 | 再其次 210 | 再则 211 | 再有 212 | 再者 213 | 再者说 214 | 再说 215 | 冒 216 | 冲 217 | 况且 218 | 几 219 | 几时 220 | 凡 221 | 凡是 222 | 凭 223 | 凭借 224 | 出于 225 | 出来 226 | 分别 227 | 则 228 | 则甚 229 | 别 230 | 别人 231 | 别处 232 | 别是 233 | 别的 234 | 别管 235 | 别说 236 | 到 237 | 前后 238 | 前此 239 | 前者 240 | 加之 241 | 加以 242 | 即 243 | 即令 244 | 即使 245 | 即便 246 | 即如 247 | 即或 248 | 即若 249 | 却 250 | 去 251 | 又 252 | 又及 253 | 及 254 | 及其 255 | 及至 256 | 反之 257 | 反而 258 | 反过来 259 | 反过来说 260 | 受到 261 | 另 262 | 另一方面 263 | 另外 264 | 另悉 265 | 只 266 | 只当 267 | 只怕 268 | 只是 269 | 只有 270 | 只消 271 | 只要 272 | 只限 273 | 叫 274 | 叮咚 275 | 可 276 | 可以 277 | 可是 278 | 可见 279 | 各 280 | 各个 281 | 各位 282 | 各种 283 | 各自 284 | 同 285 | 同时 286 | 后 287 | 后者 288 | 向 289 | 向使 290 | 向着 291 | 吓 292 | 吗 293 | 否则 294 | 吧 295 | 吧哒 296 | 吱 297 | 呀 298 | 呃 299 | 呕 300 | 呗 301 | 呜 302 | 呜呼 303 | 呢 304 | 呵 305 | 呵呵 306 | 呸 307 | 呼哧 308 | 咋 309 | 和 310 | 咚 311 | 咦 312 | 咧 313 | 咱 314 | 咱们 315 | 咳 316 | 哇 317 | 哈 318 | 哈哈 319 | 哉 320 | 哎 321 | 哎呀 322 | 哎哟 323 | 哗 324 | 哟 325 | 哦 326 | 哩 327 | 哪 328 | 哪个 329 | 哪些 330 | 哪儿 331 | 哪天 332 | 哪年 333 | 哪怕 334 | 哪样 335 | 哪边 336 | 哪里 337 | 哼 338 | 哼唷 339 | 唉 340 | 唯有 341 | 啊 342 | 啐 343 | 啥 344 | 啦 345 | 啪达 346 | 啷当 347 | 喂 348 | 喏 349 | 喔唷 350 | 喽 351 | 嗡 352 | 嗡嗡 353 | 嗬 354 | 嗯 355 | 嗳 356 | 嘎 357 | 嘎登 358 | 嘘 359 | 嘛 360 | 嘻 361 | 嘿 362 | 嘿嘿 363 | 因 364 | 因为 365 | 因了 366 | 因此 367 | 因着 368 | 因而 369 | 固然 370 | 在 371 | 在下 372 | 在于 373 | 地 374 | 基于 375 | 处在 376 | 多 377 | 多么 378 | 多少 379 | 大 380 | 大家 381 | 她 382 | 她们 383 | 好 384 | 如 385 | 如上 386 | 如上所述 387 | 如下 388 | 如何 389 | 如其 390 | 如同 391 | 如是 392 | 如果 393 | 如此 394 | 如若 395 | 始而 396 | 孰料 397 | 孰知 398 | 宁 399 | 宁可 400 | 宁愿 401 | 宁肯 402 | 它 403 | 它们 404 | 对 405 | 对于 406 | 对待 407 | 对方 408 | 对比 409 | 将 410 | 小 411 | 尔 412 | 尔后 413 | 尔尔 414 | 尚且 415 | 就 416 | 就是 417 | 就是了 418 | 就是说 419 | 就算 420 | 就要 421 | 尽 422 | 尽管 423 | 尽管如此 424 | 岂但 425 | 己 426 | 已 427 | 已矣 428 | 巴 429 | 巴巴 430 | 并 431 | 并且 432 | 并非 433 | 庶乎 434 | 庶几 435 | 开外 436 | 开始 437 | 归 438 | 归齐 439 | 当 440 | 当地 441 | 当然 442 | 当着 443 | 彼 444 | 彼时 445 | 彼此 446 | 往 447 | 待 448 | 很 449 | 得 450 | 得了 451 | 怎 452 | 怎么 453 | 怎么办 454 | 怎么样 455 | 怎奈 456 | 怎样 457 | 总之 458 | 总的来看 459 | 总的来说 460 | 总的说来 461 | 总而言之 462 | 恰恰相反 463 | 您 464 | 惟其 465 | 慢说 466 | 我 467 | 我们 468 | 或 469 | 或则 470 | 或是 471 | 或曰 472 | 或者 473 | 截至 474 | 所 475 | 所以 476 | 所在 477 | 所幸 478 | 所有 479 | 才 480 | 才能 481 | 打 482 | 打从 483 | 把 484 | 抑或 485 | 拿 486 | 按 487 | 按照 488 | 换句话说 489 | 换言之 490 | 据 491 | 据此 492 | 接着 493 | 故 494 | 故此 495 | 故而 496 | 旁人 497 | 无 498 | 无宁 499 | 无论 500 | 既 501 | 既往 502 | 既是 503 | 既然 504 | 时候 505 | 是 506 | 是以 507 | 是的 508 | 曾 509 | 替 510 | 替代 511 | 最 512 | 有 513 | 有些 514 | 有关 515 | 有及 516 | 有时 517 | 有的 518 | 望 519 | 朝 520 | 朝着 521 | 本 522 | 本人 523 | 本地 524 | 本着 525 | 本身 526 | 来 527 | 来着 528 | 来自 529 | 来说 530 | 极了 531 | 果然 532 | 果真 533 | 某 534 | 某个 535 | 某些 536 | 某某 537 | 根据 538 | 欤 539 | 正值 540 | 正如 541 | 正巧 542 | 正是 543 | 此 544 | 此地 545 | 此处 546 | 此外 547 | 此时 548 | 此次 549 | 此间 550 | 毋宁 551 | 每 552 | 每当 553 | 比 554 | 比及 555 | 比如 556 | 比方 557 | 没奈何 558 | 沿 559 | 沿着 560 | 漫说 561 | 焉 562 | 然则 563 | 然后 564 | 然而 565 | 照 566 | 照着 567 | 犹且 568 | 犹自 569 | 甚且 570 | 甚么 571 | 甚或 572 | 甚而 573 | 甚至 574 | 甚至于 575 | 用 576 | 用来 577 | 由 578 | 由于 579 | 由是 580 | 由此 581 | 由此可见 582 | 的 583 | 的确 584 | 的话 585 | 直到 586 | 相对而言 587 | 省得 588 | 看 589 | 眨眼 590 | 着 591 | 着呢 592 | 矣 593 | 矣乎 594 | 矣哉 595 | 离 596 | 竟而 597 | 第 598 | 等 599 | 等到 600 | 等等 601 | 简言之 602 | 管 603 | 类如 604 | 紧接着 605 | 纵 606 | 纵令 607 | 纵使 608 | 纵然 609 | 经 610 | 经过 611 | 结果 612 | 给 613 | 继之 614 | 继后 615 | 继而 616 | 综上所述 617 | 罢了 618 | 者 619 | 而 620 | 而且 621 | 而况 622 | 而后 623 | 而外 624 | 而已 625 | 而是 626 | 而言 627 | 能 628 | 能否 629 | 腾 630 | 自 631 | 自个儿 632 | 自从 633 | 自各儿 634 | 自后 635 | 自家 636 | 自己 637 | 自打 638 | 自身 639 | 至 640 | 至于 641 | 至今 642 | 至若 643 | 致 644 | 般的 645 | 若 646 | 若夫 647 | 若是 648 | 若果 649 | 若非 650 | 莫不然 651 | 莫如 652 | 莫若 653 | 虽 654 | 虽则 655 | 虽然 656 | 虽说 657 | 被 658 | 要 659 | 要不 660 | 要不是 661 | 要不然 662 | 要么 663 | 要是 664 | 譬喻 665 | 譬如 666 | 让 667 | 许多 668 | 论 669 | 设使 670 | 设或 671 | 设若 672 | 诚如 673 | 诚然 674 | 该 675 | 说来 676 | 诸 677 | 诸位 678 | 诸如 679 | 谁 680 | 谁人 681 | 谁料 682 | 谁知 683 | 贼死 684 | 赖以 685 | 赶 686 | 起 687 | 起见 688 | 趁 689 | 趁着 690 | 越是 691 | 距 692 | 跟 693 | 较 694 | 较之 695 | 边 696 | 过 697 | 还 698 | 还是 699 | 还有 700 | 还要 701 | 这 702 | 这一来 703 | 这个 704 | 这么 705 | 这么些 706 | 这么样 707 | 这么点儿 708 | 这些 709 | 这会儿 710 | 这儿 711 | 这就是说 712 | 这时 713 | 这样 714 | 这次 715 | 这般 716 | 这边 717 | 这里 718 | 进而 719 | 连 720 | 连同 721 | 逐步 722 | 通过 723 | 遵循 724 | 遵照 725 | 那 726 | 那个 727 | 那么 728 | 那么些 729 | 那么样 730 | 那些 731 | 那会儿 732 | 那儿 733 | 那时 734 | 那样 735 | 那般 736 | 那边 737 | 那里 738 | 都 739 | 鄙人 740 | 鉴于 741 | 针对 742 | 阿 743 | 除 744 | 除了 745 | 除外 746 | 除开 747 | 除此之外 748 | 除非 749 | 随 750 | 随后 751 | 随时 752 | 随着 753 | 难道说 754 | 非但 755 | 非徒 756 | 非特 757 | 非独 758 | 靠 759 | 顺 760 | 顺着 761 | 首先 -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | 之前做过一些自然语言处理的工作,主要是根据一些企业在互联网上的相关新闻进行分析,对其倾向性进行判断,最终目的是辅助国内某单位更好地对其管辖的企业进行监管工作。现在总结整理一下。这篇文章主要对**词向量训练阶段**进行阐述。(所有代码见我的Github) 2 | 3 | --- 4 | 5 | ## 数据获取 6 | 使用的语料库是wiki百科的中文语料库,下载地址:https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2。另外,提供百度网盘下载链接:https://pan.baidu.com/s/1eLkybiYOE_aVxsN0pALATg,提取码为:hmtn。 7 | 8 | 下载之后如下图(PyCharm截图),大小为1.16GB。 9 | 10 | ![zhwiki-latest-pages-articles.xml.bz2](https://upload-images.jianshu.io/upload_images/11579422-186c2888dab99f7c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 11 | 12 | --- 13 | 14 | ## 将xml格式数据转为txt 15 | 因为原始文件是xml格式,并且是压缩文件,所以做了一步数据解压并进行格式转换的工作。 16 | 具体使用了gensim库中的维基百科处理类WikiCorpus,该类中的get_texts方法原文件中的文章转化为一个数组,其中每一个元素对应着原文件中的一篇文章。然后通过for循环便可以将其中的每一篇文章读出,然后进行保存。 17 | 18 | ![xml2txt.py](https://upload-images.jianshu.io/upload_images/11579422-2eea76b4928478ae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 19 | 20 | ``` 21 | # coding; utf-8 22 | """ 23 | 这个代码是将从网络上下载的xml格式的wiki百科训练语料转为txt格式 24 | wiki百科训练语料 25 | 链接:https://pan.baidu.com/s/1eLkybiYOE_aVxsN0pALATg 26 | 密码:hmtn 27 | """ 28 | 29 | from gensim.corpora import WikiCorpus 30 | 31 | if __name__ == '__main__': 32 | 33 | print('主程序开始...') 34 | 35 | input_file_name = 'zhwiki-latest-pages-articles.xml.bz2' 36 | output_file_name = 'wiki.cn.txt' 37 | print('开始读入wiki数据...') 38 | input_file = WikiCorpus(input_file_name, lemmatize=False, dictionary={}) 39 | print('wiki数据读入完成!') 40 | output_file = open(output_file_name, 'w', encoding="utf-8") 41 | 42 | print('处理程序开始...') 43 | count = 0 44 | for text in input_file.get_texts(): 45 | output_file.write(' '.join(text) + '\n') 46 | count = count + 1 47 | if count % 10000 == 0: 48 | print('目前已处理%d条数据' % count) 49 | print('处理程序结束!') 50 | 51 | output_file.close() 52 | print('主程序结束!') 53 | ``` 54 | 55 | 结果文件截图: 56 | 57 | ![wiki.cn.txt](https://upload-images.jianshu.io/upload_images/11579422-a67e13e691a2a5c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 58 | 59 | 60 | ![xml2txt-result](https://upload-images.jianshu.io/upload_images/11579422-8b06a73f3adf188e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 61 | 62 | --- 63 | 64 | ## 繁体转为简体 65 | 为了方便后期处理,接下来对上面的结果进行简体化处理,将所有的繁体全部转化为简体。在这里,使用了另外一个库zhconv。对上面结果的每一行调用convert函数即可。 66 | 67 | ![tradition2simple.py](https://upload-images.jianshu.io/upload_images/11579422-a2affe683a8998df.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 68 | 69 | ``` 70 | # coding:utf-8 71 | import zhconv 72 | 73 | print('主程序执行开始...') 74 | 75 | input_file_name = 'wiki.cn.txt' 76 | output_file_name = 'wiki.cn.simple.txt' 77 | input_file = open(input_file_name, 'r', encoding='utf-8') 78 | output_file = open(output_file_name, 'w', encoding='utf-8') 79 | 80 | print('开始读入繁体文件...') 81 | lines = input_file.readlines() 82 | print('读入繁体文件结束!') 83 | 84 | print('转换程序执行开始...') 85 | count = 1 86 | for line in lines: 87 | output_file.write(zhconv.convert(line, 'zh-hans')) 88 | count += 1 89 | if count % 10000 == 0: 90 | print('目前已转换%d条数据' % count) 91 | print('转换程序执行结束!') 92 | 93 | print('主程序执行结束!') 94 | ``` 95 | 96 | 结果截图: 97 | 98 | ![wiki.cn.simple.txt](https://upload-images.jianshu.io/upload_images/11579422-95fcddafa5a4be48.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 99 | 100 | ![tradition2simple-result](https://upload-images.jianshu.io/upload_images/11579422-15e69c3d38423539.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 101 | 102 | --- 103 | 104 | ## 分词 105 | 对于中文来说,分词是必须要经过的一步处理,下面就需要进行分词操作。在这里使用了大名鼎鼎的jieba库。调用其中的cut方法即可。 106 | 107 | ![separate.py](https://upload-images.jianshu.io/upload_images/11579422-e16d06c115bb039e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 108 | 109 | ``` 110 | # coding:utf-8 111 | import jieba 112 | 113 | print('主程序执行开始...') 114 | 115 | input_file_name = 'wiki.cn.simple.txt' 116 | output_file_name = 'wiki.cn.simple.separate.txt' 117 | input_file = open(input_file_name, 'r', encoding='utf-8') 118 | output_file = open(output_file_name, 'w', encoding='utf-8') 119 | 120 | print('开始读入数据文件...') 121 | lines = input_file.readlines() 122 | print('读入数据文件结束!') 123 | 124 | print('分词程序执行开始...') 125 | count = 1 126 | for line in lines: 127 | # jieba分词的结果是一个list,需要拼接,但是jieba把空格回车都当成一个字符处理 128 | output_file.write(' '.join(jieba.cut(line.split('\n')[0].replace(' ', ''))) + '\n') 129 | count += 1 130 | if count % 10000 == 0: 131 | print('目前已分词%d条数据' % count) 132 | print('分词程序执行结束!') 133 | 134 | print('主程序执行结束!') 135 | ``` 136 | 137 | 结果截图: 138 | 139 | ![wiki.cn.simple.seprate.txt](https://upload-images.jianshu.io/upload_images/11579422-ff07ff6f7ba8775b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 140 | 141 | ![separate-result](https://upload-images.jianshu.io/upload_images/11579422-cfd18e1ef400d9d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 142 | 143 | --- 144 | 145 | ## 去除非中文词 146 | 可以看到,经过上面的处理之后,现在的结果已经差不多了,但是还存在着一些非中文词,所以下一步便将这些词去除。具体做法是通过正则表达式判断每一个词是不是符合汉字开头、汉字结尾、中间全是汉字,即“**^[\u4e00-\u9fa5]+$**”。 147 | 148 | ![remove.py](https://upload-images.jianshu.io/upload_images/11579422-04fd2508db80f17d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 149 | 150 | ``` 151 | # coding:utf-8 152 | import re 153 | 154 | print('主程序执行开始...') 155 | 156 | input_file_name = 'wiki.cn.simple.separate.txt' 157 | output_file_name = 'wiki.txt' 158 | input_file = open(input_file_name, 'r', encoding='utf-8') 159 | output_file = open(output_file_name, 'w', encoding='utf-8') 160 | 161 | print('开始读入数据文件...') 162 | lines = input_file.readlines() 163 | print('读入数据文件结束!') 164 | 165 | print('分词程序执行开始...') 166 | count = 1 167 | cn_reg = '^[\u4e00-\u9fa5]+$' 168 | 169 | for line in lines: 170 | line_list = line.split('\n')[0].split(' ') 171 | line_list_new = [] 172 | for word in line_list: 173 | if re.search(cn_reg, word): 174 | line_list_new.append(word) 175 | print(line_list_new) 176 | output_file.write(' '.join(line_list_new) + '\n') 177 | count += 1 178 | if count % 10000 == 0: 179 | print('目前已分词%d条数据' % count) 180 | print('分词程序执行结束!') 181 | 182 | print('主程序执行结束!') 183 | ``` 184 | 185 | 结果截图: 186 | 187 | ![wiki.txt](https://upload-images.jianshu.io/upload_images/11579422-989b5cf02ee59995.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 188 | 189 | ![remove-result](https://upload-images.jianshu.io/upload_images/11579422-3e6ebef3f9b15d46.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 190 | 191 | --- 192 | 193 | ## 词向量训练 194 | 上面的工作主要是对wiki语料库进行数据预处理,接下来才真正的词向量训练。 195 | 196 | ![word2vec.py](https://upload-images.jianshu.io/upload_images/11579422-976b344b74ec1836.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 197 | 198 | ``` 199 | # coding:utf-8 200 | import multiprocessing 201 | from gensim.models import Word2Vec 202 | from gensim.models.word2vec import LineSentence 203 | 204 | if __name__ == "__main__": 205 | print('主程序开始执行...') 206 | 207 | input_file_name = 'wiki.txt' 208 | model_file_name = 'wiki.model' 209 | 210 | print('转换过程开始...') 211 | model = Word2Vec(LineSentence(input_file_name), 212 | size=400, # 词向量长度为400 213 | window=5, 214 | min_count=5, 215 | workers=multiprocessing.cpu_count()) 216 | print('转换过程结束!') 217 | 218 | print('开始保存模型...') 219 | model.save(model_file_name) 220 | print('模型保存结束!') 221 | 222 | print('主程序执行结束!') 223 | ``` 224 | 225 | 也是使用了gensim库,通过其中的Word2Vec类进行了模型训练,并将最终的词向量保存起来。 226 | 227 | ![wiki.model](https://upload-images.jianshu.io/upload_images/11579422-fd2532d8da28f433.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 228 | 229 | --- 230 | 231 | > **参考文献:** 232 | > [1]. wiki中文语料库, https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2. 233 | > [2]. 使用 word2vec 训练wiki中英文语料库, https://www.jianshu.com/p/05800a28c5e4. 234 | > [3]. 中英文维基百科语料上的Word2Vec实验, http://www.52nlp.cn/%E4%B8%AD%E8%8B%B1%E6%96%87%E7%BB%B4%E5%9F%BA%E7%99%BE%E7%A7%91%E8%AF%AD%E6%96%99%E4%B8%8A%E7%9A%84word2vec%E5%AE%9E%E9%AA%8C. 235 | 236 | --- 237 | 238 | > 作者原创,如需转载及其他问题请邮箱联系:lwqiang_chn@163.com。 239 | > 个人网站:https://www.myqiang.top。 240 | > GitHub:https://github.com/liuwenqiang1202。 --------------------------------------------------------------------------------