4 import pyarrow.dataset as ds
5 import pyarrow.parquet as pq
6 from itertools import groupby, islice, chain
8 from collections import Counter
12 from nltk import wordpunct_tokenize, MWETokenizer, sent_tokenize
13 from nltk.corpus import stopwords
14 from nltk.util import ngrams
16 from random import random
19 # taken from https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url
20 urlregex = re.compile(r"[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)")
22 # compute term frequencies for comments in each subreddit by week
23 def weekly_tf(partition, mwe_pass = 'first'):
24 dataset = ds.dataset(f'/gscratch/comdata/output/reddit_comments_by_subreddit.parquet/{partition}', format='parquet')
25 if not os.path.exists("/gscratch/comdata/users/nathante/reddit_comment_ngrams_10p_sample/"):
26 os.mkdir("/gscratch/comdata/users/nathante/reddit_comment_ngrams_10p_sample/")
28 if not os.path.exists("/gscratch/comdata/users/nathante/reddit_tfidf_test_authors.parquet_temp/"):
29 os.mkdir("/gscratch/comdata/users/nathante/reddit_tfidf_test_authors.parquet_temp/")
31 ngram_output = partition.replace("parquet","txt")
33 if mwe_pass == 'first':
34 if os.path.exists(f"/gscratch/comdata/users/nathante/reddit_comment_ngrams_10p_sample/{ngram_output}"):
35 os.remove(f"/gscratch/comdata/users/nathante/reddit_comment_ngrams_10p_sample/{ngram_output}")
37 batches = dataset.to_batches(columns=['CreatedAt','subreddit','body','author'])
40 schema = pa.schema([pa.field('subreddit', pa.string(), nullable=False),
41 pa.field('term', pa.string(), nullable=False),
42 pa.field('week', pa.date32(), nullable=False),
43 pa.field('tf', pa.int64(), nullable=False)]
46 author_schema = pa.schema([pa.field('subreddit', pa.string(), nullable=False),
47 pa.field('author', pa.string(), nullable=False),
48 pa.field('week', pa.date32(), nullable=False),
49 pa.field('tf', pa.int64(), nullable=False)]
52 dfs = (b.to_pandas() for b in batches)
55 df['week'] = (df.CreatedAt - pd.to_timedelta(df.CreatedAt.dt.dayofweek, unit='d')).dt.date
58 dfs = (add_week(df) for df in dfs)
60 def iterate_rows(dfs):
62 for row in df.itertuples():
65 rows = iterate_rows(dfs)
67 subreddit_weeks = groupby(rows, lambda r: (r.subreddit, r.week))
69 if mwe_pass != 'first':
70 mwe_dataset = pd.read_feather(f'/gscratch/comdata/users/nathante/reddit_multiword_expressions.feather')
71 mwe_dataset = mwe_dataset.sort_values(['phrasePWMI'],ascending=False)
72 mwe_phrases = list(mwe_dataset.phrase)
73 mwe_phrases = [tuple(s.split(' ')) for s in mwe_phrases]
74 mwe_tokenizer = MWETokenizer(mwe_phrases)
75 mwe_tokenize = mwe_tokenizer.tokenize
78 mwe_tokenize = MWETokenizer().tokenize
80 def remove_punct(sentence):
82 for token in sentence:
85 if c not in string.punctuation:
87 if len(new_token) > 0:
88 new_sentence.append(new_token)
92 stopWords = set(stopwords.words('english'))
94 # we follow the approach described in datta, phelan, adar 2017
95 def my_tokenizer(text):
96 # remove stopwords, punctuation, urls, lower case
101 text = urlregex.sub("", text)
104 sentences = sent_tokenize(text)
107 sentences = map(wordpunct_tokenize, sentences)
111 sentences = map(remove_punct, sentences)
113 # remove sentences with less than 2 words
114 sentences = filter(lambda sentence: len(sentence) > 2, sentences)
116 # datta et al. select relatively common phrases from the reddit corpus, but they don't really explain how. We'll try that in a second phase.
117 # they say that the extract 1-4 grams from 10% of the sentences and then find phrases that appear often relative to the original terms
118 # here we take a 10 percent sample of sentences
119 if mwe_pass == 'first':
120 sentences = list(sentences)
121 for sentence in sentences:
123 grams = list(chain(*map(lambda i : ngrams(sentence,i),range(4))))
124 with open(f'/gscratch/comdata/users/nathante/reddit_comment_ngrams_10p_sample/{ngram_output}','a') as gram_file:
126 gram_file.write(' '.join(ng) + '\n')
127 for token in sentence:
128 if token not in stopWords:
133 sentences = map(mwe_tokenize, sentences)
134 sentences = map(lambda s: filter(lambda token: token not in stopWords, s), sentences)
135 for sentence in sentences:
136 for token in sentence:
139 def tf_comments(subreddit_weeks):
140 for key, posts in subreddit_weeks:
141 subreddit, week = key
143 authors = Counter([])
145 tokens = my_tokenizer(post.body)
147 authors.update([post.author])
149 for term, tf in tfs.items():
150 yield [True, subreddit, term, week, tf]
152 for author, tf in authors.items():
153 yield [False, subreddit, author, week, tf]
155 outrows = tf_comments(subreddit_weeks)
159 with pq.ParquetWriter(f"/gscratch/comdata/users/nathante/reddit_tfidf_test.parquet_temp/{partition}",schema=schema,compression='snappy',flavor='spark') as writer, pq.ParquetWriter(f"/gscratch/comdata/users/nathante/reddit_tfidf_test_authors.parquet_temp/{partition}",schema=author_schema,compression='snappy',flavor='spark') as author_writer:
163 chunk = islice(outrows,outchunksize)
164 chunk = (c for c in chunk if c[1] is not None)
165 pddf = pd.DataFrame(chunk, columns=["is_token"] + schema.names)
166 author_pddf = pddf.loc[pddf.is_token == False, schema.names]
167 pddf = pddf.loc[pddf.is_token == True, schema.names]
168 author_pddf = author_pddf.rename({'term':'author'}, axis='columns')
169 author_pddf = author_pddf.loc[:,author_schema.names]
170 table = pa.Table.from_pandas(pddf,schema=schema)
171 author_table = pa.Table.from_pandas(author_pddf,schema=author_schema)
174 if table.shape[0] != 0:
175 writer.write_table(table)
177 if author_table.shape[0] != 0:
178 author_writer.write_table(author_table)
185 author_writer.close()
188 def gen_task_list(mwe_pass='first'):
189 files = os.listdir("/gscratch/comdata/output/reddit_comments_by_subreddit.parquet/")
190 with open("tf_task_list",'w') as outfile:
192 if f.endswith(".parquet"):
193 outfile.write(f"./tf_comments.py weekly_tf --mwe-pass {mwe_pass} {f}\n")
195 if __name__ == "__main__":
196 fire.Fire({"gen_task_list":gen_task_list,
197 "weekly_tf":weekly_tf})