第3章:中文本文向量化——代码详解
创始人
2024-02-13 22:15:12
0

1.单条语句的向量化

根据不同的向量化,对“中华女子学院:本科层次仅1专业招男生”这句话进行向量化

1.1 One-hot方法

# one-hot代码
import jieba
import os
import numpy as npstopwords = open('./data/哈工大停用词表.txt').read().split("\n")words = '中华女子学院:本科层次仅1专业招男生'
word = jieba.lcut(words)
# print(word)
word = [w for w in word if w not in stopwords]  #去除停用词one_hots = {}
lenth = len(word)
for index,word in enumerate(word):one_hot = [0]*lenth  #构造一个全为0的列表或者是一个一维矩阵one_hot[index] = 1one_hots[word] =one_hot 
print(one_hots)

1.2 TF和TF-IDF向量法

方法1:先使用CountVectorizer完成文本的TF表示,之后再使用TfidfTransformer完成文本的TF-IDF表示

# TF-IDF方法制作一句话的向量
import jieba
import os
import numpy as npstopwords = open('./data/哈工大停用词表.txt').read().split("\n")
words = '中华女子学院:本科层次仅1专业招男生'
word = jieba.lcut(words)
word = [w for w in word if w not in stopwords]  #去除停用词dic_data =[ " ".join(word)]
print(dic_data)
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
# 矩阵中包含一个元素a[i][j],它表示j词在i类文本下的词频。它通过fit_transform函数计算各个词语出现的次数
count = vectorizer.fit_transform(dic_data)
# print(count)
from sklearn.feature_extraction.text import TfidfTransformer
transformer = TfidfTransformer() #类调用
tfidf = transformer.fit_transform(count) #将词频统计成TF-IDF值
#查看数据结构 tfidf[i][j]表示i类文本中的tf-idf权重
print(tfidf.toarray())

方法2:直接使用TfidfVectorizer完成文本的向量化

import jieba
import os
import numpy as npstopwords = open('./data/哈工大停用词表.txt').read().split("\n")
words = '中华女子学院:本科层次仅1专业招男生'
word = jieba.lcut(words)
word = [w for w in word if w not in stopwords]  #去除停用词dic_data =[ " ".join(word)]
print(dic_data)from sklearn.feature_extraction.text import TfidfVectorizer
counttdidf = TfidfVectorizer()
count_tfidf = counttdidf.fit_transform(dic_data)
# print(count_tfidf)
train_weight = count_tfidf.toarray() #篇章(句)向量化 1800*dic
print(count_tfidf.toarray())

1.3 Word2Vec向量法

  • 环境安装:pip install gensim
  • 使用gensim进行Word2Vec进行向量化的数据格式为:[[‘中华’, ‘女子’, ‘学院’, ‘本科’, ‘层次’, ‘仅’, ‘1’, ‘专业’, ‘招’, ‘男生’], [‘两天’, ‘价’, ‘网站’, ‘背后’, ‘重重’, ‘迷雾’, ‘做个’, ‘网站’, ‘究竟’, ‘钱’], [‘东’, ‘5’, ‘环’, ‘海棠’, ‘公社’, ‘230’, ‘290’, ‘平’, ‘2’, ‘居’, ‘准现房’, ‘98’, ‘折’, ‘优惠’]]
from gensim.models import Word2Vecstopwords = open('./data/哈工大停用词表.txt').read().split("\n")
words = '中华女子学院:本科层次仅1专业招男生'
word = jieba.lcut(words)
# print(word)
word = [[w for w in word if w not in stopwords]]  #去除停用词
# print(word)
#训练模型
model = Word2Vec(word, vector_size=5,min_count=1,epochs=7) #vector_size:向量表示的数量;min_count:最小统计的词;epochs:迭代次数,使得w1和w2更新的次数
#保存模型
model.save('model.bin')
# load model
new_model = Word2Vec.load('model.bin')
print(new_model.wv['学院'])
print("和学院相关性最高的前5个词:",model.wv.most_similar('学院',topn=5))

1.4 BERT向量法

  • 环境配置
    1.python -V
    2.conda create -n pytorch python==3.7
    3.conda activate pytorch
    4.打开pytorch官网:https://pytorch.org/
    5.验证:
    import torch
    torch.version
    torch.cuda.is_available()
    6.pip install transformers
使用transformers进行向量化的数据格式为:['北京欢迎你','智能科学与技术']
"""
from transformers import BertTokenizer,BertModel
# 初始化分词器
tokenizer = BertTokenizer.from_pretrained('./bert_base_chinese')
#加载预训练模型
model = BertModel.from_pretrained('./bert_base_chinese')batch_token1 = tokenizer(['北京欢迎你','智能科学与技术'],padding=True,return_tensors='pt') #padding=True:根据最长的句子进行填充;return_tensors='pt':表示使用pytorch版本
print(batch_token1)encoded = model(batch_token1['input_ids']) #有id转向量化过程
print(encoded) #last_hidden_state:词向量;pooler_output:分类的输出结果
encoded_text = encoded[0] #仅仅获得词向量
print('词向量:',encoded_text)
print("输出词向量的形状:",encoded_text.shape) #torch.Size([2, 9, 768]) 2个句子*9个词id*768

2.多条语句的向量化

在这里插入图片描述
语料说明:将3个文本保存在data.txt文档中,同时将data.txt文档存放在“练习data”这个文件夹当中。同时我们的代码是和该文件夹在同一目录中。

2.1 One-hot方法

# one-hot代码import jieba
import os
import numpy as npstopwords = open('./data/哈工大停用词表.txt').read().split("\n")dic = []
with open('./练习data/data.txt','r',encoding='utf-8') as f:lines = f.read().split('\n')for line in lines:words = jieba.lcut(line)# for w in words:#     if w not in stopwords:#         dic.append(w)dic.append([w for w in words if w not in stopwords])
# print(dic)        
def mak_dic(seg_dic):dics = []for lines in seg_dic:for words in lines:if words not in dics:dics.append(words)print(len(dics))one_hots = {}lenth = len(dics)for index,word in enumerate(dics):print(word)one_hot = [0]*lenth  #构造一个全为0的列表或者是一个一维矩阵one_hot[index] = 1print(one_hot)one_hots[word] =one_hotreturn one_hots
result = mak_dic(dic)

2.2 TF-IDF方法

使用skearn进行TF-IDF进行向量化的数据格式为:['北京 欢迎 北京','你们 喜欢 北京','北京 天安门']
# TF-IDF方法
stopwords = open('./data/哈工大停用词表.txt').read().split("\n")dic = []
dic_data = []
with open('./练习data/data.txt','r',encoding='utf-8') as f:lines = f.read().split('\n')for line in lines:words = jieba.lcut(line)# for w in words:#     if w not in stopwords:#         dic.append(w)dic.append([w for w in words if w not in stopwords])dic_datas = " ".join(words)dic_data.append(dic_datas)from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
# 矩阵中包含一个元素a[i][j],它表示j词在i类文本下的词频。它通过fit_transform函数计算各个词语出现的次数
count = vectorizer.fit_transform(dic_data)
# print(count)
from sklearn.feature_extraction.text import TfidfTransformer
transformer = TfidfTransformer() #类调用
tfidf = transformer.fit_transform(count) #将词频统计成TF-IDF值
#查看数据结构 tfidf[i][j]表示i类文本中的tf-idf权重
print(tfidf.toarray())

2.3 Word2Vec向量法

"""
# 环境配置:pip install gensim
使用gensim进行Word2Vec进行向量化的数据格式为:
[['中华', '女子', '学院', '本科', '层次', '仅', '1', '专业', '招', '男生'], ['两天', '价', '网站', '背后', '重重', '迷雾', '做个', '网站', '究竟', '钱'], ['东', '5', '环', '海棠', '公社', '230', '290', '平', '2', '居', '准现房', '98', '折', '优惠']]
"""
from gensim.models import Word2Vec
from sklearn.decomposition import PCA
import matplotlib.pyplot as pltstopwords = open('./data/哈工大停用词表.txt').read().split("\n")dic = []
dic_data = []
with open('./练习data/data.txt','r',encoding='utf-8') as f:lines = f.read().split('\n')for line in lines:words = jieba.lcut(line)# for w in words:#     if w not in stopwords:#         dic.append(w)dic.append([w for w in words if w not in stopwords])dic_datas = " ".join(words)dic_data.append(dic_datas)
#x训练模型
model = Word2Vec(dic, vector_size=5,min_count=1,epochs=7)
#保存模型
model.save('./model.bin')
# load model
new_model = Word2Vec.load('model.bin')
print(model.wv['学院'])
print("和学院相关性最高的前5个词:",model.wv.most_similar('学院',topn=5))## 判断词之间的相似度
print(model.wv.similarity('本科','本科'))  #1.0
## 判断词之间的相似度
print(model.wv.similarity('男生','女子'))  #-0.15#PCA降维为2维空间
rawWordVec =[]
word2ind = {}
for i,w in enumerate(model.wv.index_to_key): #序号、词语word2ind[w] = irawWordVec.append(model.wv[w])
rawWordVec = np.array(rawWordVec)
X_reduced  = PCA(n_components=2).fit_transform(rawWordVec)print(X_reduced)#绘制星空表
# 绘制星空图
# 绘制所有单词向量的二维空间投影
fig = plt.figure(figsize = (5, 5))
ax = fig.gca()
ax.set_facecolor('white')
ax.plot(X_reduced[:, 0], X_reduced[:, 1], '.', markersize = 5, alpha = 0.3, color = 'black') #黑色表示所有的词语# 绘制几个特殊单词的向量
words = ['学院','女子']# 设置中文字体 否则乱码 
import matplotlib
zhfont1 = matplotlib.font_manager.FontProperties(fname='./华文仿宋.ttf', size=16)
for w in words:if w in word2ind:ind = word2ind[w]xy = X_reduced[ind]plt.plot(xy[0], xy[1], '.', alpha =1, color = 'orange',markersize=10)plt.text(xy[0], xy[1], w, fontproperties = zhfont1, alpha = 1, color = 'red')

2.4 BERT向量法

"""
# 环境配置
1.python -V
2.conda create -n pytorch python==3.7
3.conda activate pytorch
4.打开pytorch官网:https://pytorch.org/
5.验证:import torchtorch.__version__torch.cuda.is_available()
6.pip install transformers使用transformers进行向量化的数据格式为:['北京欢迎你','智能科学与技术']"""dic = []
with open('./练习data/data.txt','r',encoding='utf-8') as f:lines = f.read().split('\n')for line in lines:dic.append(line)from transformers import BertTokenizer,BertModel
from transformers import logginglogging.set_verbosity_error()model = BertModel.from_pretrained('./bert_base_chinese')batch_token1 = tokenizer(dic,padding=True,return_tensors='pt') #padding=True:根据最长的句子进行填充;return_tensors='pt':表示使用pytorch版本encoded = model(batch_token1['input_ids']) #有id转向量化过程
# print(encoded) #last_hidden_state:词向量;pooler_output:分类的输出结果
encoded_text = encoded[0] #仅仅获得词向量
print(encoded_text.shape) #torch.Size([2, 9, 768]) 2个句子*9个词id*768

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...