Module: CodeModels

Defined in:
lib/emf/stats.rb

Defined Under Namespace

Classes: CountingMap

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entropy(counting_map) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/emf/stats.rb', line 42

def self.entropy(counting_map)
	s = 0.0	
	counting_map.each do |k,v|
		p = counting_map.p(k)
		s += p*Math.log(p)
	end
	-s
end

.load_models_from_dir(dir, verbose = false, max = -1)) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/emf/stats.rb', line 70

def self.load_models_from_dir(dir,verbose=false,max=-1)
	per_type_values_map = Hash.new do |pt_hash,pt_key|	
		pt_hash[pt_key] = Hash.new do |pa_hash,pa_key|
			pa_hash[pa_key] = CountingMap.new
		end
	end

	n = 0
	files = Dir[dir+'/**/*.json']
	files = files[0..(max-1)] if max!=-1
	files.each do |f|
		n+=1
		puts "...#{n}) #{f}" if verbose
		model = ::JSON.load_file(f,max_nesting=500)
		EMF.traverse(model) do |n|
			if n
				puts "\tnode: #{n['type']}" if verbose
				EMF.attrs(n).each do |a|
					puts "\t\tattr: #{a}" if verbose
					per_type_values_map[n['type']][a].inc(n[a])
				end
			end
		end
	end
	per_type_values_map
end

Instance Method Details

#combine(arr1, arr2, &op) ⇒ Object



63
64
65
66
67
# File 'lib/emf/stats.rb', line 63

def combine(arr1,arr2,&op)
	arr1.each do |el1|
		arr2.each {|el2| op.call(el1,el2)}
	end
end

#combine_self(arr, &op) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/emf/stats.rb', line 55

def combine_self(arr,&op)
	for i in 0..(arr.count-2)
		for j in (i+1)..(arr.count-1)
			op.call(arr[i],arr[j])
		end
	end
end

#idf(n, n_docs) ⇒ Object



51
52
53
# File 'lib/emf/stats.rb', line 51

def idf(n,n_docs)
	Math.log(n_docs.to_f/n.to_f)
end