使用python实现词项词典的生成
时间: 2023-10-07 20:09:45 浏览: 50
生成词项词典可以使用Python内置的collections库中的Counter类。具体步骤如下:
1. 读取文本文件,将文本分词,得到词项列表。
2. 使用Counter类统计词项的出现次数。
3. 将Counter对象转换为字典类型,得到词项词典。
下面是一个示例代码:
```python
from collections import Counter
# 读取文本文件
with open('text.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分词,得到词项列表
words = text.split()
# 统计词项的出现次数
word_count = Counter(words)
# 将Counter对象转换为字典类型,得到词项词典
word_dict = dict(word_count)
```
在这个示例中,我们首先使用Python内置的open函数读取了名为text.txt的文本文件,并将文件内容保存在变量text中。接着,我们使用字符串的split方法将文本分词,得到词项列表words。然后,我们使用collections库中的Counter类统计词项的出现次数,并将结果保存在变量word_count中。最后,我们将Counter对象word_count转换为字典类型,得到词项词典word_dict。
相关问题
使用python生成词项词典和倒排记录表
生成词项词典和倒排记录表的具体实现步骤如下:
1. 首先,需要读入一个文本文件,并对文本进行分词处理。可以使用Python中的nltk库或jieba库进行分词。
2. 对于每个分词结果,需要对其进行词项归一化操作,比如将其转化为小写字母形式等。
3. 对于每个词项,需要记录其在文本中的出现次数,以及出现该词项的文档ID。
4. 根据文档ID,将每个词项加入到对应的倒排记录表中。
5. 最后,将所有词项及其对应的出现次数和倒排记录表保存到磁盘上,以便后续的信息检索操作使用。
下面是一个简单的Python代码示例,用于生成词项词典和倒排记录表:
```python
import jieba
# 读取文本文件
with open('test.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分词
words = jieba.lcut(text)
# 词项归一化
words = [word.lower() for word in words]
# 生成词项词典和倒排记录表
word_dict = {}
for i, word in enumerate(words):
if word not in word_dict:
word_dict[word] = {'tf': 1, 'doc_ids': [i]}
else:
word_dict[word]['tf'] += 1
word_dict[word]['doc_ids'].append(i)
# 保存词项词典和倒排记录表到磁盘上
with open('word_dict.txt', 'w', encoding='utf-8') as f:
for word, info in word_dict.items():
f.write('{}\t{}\t{}\n'.format(word, info['tf'], ','.join(map(str, info['doc_ids']))))
```
在上述代码中,我们使用了jieba库进行分词,并对每个词项进行了归一化操作。然后,我们遍历所有词项,根据其出现次数和文档ID生成了词项词典和倒排记录表。最后,我们将这些信息保存到磁盘上。需要注意的是,我们在保存倒排记录表时使用了逗号分隔的文档ID列表,以便后续进行查询操作时能够方便地进行处理。
用python实现情感词典生成生成情感词然后朴素贝叶斯再去分类的代码
好的,下面是一个简单的情感词典生成和朴素贝叶斯分类的Python代码示例:
```python
import os
import re
from collections import Counter
import math
# 情感词典文件路径
SENTIMENT_DICT_FILE = 'sentiment_dict.txt'
# 训练集文件路径
TRAIN_DATA_FILE = 'train_data.txt'
# 测试集文件路径
TEST_DATA_FILE = 'test_data.txt'
# 情感词典生成函数
def generate_sentiment_dict(train_data_file, output_file):
sentiment_words = Counter()
with open(train_data_file, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
words, label = line.split('\t')
if label == '1':
sentiment_words.update(words.split(' '))
with open(output_file, 'w', encoding='utf-8') as f:
for word, freq in sentiment_words.items():
f.write('{}\t{}\n'.format(word, freq))
# 朴素贝叶斯分类器
class NaiveBayesClassifier:
def __init__(self):
self.total_docs = 0 # 总文档数
self.class_docs = {} # 每个类别的文档数
self.word_freq = {} # 每个词在每个类别中的出现次数
self.classes = set() # 类别集合
self.vocab = set() # 词汇表
# 训练函数
def train(self, train_data_file):
with open(train_data_file, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
words, label = line.split('\t')
self.total_docs += 1
self.class_docs[label] = self.class_docs.get(label, 0) + 1
for word in words.split(' '):
self.word_freq[label] = self.word_freq.get(label, Counter())
self.word_freq[label][word] += 1
self.vocab.add(word)
self.classes.add(label)
# 预测函数
def predict(self, text):
words = re.findall(r'\w+', text)
scores = {c: math.log(self.class_docs[c] / self.total_docs) for c in self.classes}
for word in words:
if word not in self.vocab:
continue
for c in self.classes:
freq = self.word_freq[c].get(word, 0)
scores[c] += math.log((freq + 1) / (sum(self.word_freq[c].values()) + len(self.vocab)))
return max(scores, key=scores.get)
# 生成情感词典
generate_sentiment_dict(TRAIN_DATA_FILE, SENTIMENT_DICT_FILE)
# 加载情感词典
sentiment_dict = set()
with open(SENTIMENT_DICT_FILE, 'r', encoding='utf-8') as f:
for line in f:
word, freq = line.strip().split('\t')
if int(freq) > 10: # 过滤掉出现次数过少的词
sentiment_dict.add(word)
# 训练朴素贝叶斯分类器
classifier = NaiveBayesClassifier()
classifier.train(TRAIN_DATA_FILE)
# 测试朴素贝叶斯分类器
with open(TEST_DATA_FILE, 'r', encoding='utf-8') as f:
num_correct = 0
num_total = 0
for line in f:
line = line.strip()
if not line:
continue
text, label = line.split('\t')
pred_label = classifier.predict(text)
num_correct += int(pred_label == label)
num_total += 1
accuracy = num_correct / num_total
print('Accuracy: {:.2%}'.format(accuracy))
```
上面的代码实现了以下功能:
1. 从训练集文件中生成情感词典文件。
2. 加载情感词典和训练集文件,训练朴素贝叶斯分类器。
3. 加载测试集文件,测试朴素贝叶斯分类器的准确率。
请注意,这只是一个简单的示例代码,实际上情感分析需要更多的预处理和特征提取步骤。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)