以下是使用 NLTK 库实现 IMDB 情感分析的 Python 源码:\n\npython\nimport nltk\nfrom nltk.corpus import stopwords\nfrom nltk.tokenize import word_tokenize\nfrom nltk.stem import WordNetLemmatizer\nfrom nltk.classify import NaiveBayesClassifier\nfrom nltk.classify.util import accuracy\n\n# 下载必要的语料库和模型\nnltk.download('punkt')\nnltk.download('stopwords')\nnltk.download('wordnet')\n\n# 加载 IMDB 电影评论数据集\ndef load_reviews():\n pos_reviews = []\n neg_reviews = []\n \n # 加载正面评论\n with open('positive_reviews.txt', 'r', encoding='utf-8') as file:\n for line in file:\n pos_reviews.append((line.strip(), 'positive'))\n \n # 加载负面评论\n with open('negative_reviews.txt', 'r', encoding='utf-8') as file:\n for line in file:\n neg_reviews.append((line.strip(), 'negative'))\n \n return pos_reviews, neg_reviews\n\n# 预处理评论数据\ndef preprocess_reviews(reviews):\n lemmatizer = WordNetLemmatizer()\n preprocessed_reviews = []\n \n for review, sentiment in reviews:\n # 分词\n words = word_tokenize(review.lower())\n \n # 去除停用词和标点符号\n words = [word for word in words if word.isalpha() and word not in stopwords.words('english')]\n \n # 词形还原\n words = [lemmatizer.lemmatize(word) for word in words]\n \n preprocessed_reviews.append((words, sentiment))\n \n return preprocessed_reviews\n\n# 特征提取\ndef extract_features(reviews):\n all_words = []\n \n for words, _ in reviews:\n all_words.extend(words)\n \n # 选择最常见的 2000 个词作为特征\n word_freq = nltk.FreqDist(all_words)\n feature_words = list(word_freq.keys())[:2000]\n \n features = []\n \n for words, sentiment in reviews:\n # 提取文本向量表示\n features.append((dict([(word, True) for word in words if word in feature_words]), sentiment))\n \n return features\n\n# 训练和预测\ndef train_and_predict(features):\n # 划分训练集和测试集\n train_set = features[:1500]\n test_set = features[1500:]\n \n # 使用朴素贝叶斯分类器训练模型\n classifier = NaiveBayesClassifier.train(train_set)\n \n # 输出分类器的准确率\n print("Classifier accuracy:", accuracy(classifier, test_set))\n \n return classifier\n\n# 加载评论数据集\npos_reviews, neg_reviews = load_reviews()\n\n# 预处理评论数据\npreprocessed_reviews = preprocess_reviews(pos_reviews + neg_reviews)\n\n# 提取特征\nfeatures = extract_features(preprocessed_reviews)\n\n# 训练和预测\nclassifier = train_and_predict(features)\n\n# 预测示例评论\nexample_review = "This movie is great!"\nwords = word_tokenize(example_review.lower())\nwords = [word for word in words if word.isalpha() and word not in stopwords.words('english')]\nwords = [lemmatizer.lemmatize(word) for word in words]\ninput_features = {word: True for word in words if word in feature_words}\nprint("Sentiment:", classifier.classify(input_features))\n\n\n需要注意的是,以上代码中的 positive_reviews.txtnegative_reviews.txt 是 IMDB 电影评论数据集的文件,需自行准备。此外,可以根据具体需求调整代码中的参数和特征提取方法以提高分类器的准确率。

IMDB 情感分析:使用 NLTK 朴素贝叶斯分类器实现 Python 源码

原文地址: https://www.cveoy.top/t/topic/pv4A 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录