tfidf_model = models.TfidfModel(corpus) corpus_tfidf=tfidf_model[corpus]怎么输出corpus_tfifd的内容
时间: 2024-05-04 16:18:40 浏览: 95
你可以使用以下代码来输出 `corpus_tfidf` 的内容:
```python
for doc in corpus_tfidf:
print(doc)
```
这将会输出每个文档的 TF-IDF 向量。每个文档的向量都是一个元组列表,其中每个元组包含一个词的 ID 和该词在该文档中的 TF-IDF 值。例如,如果你的语料库中有三个文档,那么输出可能如下所示:
```
[(0, 0.123), (1, 0.456), (2, 0.789), ...]
[(3, 0.234), (4, 0.567), (5, 0.890), ...]
[(6, 0.345), (7, 0.678), (8, 0.901), ...]
```
其中,每个数字代表一个词的 ID,每个小数代表该词在该文档中的 TF-IDF 值。
相关问题
Add comments to my code below and return,import pandas as pd import jieba from gensim import corpora, models, similarities # 读取Excel表格数据 df = pd.read_excel(r'D:\pycharm_project\excel_column_similar\cosmic核减率分析\excel_test_clean.xlsx') # 使用jieba进行分词,并去除停用词 stop_words = {'的', '了', '是', '我', '你', '他', '她', '(', ')'} texts = [] for text in df['子过程描述']: words = [word for word in jieba.cut(text) if word not in stop_words] texts.append(words) print(texts) # 构建词典和文档向量 dictionary = corpora.Dictionary(texts) corpus = [dictionary.doc2bow(text) for text in texts] print(corpus) # 计算TF-IDF特征向量 tfidf = models.TfidfModel(corpus) corpus_tfidf = tfidf[corpus] print(tfidf) print(corpus_tfidf) # 计算相似度 index = similarities.MatrixSimilarity(corpus_tfidf) similarity_matrix = index[corpus_tfidf] print(index) print(similarity_matrix.shape) # 标注相似度大于0.75的单元格 for i in range(similarity_matrix.shape[0]): for j in range(i + 1, similarity_matrix.shape[1]): if similarity_matrix[i, j] > 0.75: df.at[i, '子过程描述'] = str(df.at[i, '子过程描述']) + ' (' + str(round(similarity_matrix[i, j], 2)) + ')' df.at[j, '子过程描述'] = str(df.at[j, '子过程描述']) + ' (' + str(round(similarity_matrix[i, j], 2)) + ')' # 保存结果到Excel表格 df.to_excel(r'example_similarity.xlsx', index=False)
下面是代码的注释:
```python
import pandas as pd
import jieba
from gensim import corpora, models, similarities
# 读取Excel表格数据
df = pd.read_excel(r'D:\pycharm_project\excel_column_similar\cosmic核减率分析\excel_test_clean.xlsx')
# 使用jieba进行分词,并去除停用词
stop_words = {'的', '了', '是', '我', '你', '他', '她', '(', ')'}
texts = []
for text in df['子过程描述']:
words = [word for word in jieba.cut(text) if word not in stop_words]
texts.append(words)
print(texts)
# 构建词典和文档向量
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
print(corpus)
# 计算TF-IDF特征向量
tfidf = models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
print(tfidf)
print(corpus_tfidf)
# 计算相似度
index = similarities.MatrixSimilarity(corpus_tfidf)
similarity_matrix = index[corpus_tfidf]
print(index)
print(similarity_matrix.shape)
# 标注相似度大于0.75的单元格
for i in range(similarity_matrix.shape[0]):
for j in range(i + 1, similarity_matrix.shape[1]):
if similarity_matrix[i, j] > 0.75:
df.at[i, '子过程描述'] = str(df.at[i, '子过程描述']) + ' (' + str(round(similarity_matrix[i, j], 2)) + ')'
df.at[j, '子过程描述'] = str(df.at[j, '子过程描述']) + ' (' + str(round(similarity_matrix[i, j], 2)) + ')'
# 保存结果到Excel表格
df.to_excel(r'example_similarity.xlsx', index=False)
```
注释中解释了每个步骤的作用:
1. 读取Excel表格数据,保存到dataframe对象df中。
2. 使用jieba对子过程描述列进行分词,并去除停用词。
3. 使用gensim中的Dictionary类构建词典,并将分词后的文本转换为文档向量。
4. 使用TF-IDF模型计算词语的权重,并得到TF-IDF特征向量。
5. 使用MatrixSimilarity计算文本之间的相似度,并保存到相似度矩阵中。
6. 遍历相似度矩阵,将相似度大于0.75的单元格标注出来。
7. 将结果保存到新的Excel表格中。
import jieba from sklearn.feature_extraction.text import TfidfVectorizer import numpy as np import pandas as pd # 读取停用词文件 def read_stopwords(file_path): with open(file_path, 'r', encoding='gbk') as f: stopwords = [line.strip() for line in f] return set(stopwords) # 中文分词 def chinese_word_cut(text, stopwords): words = jieba.cut(text) result = [] for word in words: if word not in stopwords: result.append(word) return " ".join(result) # 读取CSV文件 weibo_data = pd.read_csv('E:\Python自然语言处理\data\weibo_Convid19.csv', sep='\t') df = weibo_data['text_raw'] # 获取停用词集合 stopwords = read_stopwords('E:\Python自然语言处理\data\stopword.txt') # 对每条微博进行分词和去停用词 corpus_list = df.apply(lambda x: chinese_word_cut(x, stopwords)) # 提取关键词 corpus = ' '.join(corpus_list) tfidf = TfidfVectorizer() tf_key = tfidf.fit_transform([corpus]) word = tfidf.get_feature_names() weight = tf_key.toarray()[0] w_sort = np.argsort(-weight) print('Top 20 keywords:') for i in range(20): print(word[w_sort[i]])结果含有表情包,怎么去除
可以使用正则表达式来去除文本中的表情包,例如:
```
import re
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
"]+", flags=re.UNICODE)
return emoji_pattern.sub(r'', text)
# 在 chinese_word_cut 函数中调用 remove_emoji 函数
def chinese_word_cut(text, stopwords):
text = remove_emoji(text)
words = jieba.cut(text)
result = []
for word in words:
if word not in stopwords:
result.append(word)
return " ".join(result)
```
这样就可以在分词前去除文本中的表情包。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)