Skip to content
Snippets Groups Projects
Commit 613f619f authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Created task to generate pajek tags graph

parent 69f3142e
No related branches found
No related tags found
Loading
require 'json'
namespace :tag do
desc 'Generate tag clusters'
task generate_clusters: :environment do
hash = {}
edges_total = 0
outDIR = "tmp/"
def swap(a, b)
tmp = a
a = b
b = tmp
end
LearningObject.all.each do |lo|
# for each lo, count tags and tag pairs and add to hash
# if id1 <= id2
# hash[id1][id2] will equal how many times tags with id1 and id2 appear together on a LO
lo.tags.each.with_index do |t, i|
hash[t.id] = {} if hash[t.id].nil?
lo.tags.drop(i+1).each do |t2|
if t.id > t2.id
swap(t, t2)
hash[t.id] = {} if hash[t.id].nil?
end
if hash[t.id][t2.id].nil?
hash[t.id][t2.id] = 0
end
hash[t2.id] = {} if hash[t2.id].nil?
hash[t2.id][t2.id] = 0 if hash[t2.id][t2.id].nil?
hash[t2.id][t2.id] += 1
hash[t.id][t.id] = 0 if hash[t.id][t.id].nil?
hash[t.id][t.id] += 1
hash[t.id][t2.id] += 1
end
end
end
File.open(outDIR + "pajek.net", "w+") do |f|
f << "*Vertices #{Tag.all.size}\n"
# tags = Tag.all.to_ary
tag_index = {}
Tag.all.each_with_index do |t,i|
f << "#{i+1} \"#{t.name}\"\n"
tag_index[t.id] = i+1
end
f << "*Edges #{edges_total}\n"
hash.each do |id1, ids2Hash|
ids2Hash.each do |id2, value|
if id1 != id2
f << "#{tag_index[id1]} #{tag_index[id2]} #{hash[id1][id2].to_f/(hash[id1][id1].to_f+hash[id2][id2].to_f)}\n"
end
end
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment