Last active
December 17, 2015 04:28
-
-
Save JKirchartz/5550316 to your computer and use it in GitHub Desktop.
Transform words in a phrase to hashtags using a whitelist, without changing the upper/lower case characters in the words in the phrase (i.e. if your phrase has "Beer" but your whitelist has "beer", you'll see "#Beer")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
whitelist = ['quick','lazy','boring'] | |
tweet = "The quick brown fox jumped over the lazy dog. The Quick Brown Fox Jumped Over The Boring Dog" | |
for item in whitelist: | |
tweet = re.compile('('+item+')',re.I).sub(r'#\1', tweet, 1) | |
# remove the `1` if you want it to match all instances | |
print tweet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment