Last active
May 13, 2017 00:20
-
-
Save lucianmarin/30a6e0d1298c0f8d41217f3538646a48 to your computer and use it in GitHub Desktop.
Return unique mentions from a string.
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
def get_mentions(text): | |
limits = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_' | |
words = text.split() | |
mentions = [] | |
for word in words: | |
if word.endswith(('.', ',', '!', '?', ':', ';')): | |
word = word[:-1] | |
if word.endswith(')'): | |
word = word[:-1] | |
if word.startswith('('): | |
word = word[1:] | |
if word.startswith('@'): | |
handle = word[1:] | |
if handle and all(c in limits for c in handle): | |
mentions.append(handle) | |
return set(mentions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment