-
Notifications
You must be signed in to change notification settings - Fork 3
/
jessica_emoint_data_conversion.py
86 lines (76 loc) · 2.01 KB
/
jessica_emoint_data_conversion.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#########jessica_emoint_data_conversion.py#########
import re
import csv
from pyspark import *
from pyspark.sql import *
from pyspark.sql.types import *
from pyspark.sql.functions import *
sc = SparkContext("local")
sqlContext = SparkSession.builder.getOrCreate()
def str2float(input):
try:
return float(input)
except:
return None
#str2float("7.567")
'''
load the files to data
'''
'''
emotion_tag = "fear"
data_file = "*.train.txt"
texts, tags = convert_file_to_text_and_tag_list(
emotion_tag,
data_file)
'''
def convert_file_to_text_and_tag_list(
emotion_tag,
data_file):
udf_str2float = udf(str2float, FloatType())
schema = StructType()\
.add("tweet_id",StringType(),True)\
.add("text",StringType(),True)\
.add("emotion",StringType(),True)\
.add("intensity",FloatType(),True)
train = sqlContext.read.format('csv')\
.options(delimiter='\t')\
.schema(schema)\
.load(data_file)\
.withColumn("intensity", udf_str2float("intensity"))
train.registerTempTable("train")
train_list = sqlContext.sql(u"""
SELECT *,
CASE
WHEN emotion = '%s' THEN 1
ELSE 0
END AS label
FROM train
WHERE emotion IS NOT NULL
"""%(emotion_tag)).collect()
texts = [r.text for r in train_list]
tags = [r.label for r in train_list]
return texts, tags
def convert_file_to_text_and_score_list(
emotion_tag,
data_file):
udf_str2float = udf(str2float, FloatType())
schema = StructType()\
.add("tweet_id",StringType(),True)\
.add("text",StringType(),True)\
.add("emotion",StringType(),True)\
.add("intensity",FloatType(),True)
train = sqlContext.read.format('csv')\
.options(delimiter='\t')\
.schema(schema)\
.load(data_file)\
.withColumn("intensity", udf_str2float("intensity"))
train.registerTempTable("train")
train_list = sqlContext.sql(u"""
SELECT *
FROM train
WHERE emotion = '%s' AND intensity IS NOT NULL
"""%(emotion_tag)).collect()
texts = [r.text for r in train_list]
scores = [r.intensity for r in train_list]
return texts, scores
#########jessica_emoint_data_conversion.py#########