forked from xldenis/proj1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.rb
68 lines (58 loc) · 1.89 KB
/
features.rb
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
require 'json'
require 'fast_stemmer'
require 'csv'
Encoding.default_external = "utf-8"
def markdown_strip(md)
strip = md.gsub(/!?\[([^\[\]\(\)]*)\]\(.*\)/,'\1') #links
strip = strip.gsub(/\*\*?([^\*]+)\*\*?/,'\1') #emphasis
strip = strip.gsub(/\#{1,6}\s*(\w+)\n/,'\1')
end
def tokenize(string)
string.downcase.split.map {|w| w.gsub('^',' ').split }.flatten
.map {|w| Stemmer::stem_word(w.gsub(/[.|,|!|?|"|:]/,'')).force_encoding('utf-8')}
end
def traverse(node, &trans)
children = if node['replies'] && node['replies'] != ""
node['replies']['data']['children'].map {|n| traverse(n, &trans)}
end
[trans.call(node)] + (children || []).flatten
end
histogram = Hash.new(0)
posts = []
ARGV.each do |file|
comments = JSON.parse(File.read(file).force_encoding('UTF-8'))['comments']
filtered = comments.map do |com|
traverse(com) do |comment|
body_string = markdown_strip(comment['body'])
{
author: comment['author'],
body: body_string,
subreddit: comment['subreddit'],
score: comment['score'],
tokens: tokenize(body_string),
}
end
end.flatten
filtered.each {|row| row[:tokens].map {|tok| histogram[tok] += 1} }
posts << filtered
end
features = histogram.sort_by {|k,v| v}.last(2500).map {|m| m[0]}
posts.each_with_index do |post,i|
uni_mat = post.map do |comment|
mini_histo = comment[:tokens].each_with_object(Hash.new(0)) {|w,c| c[w] += 1}
features.map {|f| mini_histo[f]}
end
puts features
name = File.basename(ARGV[i],'.json') + "_filtered.csv"
CSV.open(name, 'w') do |csv|
csv << features
uni_mat.map {|u| csv << u}
end
end
# File.open(name,'w:UTF-8') {|f| f.write(filtered.to_json)}
# name = File.basename(file,'.json') + "_filtered.csv"
# CSV.open(name, 'w') do |csv|
# filtered.each do |ft|
# csv <<[ft[:author], ft[:body], ft[:subreddit], ft[:score], ft[:tokens].join(" ")]
# end
# end