week1-text preprocessing->feature extraction

本文探讨了文本分类中的预处理技术,如分词、标准化,并介绍了如何将文本转换为特征向量,包括词袋模型及其存在的问题,以及如何通过n-gram改进词序考虑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


tips: text classification can be used to sentiment analysis.

text preprocessing

Tokenization

How to process text depends on what you think of text as.
a sequence of

  • characters
  • words
  • phrases and named entities
  • sentences
  • paragraphs

Here, we think of text as a sequence of words because we reckon that a word is a meaningful sequence of characters.
Therefore, we should extract all words from a sentence.This process is called tokenization. So what’s the boundary of words?
Here, we mainly talk about English.
In English we can split a sentence by spaces or punctuation.
Three methods of tokenization are built in Python ntlk liberary.

  1. whitespace tokenizer
  2. puctuation tokenizer
  3. treebankword tokenizer

examples

Normalization

  • stemming
    rules of stemming

  • lemmatization
    rules of lemmatization

examples

transforming tokens into features / text to text vector

== bag of words==

  • count occurrences of a particular token in our text
    the most simple
    problems:
  1. loose word order
  2. counters are not normalized
  • so, for word order, we count token pairs, triplets,etc. n-gram
  • therefore, there are too many features
  • then, we remove some n-grams based on their occurrence frequency in documents of our corpus(df).(remove too high or too low)
  • and then, all features we have are moderately appearing among documents of our corpus. Next, we should focus on the value of feature columns.–or term frequency.
    step by step
  • and then more accurately, we can get df in detail, not just medium df.

-IDF

Then, we multiply them together as the value of feature column.
Tf-Idf

python code
python code

For now, you have vectorize your text by using a couple of numbers. You still don’t do text classification. The simplist way is logestic regression.

LDA(Latent Dirichlet allocation)是一种常用的主题模型,它可以将文档集中每篇文档的主题按照概率分布的形式给出。它是一种无监督学习算法,在训练时仅需要输入文档集并给定主题数量。这一模型目前在文本挖掘,包括文本主题识别、文本分类以及文本相似度计算方面均有应用。请利用week.csv提供的广州八大热门糖水店的评论数据,进一步对评论文本(即cus_comment)进行话题识别与分析。注意,本周使用了两类方法来实现lda(sklearn和gensim),本次作业选自己喜欢的来实现即可。 1. 文档预处理。 一般来讲,LDA在评论等短文本上的效果并不理想,且多数情况下,我们希望给话题赋予时间含义,以讨论其“波动性”。因此,往往先需要按时间进行文档的生成,比如,将某一店铺的评论按年进行合并,即将某店铺某年发布的所有评论视为一个文档。请实现一个模块,其中包含一个或多个函数,其能够读取该数据集并将之分店铺(共8家店铺,可根据shopID进行区分)处理以天(或其他时间单位)为单位的文档集合。 2. 文本的特征表示。 实现一个模块,通过一个或多个函数,将每个文档转变为词频特征表示,以形成文档-词语的词频矩阵,可以选择使用sklearn中的CountVectorizer和TfidfVectorizer两种方式。也可以使用gensim中的dictionary.doc2bow等。 3. 文本的话题分析。 实现一个模块,通过一个或多个函数,借助sklearn.decomposition中的LatentDirichletAllocation构建主题模型(话题数目可以自主指定),并对发现的主题进行分析(每个主题对应的词语可利用model.components_来查看,每篇文档的主题概率分布可通过model.transform来查看)。也可以参考demo里的ldav.py,用gensim进行LDA分析,并进行可视化。 4. 序列化保存。 利用pickle或json对所得到的lda模型、对应的词频矩阵、以及特征表示等进行序列化保存。 5. (附加题)根据困惑度选取最优话题数。 任务3中的超参数k(即话题的数目)变化时,评价LDA模型的一个指标即困惑度(lda.perplexity)会随之波动,尝试绘制困惑度随话题数目k变化的曲线,找到较优的k。 6. (附加题)话题分布的时间趋势分析。 根据评论文档的时间信息,观察特定话题随着时间的变化趋势,即分析某一话题在不同时间段内的出现频率或权重,以便了解该话题在不同时期内的热度变化。 参考材料: 1. https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html 2. https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html#sklearn.feature_extraction.text.TfidfVectorizer 3. https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.LatentDirichletAllocation.html 4. https://radimrehurek.com/gensim/apiref.html#api-reference
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值