文本分词:

import jieba

text = "今天天气真好,适合出门散步。"
seg_list = jieba.cut(text)  # 精确模式分词
print(" ".join(seg_list))  # 输出分词结果,用空格分隔

停用词处理:

stopwords = [",", "。", "!", "?", "的", "了", "是", "我", "你", "他", "她"]  # 定义停用词列表

text = "今天天气真好,适合出门散步。"
seg_list = jieba.cut(text)  # 精确模式分词
result = []  # 保存结果
for word in seg_list:
    if word not in stopwords:  # 如果不是停用词
        result.append(word)  # 保存到结果列表中
print(" ".join(result))  # 输出结果,用空格分隔

文本向量化:

from sklearn.feature_extraction.text import CountVectorizer

texts = ["今天天气真好,适合出门散步。",
         "明天可能会下雨,最好待在家里。",
         "今天上班很忙,没有时间休息。"]

# 创建CountVectorizer对象,用于将文本向量化
vectorizer = CountVectorizer()

# 对texts进行向量化
X = vectorizer.fit_transform(texts)

# 将向量化结果转换为数组并输出
print(X.toarray())

输出结果为:

[[0 1 0 1 1 1 0 1 0 0 1 0]
 [1 0 1 0 0 0 1 0 1 1 0 1]
 [0 0 0 0 1 0 1 0 0 0 0 0]]

其中,每一行表示一个文本的向量化结果,每一列表示一个单词,对应的值表示该单词在该文本中出现的次数

请采用文本预处理方法实现文本分词、停用词处理、文本向量化可采用one-hot。python代码 编写

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

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