文章目录
- 一.Python代码
- 二.代码说明
- 1.数据准备
- 2.数据集划分
- 3.数据加载和清洗
- 4.定义词汇表
- 5.保存词汇表
- 三.结果输出
一.Python代码
#!/usr/bin/env python3
# encoding: utf-8
'''
@file: keras_data_process.py
@time: 2020/6/29 0029 21:02
@author: Jack
@contact: jack18588951684@163.com
'''
import string
import re
from os import listdir
from collections import Counter
from nltk.corpus import stopwords
## 加载文档至内存
def load_doc(filename):
file = open(filename, 'r')
text = file.read()
file.close()
return text
## 将文档转成clean tokens
def clean_doc(doc):
## 文档按空格分词
tokens = doc.split()
## 准备标点符号过滤正则
re_punc = re.compile('[%s]' % re.escape(string.punctuation))
## 移除每个单词的标点
tokens = [re_punc.sub('', w) for w in tokens]
## 移除全部非字母组成的token
tokens = [word for word in tokens if word.isalpha()]
## 移除停用词
stop_words = set(stopwords.words('english'))
tokens = [w for w in tokens if not w in stop_words]
## 移除长度小于等于1的token
tokens = [w for w in tokens if len(w) > 1]
return tokens
## 用新增文档更新词汇表
def add_doc_to_vocab(filename, vocab):
doc = load_doc(filename)
tokens = clean_doc(doc)
vocab.update(tokens)
## 加载文件夹下的所有文档
def process_docs(directory, vocab):
## 遍历文件夹下的所有文档
for filename in listdir(directory):
## 跳过测试集
if filename.startswith('cv9'):
continue
path = directory + '/' + filename
add_doc_to_vocab(path, vocab)
## 保存列表至文件
def save_list(lines, filename):
data = '\n'.join(lines)
file = open(filename, 'w')
file.write(data)
file.close()
vocab = Counter()
process_docs('txt_sentoken/pos', vocab)
process_docs('txt_sentoken/neg', vocab)
print(len(vocab))
print(vocab.most_common(50))
min_occurence = 2
tokens = [k for k, c in vocab.items() if c >= min_occurence]
print(len(tokens))
save_list(tokens, 'vocab.txt')
二.代码说明
1.数据准备
在这部分我们使用的是电影评论数据集Polarity Dataset:
下载链接:https://share.weiyun.com/WAEtwDKe
解压缩文件后,将得到一个名为txt_sentoken的目录,其中包含两个子目录neg和pos,分别存放负面和正面评论。neg和pos目录中的每个文件存储一个评论,命名规则为cv000到cv999。
2.数据集划分
使用最后100个正面评论和最后100个负面评论作为测试集(200条评论),其余1800条评论作为训练数据集,即数据集的90%数据用于训练,10%数据用于测试。这里可以通过评论的文件名来实现数据集划分,其中评论为000~899的评论用于训练,而评论900以上的评论用于测试模型。
3.数据加载和清洗
- 以空格为分隔符分词;
- 从单词中删除所有标点符号;
- 删除所有不完全由字母字符组成的单词;
- 删除所有已知停用词;
- 删除长<=1个字符的所有单词。
上述这5个数据清洗步骤都封装在clean_doc()函数中,该函数将从文件加载的原始文本作为参数,并返回清理后的分词列表。
4.定义词汇表
通过使用Counter类来构建一个词汇表,它是一个单词及其计数的字典映射,可以让我们轻松更新和查询。代码中,每个文档都可以添加到Counter(通过add_doc_to_vocab()函数)中,通过process_docs()函数来分别处理负面评论和正正面评论中的评论文件。代码最后通过设定阈值,将词汇表中出现频次较低的词汇过滤掉,在这里阈值为2。
5.保存词汇表
最后通过save_list()函数将最终的词汇表保存到一个名为vocab.txt的新文件中,便于在之后的操作中可以随时加载并使用。
三.结果输出
44287
[('film', 7983), ('one', 4946), ('movie', 4826), ('like', 3201), ('even', 2262), ('good', 2080), ('time', 2041), ('story', 1907), ('films', 1873), ('would', 1844), ('much', 1824), ('also', 1757), ('characters', 1735), ('get', 1724), ('character', 1703), ('two', 1643), ('first', 1588), ('see', 1557), ('way', 1515), ('well', 1511), ('make', 1418), ('really', 1407), ('little', 1351), ('life', 1334), ('plot', 1288), ('people', 1269), ('could', 1248), ('bad', 1248), ('scene', 1241), ('movies', 1238), ('never', 1201), ('best', 1179), ('new', 1140), ('scenes', 1135), ('man', 1131), ('many', 1130), ('doesnt', 1118), ('know', 1092), ('dont', 1086), ('hes', 1024), ('great', 1014), ('another', 992), ('action', 985), ('love', 977), ('us', 967), ('go', 952), ('director', 948), ('end', 946), ('something', 945), ('still', 936)]
25777