Python通过评论特征来提取与软件功能相关的评论

import jieba
from collections import Counter
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report, confusion_matrix
import pandas as pd

df = pd.read_csv('评论数据.csv').astype(str)
comments = df['pl'].tolist()
labels = df['label'].tolist()


def segment_words(sentence):
    return list(jieba.cut(sentence))  # 使用jieba库进行分词

def count_words(words):
    word_counts = Counter(words)  # 统计词语的出现频率
    return word_counts

def extract_keywords(comments, n_keywords=10):
    words = [segment_words(comment) for comment in comments]
    vectorizer = TfidfVectorizer(stop_words='english')  # 使用TF-IDF算法提取关键词
    tfidf = vectorizer.fit_transform([''.join(comment) for comment in words])
    keywords = vectorizer.get_feature_names_out()[tfidf.toarray().argsort()[0, -n_keywords:]]  # 获取重要的关键词
    return keywords

def vectorize_features(words, keywords):
    vectorizer = CountVectorizer(vocabulary=keywords)  # 将关键词转换为特征向量
    feature_vector = vectorizer.fit_transform(words).toarray()
    return feature_vector

def train_classifier(feature_vector, labels):
    X_train, X_test, y_train, y_test = train_test_split(feature_vector, labels, test_size=0.2, random_state=42)
    clf = MultinomialNB()  # 使用朴素贝叶斯分类器作为示例
    clf.fit(X_train, y_train)  # 训练分类器
    y_pred = clf.predict(X_test)
    print(classification_report(y_test, y_pred))  # 打印分类报告
    print(confusion_matrix(y_test, y_pred))  # 打印混淆矩阵
    return clf

def predict_comment(clf, feature_vector):
    y_pred = clf.predict(feature_vector)  # 使用分类器对评论进行预测
    return y_pred[0]

def process_comment(comment, clf, keywords):
    words = segment_words(comment)  # 分词
    word_counts = count_words(words)  # 统计词频
    feature_vector = vectorize_features(words, keywords)  # 转换为特征向量
    prediction = predict_comment(clf, feature_vector)  # 预测评论分类
    return prediction

def main():
    keywords = extract_keywords(comments)  # 提取关键词
    feature_vector = vectorize_features(comments, keywords)  # 转换为特征向量
    clf = train_classifier(feature_vector, labels)  # 训练分类器

    test_comment = "这个软件很好用!"
    prediction = process_comment(test_comment, clf, keywords)  #
    print(f"预测结果:{prediction}")

    if prediction == 1:
        print("评论与软件功能相关。")
    else:
        print("评论与软件功能不相关。")

if __name__ == "__main__":
    main()

请问一下,为什么这个东西跑出来的结果,只预测了与软件功能有关的评论?

img


数据集是这个样子的

img

【以下回答由 GPT 生成】

针对这个问题,我可以提供一些优化方案:

  1. 使用更高级的分词工具

目前代码中使用了jieba分词库进行分词,但是使用更高级的分词工具可能会提高分词的准确性和效果。例如,可以考虑使用HanLP或NLTK等分词工具。

  1. 增加停用词列表

在提取关键词的函数中,使用了TfidfVectorizer的stop_words参数来去除英文停用词。但是代码中并未给出自定义的中文停用词列表。应该将常见的无意义中文词语加入停用词列表,例如"的"、"是"等常用词。

示例代码:

vectorizer = TfidfVectorizer(stop_words=['的', '是'])
  1. 添加异常处理

在读取评论数据时,可以添加异常处理以防止读取错误或找不到文件的问题。可以使用try-except语句来捕获异常,并在发生异常时正确处理。

示例代码:

try:
    df = pd.read_csv('评论数据.csv').astype(str)
    comments = df['评论内容'].tolist()
    labels = df['标签'].tolist()
except FileNotFoundError:
    print("无法找到评论数据文件")

以上是我给出的一些建议和优化方案,希望对您有所帮助。如果还有其他问题,我将非常愿意为您提供帮助。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^