Skip to content

Instantly share code, notes, and snippets.

@lucianmarin
Last active May 13, 2017 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucianmarin/30a6e0d1298c0f8d41217f3538646a48 to your computer and use it in GitHub Desktop.
Save lucianmarin/30a6e0d1298c0f8d41217f3538646a48 to your computer and use it in GitHub Desktop.
Return unique mentions from a string.
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