Захоплення шаблону токена

Аналіз тональності в Python

Violeta Misheva

Data Scientist

Оператори та порівняння рядків

# Checks if a string is composed only of letters  
my_string.isalpha()
# Checks if a string is composed only of digits 
my_string.isdigit()
# Checks if a string is composed only of alphanumeric characters
my_string.isalnum()
Аналіз тональності в Python

Оператори рядків із списковими включеннями

# Original word tokenization
word_tokens = [word_tokenize(review) for review in reviews.review]
# Keeping only tokens composed of letters
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'
# Extract #, followed by any letter, small or capital
x = re.search('#[A-Za-z]', my_string)
x
<re.Match object; span=(0, 2), match='#W'>
Аналіз тональності в Python

Шаблон токенів у BOW

# Default token pattern in CountVectorizer
'\b\w\w+\b'
# Specify a particular token pattern
CountVectorizer(token_pattern=r'\b[^\d\W][^\d\W]+\b')
Аналіз тональності в Python

Давайте потренуємось!

Аналіз тональності в Python

Preparing Video For Download...