-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleantry.py
More file actions
53 lines (36 loc) · 1.5 KB
/
cleantry.py
File metadata and controls
53 lines (36 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 24 12:39:59 2017
#Reference/Source: Prof. Gene Lee's codes from Dropbox code files.
Reference: #http://stackoverflow.com/questions/24399820/expression-to-remove-url-links-from-twitter-tweet
"""
#Reference/Source: Prof. Gene Lee's codes from Dropbox code files.
#http://stackoverflow.com/questions/24399820/expression-to-remove-url-links-from-twitter-tweet
import re
import json
import HTMLParser
import string
from nltk.corpus import stopwords
html_parser = HTMLParser.HTMLParser()
infile = open("tweet_stream_trump_cal nev.json",'r')
content = infile.readlines()
#Removing links from tweets
wourl_tweets = []
for each_line in content:
url_tweet = re.sub('http.://\w+.*', " ", each_line)
wourl_tweets.append(url_tweet)
#content = html_parser.unescape(content)
processed_tweets = []
for each_line in wourl_tweets:
a = string.punctuation
b = string.digits
c = string.maketrans(a, len(a)*" ")
d = string.maketrans(b, len(b)*" ")
tweets = str(each_line).translate(c)
tweets = str(tweets).translate(d)
processed_tweets.append(tweets.strip().encode('utf-8'))
infile.close()
with open("processed_tweets.json","w") as outfile:
json.dump(processed_tweets,outfile,indent=4)
#Reference/Source: Prof. Gene Lee's codes from Dropbox code files.
#http://stackoverflow.com/questions/24399820/expression-to-remove-url-links-from-twitter-tweet