捕获标记模式

Python 中的情感分析

Violeta Misheva

Data Scientist

字符串运算与比较

# 检查字符串是否仅由字母组成  
my_string.isalpha()
# 检查字符串是否仅由数字组成 
my_string.isdigit()
# 检查字符串是否仅由字母数字字符组成
my_string.isalnum()
Python 中的情感分析

结合列表推导的字符串运算

# 基础分词
word_tokens = [word_tokenize(review) for review in reviews.review]
# 仅保留由字母组成的标记
cleaned_tokens = [[word for word in item if word.isalpha()] for item in word_tokens]
len(word_tokens[0])
87
len(cleaned_tokens[0])
78
Python 中的情感分析

正则表达式

import re
my_string = '#Wonderfulday'
# 提取 # 后接任意字母(大小写)
x = re.search('#[A-Za-z]', my_string)
x
<re.Match object; span=(0, 2), match='#W'>
Python 中的情感分析

BOW 的标记模式

# CountVectorizer 的默认标记模式
'\b\w\w+\b'
# 指定自定义标记模式
CountVectorizer(token_pattern=r'\b[^\d\W][^\d\W]+\b')
Python 中的情感分析

开始练习!

Python 中的情感分析

Preparing Video For Download...