Skip to content

Instantly share code, notes, and snippets.

@saiplanner
Forked from vickyqian/preprocesstweet.txt
Created May 7, 2019 06:19
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 saiplanner/740907c277c91038171e18b12f3313e1 to your computer and use it in GitHub Desktop.
Save saiplanner/740907c277c91038171e18b12f3313e1 to your computer and use it in GitHub Desktop.
Preprocess Tweets
###Preprocess tweets
def processTweet2(tweet):
# process the tweets
#Convert to lower case
tweet = tweet.lower()
#Convert www.* or https?://* to URL
tweet = re.sub('((www\.[^\s]+)|(https?://[^\s]+))','URL',tweet)
#Convert @username to AT_USER
tweet = re.sub('@[^\s]+','AT_USER',tweet)
#Remove additional white spaces
tweet = re.sub('[\s]+', ' ', tweet)
#Replace #word with word
tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
#trim
tweet = tweet.strip('\'"')
return tweet
###get stopword list
def getStopWordList(stopWordListFileName):
#read the stopwords file and build a list
stopWords = []
stopWords.append('AT_USER')
stopWords.append('URL')
fp = open(stopWordListFileName, 'r')
line = fp.readline()
while line:
word = line.strip()
stopWords.append(word)
line = fp.readline()
fp.close()
return stopWords
stopWords = []
st = open('stopwords.txt', 'r')
stopWords = getStopWordList('stopwords.txt')
def replaceTwoOrMore(s):
#look for 2 or more repetitions of character and replace with the character itself
pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
return pattern.sub(r"\1\1", s)
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment