-
Notifications
You must be signed in to change notification settings - Fork 2
/
CreateJsonFromTweets.py
50 lines (38 loc) · 1.67 KB
/
CreateJsonFromTweets.py
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
from featureExtract.demo import *
from utilities.readTweet import *
import os, json, string
import io
def GroupTweetTokenizer(Groupoptions): # e.g. GroupTweetTokenizer({'condition':'control'})
#print 'hello'
CMUParserLoc = '../ark-tweet-nlp-0.3.2/'
csvFileLoc = '../data/clpsych2015/schizophrenia/anonymized_user_manifest.csv'
controlFolder = '../data/clpsych2015/schizophrenia/anonymized_control_tweets/'
schizoFolder ='../data/clpsych2015/schizophrenia/anonymized_schizophrenia_tweets/'
x = readCSV(csvFileLoc,Groupoptions)
allControlWithTweets = getTweetsForGroup(x, controlFolder, schizoFolder, fields = ['text']) #read tweet files and only keep the text part
allControlText = getFieldFromGroup(allControlWithTweets, 'text') #convert it to a list of strings
CreateListofTokens(allControlText,CMUParserLoc)
def CreateListofTokens(alltext,CMUParserLoc):
options = '--output-format conll '
Dict = []
f = open('tweets_control.txt', 'w')
for t in alltext:
f.write(t.encode('ascii', 'ignore')+'\n')
f.close()
os.system(CMUParserLoc + 'runTagger.sh ' + options + '../schizophreniaThroughTweets/tweets_control.txt > TwokenizerOut_tweets_control.txt')
os.remove('tweets_control.txt')
f = open('TwokenizerOut_tweets_control.txt', 'r')
L = []
for l in f:
if l == '\n':
Dict.append(L)
L = []
continue
values = l.split("\t")
L.append(values[0])
f.close()
os.remove('TwokenizerOut_tweets_control.txt')
#Dict.append(L)
with open('data.txt', 'w') as outfile:
json.dump(Dict, outfile)
GroupTweetTokenizer({'condition':'control'})