擷取權杖樣式

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...