Utilities for reading, writing, and processing tweets
Project description
Little Bird
Basic utilties for processing Tweets. Includes:
TweetTokenizerfor tokenizing the Tweet contentTweetReaderfor easily iterating over TweetsTweetWriterfor conveniently writing one or more Tweets to a file in JSONlines format
Installation
There are two options for installing littlebird.
1. Install from source
git clone https://github.com/AADeLucia/littlebird.git
cd littlebird
python setup.py develop
2. Install with pip
pip install git+git://github.com/AADeLucia/littlebird.git#egg=littlebird
Read and Write a Tweet JSONlines file
The example below reads in a Tweet file, filters to Tweets that have a hashtag, and writes out to a new file.
TweetWritercan write a single Tweet or list of Tweets to a file in JSONlines format. It will also automatically open a GZIP file if the provided filename has a .gz extension. If you are writing to a GZIP file, it is recommended to write all Tweets at once instead of writing incrementally; this provides better file compression. If you do need to write incrementally, I recommend writing to a normal file and GZIPping after.
from littlebird import TweetReader, TweetWriter
# File in JSONlines form. Automatically handles GZIP files.
tweet_file = "2014_01_02.json.gz"
reader = TweetReader(tweet_file)
# Iterate over Tweets
# Save Tweets that contain hashtags
filtered_tweets = []
for tweet in reader.read_tweets():
if tweet.get("truncated", False):
num_hashtags = len(tweet["extended_tweet"]["entities"]["hashtags"])
else:
num_hashtags = len(tweet["entities"]["hashtags"])
if num_hashtags > 0:
filtered_tweets.append(tweet)
# Write out filtered Tweets
writer = TweetWriter("filtered.json")
writer.write(filtered_tweets)
You can also skip retweeted and quoted statuses
for tweet in reader.read_tweets(skip_retweeted_and_quoted=True):
Tokenize Tweets
There are multiple pre-defined tokenizers that are useful for different downstream usecases.
- TweetTokenizer: default customizable tokenizer
- BERTweetTokenizer: tokenizer based off the original BERTweet pre-processing from the BERTweet model repo.
- GloVeTweetTokenizer: tokenizer based off a modified version of the GloVe preprocessor. It's based off of a modified version because the original ruby processing script does not work.
You can also write your own tokenizer. Examples are below.
Example usage
A basic example using the default Tokenizer settings is below.
from littlebird import TweetReader, TweetTokenizer
# File in JSONlines form. Automatically handles GZIP files.
tweet_file = "2014_01_02.json.gz"
reader = TweetReader(tweet_file)
tokenizer = TweetTokenizer()
# Iterate over tweets and get the tokenized text.
# Automatically checks for retweeted and quoted text except if
# tokenizer = TweetTokenizer(include_retweeted_and_quoted_content=False)
for tweet in reader.read_tweets():
# Tokenize the Tweet's text
tokens = tokenizer.get_tokenized_tweet_text(tweet)
# Get tweet hashtags. Also includes hashtags from retweeted and quoted statuses
hashtags = tokenizer.get_hashtags(tweet)
You can directly access the internal tokenize method with tokenizer.tokenize(text).
If you do not need to skip over tweets and only need the text, then you can use the tokenize_tweet_file() method. This method gets the tokenized text from the entire tweet (retweet, quoted, and full text).
from littlebird import TweetTokenizer
tweet_file = "2014_01_02.json.gz"
tokenizer = TweetTokenizer()
# Returns a tokenized string for each tweet
tokenized_tweets = tokenizer.tokenize_tweet_file(tweet_file)
# Returns a list of tokens for each tweet
tokenized_tweets = tokenizer.tokenize_tweet_file(tweet_file, return_tokens=True)
Tokenizer settings
Available TweetTokenizer settings:
language: right now it only really supports English, but as long as you change thetoken_patternaccordingly, it should work with other languages. A future integration is usingMosesfor Arabic tokenization.token_pattern: Pattern to match for acceptable tokens. Default isr"\b\w+\b"stopwords: provide a list of stopwords to remove from the text. Default isNonefor no stopword removal.remove_hashtags: Default isFalseto keep hashtags in the text (only strips the "#" symbol)lowercase: Default isTrueto lowercase all of the text. Change this toFalseif you are doing case-sensitive tasks like Name Entity Recognition (NER)
The tokenizer works in the following steps:
- Remove hashtags (optional)
- Remove URLs, handles, and "RT"
- Lowercase the text (optional)
- Find all tokens that match the
token_patternwithregex.findall(token_pattern, text) - Remove stopwords (optional)
Token patterns
The token pattern is extremely important to set for your use case. Below are some sample token patterns, but I highly recommend refreshing on your regular expressions if you need something more advanced.
Note: the regex package is used to access character classes like \p{L}. Basically Java regex patterns.
r"\b\w+\b"matches any token with one or more letters, numbers, and underscores. This is equivalent to"[\p{L}\_\p{N}]+"r"\b\p{L}+\b"matches any token with one or more "letters" (across all alphabets).r"\b[\w']\bmatches any token with one or more letters, numbers, and underscores. This pattern also preserves apostraphes "'", which is useful for not splitting contractions (e.g. not "can't" -> "can t"). It does not preserve quotes (e.g. "She said 'hello'" -> "she said hello")
Writing your own tokenizer
If you would like to write your own, simply extend the BaseTweetTokenizer class. See below and tweet_tokenizer.py for examples.
from littlebird import BaseTweetTokenizer
class NewTweetTokenizer(BaseTweetTokenizer):
def __init__(self, include_retweeted_and_quoted_content = True):
# Initialize super class
super().__init__(include_retweeted_and_quoted_content)
# Define class variables and settings
self.token_pattern = "aa*"
# Overwrite the tokenize method
def tokenize(self, tweet):
regex.findall(self.token_pattern, tweet)
return
# Define any helper methods as needed
def _helper(self):
return
This package is a work in progress. Feel free to open any issues you run into or recommend features. I started this package for my own NLP research with tweets.
@misc{DeLucia2020,
author = {Alexandra DeLucia},
title = {Little Bird},
year = {2020},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/aadelucia/littlebird}},
}
Copyright (c) 2020 Alexandra DeLucia
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file littlebird-twitter-utils-1.0.0.tar.gz.
File metadata
- Download URL: littlebird-twitter-utils-1.0.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b872116eb16aba576a98cda00822ecd9bc494c41247c564fb661059977b51053
|
|
| MD5 |
491ea89b2eb6ed0a84721e68cdc22af1
|
|
| BLAKE2b-256 |
730859f4de71f787217b6306b6cccff4154dea80f350a896d3bc93c3330cac04
|
File details
Details for the file littlebird_twitter_utils-1.0.0-py3-none-any.whl.
File metadata
- Download URL: littlebird_twitter_utils-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f350ce0b9d75fad4737c3195994eb50368374a2117dc4227d65d7bb3d90f49e6
|
|
| MD5 |
ce4b1fea64f77918b761e440d71dd99f
|
|
| BLAKE2b-256 |
0d990195c32596361476e9e33124405ca6db77769314f1ecca120dce7fc34841
|