Skip to content

Instantly share code, notes, and snippets.

@dreikanter
Created May 25, 2012 10:18
Show Gist options
  • Save dreikanter/2787146 to your computer and use it in GitHub Desktop.
Save dreikanter/2787146 to your computer and use it in GitHub Desktop.
Extract hashtags from a string (w/o regexps) #python
text = """Sample #line of #text to demonstrate #hashtags
extraction. Here is some duplicate ones: #text #hashtags #tags."""
def get_hashtags(text, order=False):
tags = set([item.strip("#.,-\"\'&*^!") for item in text.split() if (item.startswith("#") and len(item) < 256)])
return sorted(tags) if order else tags
print "\n".join(get_hashtags(text, True))
@sudlo
Copy link

sudlo commented Apr 4, 2019

i try executing this cript i get the following
File "get-hashtags.py", line 10 print "/n.join(get_hashtags(text, True)) ^ SyntaxError: EOL while scanning string literal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment