Skip to content

Commit

Permalink
changed evaluation script
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas Wenneker committed Aug 30, 2010
1 parent 10caabf commit febfa91
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 14 deletions.
17 changes: 17 additions & 0 deletions app/controllers/webservice_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class WebserviceController < ApplicationController
def index

puts request

ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
citation = ic.iconv(params[:citation] << ' ')[0..-2]

x = Bibmix::Pipeline.instance.execute_pipeline(citation)

respond_to do |format|
format.json { render :json => {:reference => x } }
end

end

end
2 changes: 2 additions & 0 deletions app/helpers/webservice_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module WebserviceHelper
end
327 changes: 327 additions & 0 deletions app/models/eval_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,184 @@ def type
self.mytype
end

def calc_empty_fields

enriched_results = EvalReference.all(:conditions => { :referencetype => 'enriched'})
attrs = get_reference_attrs
x = {}
attrs.each do |attr|
x[attr] = 0
end

enriched_results.each do |enriched|
attrs.each do |attr|
x[attr] += 1 if enriched.has_attr(attr)
end
end


attrs.each do |attr|
val = ((x[attr].to_f/enriched_results.size)*100).round(1)
puts "#{attr}\t\t&#{val}"+'\\\\'
end
end

def calc_not_parsed_stats
#:id => 26,
expert_results = EvalReference.all(:conditions => { :referencetype => 'expert', :status => '1'})

attrs = get_reference_attrs

tp, fp, tn, fn, weight = {}, {}, {}, {}, {}



attrs.delete('chapter')
attrs.delete('school')
attrs.delete('status')
attrs.delete('howpublished')
attrs.delete('edition')

enriched_results = EvalReference.all(:conditions => { :referencetype => 'enriched'})

attrs.each do |attr|
tp[attr] = 0
fp[attr] = 0
tn[attr] = 0
fn[attr] = 0
weight[attr] = 0
end

enriched_results.each do |enriched|
attrs.each do |attr|
# weight[attr] += 1 if enriched.has_attr(attr)
end
end

attrs.each do |attr|
#weight[attr] = ((weight[attr].to_f/enriched_results.size)*100).round(1)
puts "#{attr}\t\t&#{weight[attr]}"+'\\\\'
end

c = 0
expert_results.each do |expert|
enriched = EvalReference.find(:first, :conditions => {:referencetype => 'enriched', :citation => expert.citation})
parsed = EvalReference.find(:first, :conditions => {:referencetype => 'parsed', :citation => expert.citation})

# expert.id, enriched.id, enriched.to_yaml, expert.to_yaml

attrs.each do |attr|

#weight[attr] += 1
# precision
if enriched.has_attr(attr)
x = attr
#
if enriched[attr] == expert[attr]
# True positive: Sick people correctly diagnosed as sick
# TP: gevonden en hetzelfde als expert

if enriched[attr] == parsed[attr]

#puts attr
next
end

weight[attr] +=1

tp[attr] += 1
else
# False positive: Healthy people incorrectly identified as sick
# FP: gevonden en niet hetzelfde als expert
fp[attr] += 1
end
else

if !expert.has_attr(attr)
#tn: niets gevonden en expert ook niet
tn[attr] += 1
else
#fn: niets gevonden maar expert wel
fn[attr] += 1
end

end

end

c += 1

end


puts "measure:\t\t Precision\t\tRecall\t\tF"

afp, afr, aff = 0, 0, 0
afpw, afrw, affw = 0, 0, 0
weightsum = 0
nanat = []
attrs.each do |attr|

if tp[attr] == 0
precision = 0.0
else
precision = ((tp[attr].to_f/(tp[attr]+fp[attr]))*100).round(1)
end

if tp[attr] == 0
recall = 0.0
else
recall = ((tp[attr].to_f/(tp[attr]+fn[attr]))*100).round(1)
end

if precision == 0 or recall == 0
f = 0.0
else
f = ((2*precision*recall)/(precision+recall)).round(1)
end

# if precision.nan? || recall.nan?
# nanat << attr
# next
# end

# precision = 1 if precision.nan?
# recall = 1 if recall.nan?
# f = 1 if f.nan?

afp += precision
afr += recall
aff += f

afpw += precision *weight[attr]
afrw += recall *weight[attr]
affw += f *weight[attr]
#puts "#{precision} *#{weight[attr]} = #{precision*weight[attr]} == #{afpw}/#{weightsum} = #{(afpw/weightsum)}",
weightsum += weight[attr]
#puts attr
# precision = (precision*100).round(1)
# recall = (recall*100).round(1)
# f = (f*100).round(1)
puts '\\'+"hline "+ '\\' +"textbf{#{attr.capitalize}} & #{precision} & #{recall} & #{f} & #{((weight[attr].to_f/0.61).round(1))}" + '\\\\'
end
afp = ((afp/(attrs.size-nanat.size))).round(1)
afr = ((afr/(attrs.size-nanat.size))).round(1)
aff = ((aff/(attrs.size-nanat.size))).round(1)
puts "#################################"
puts "Totals\t\t #{afp}\t\t#{afr}\t\t#{aff}"

afp = ((afpw/weightsum)).round(1)
afr = ((afrw/weightsum)).round(1)
aff = ((affw/weightsum)).round(1)
puts "#################################"
puts "TotalsWeighted\t\t #{afpw}\t\t#{afrw}\t\t#{affw}"
puts weightsum
puts attrs.size, (attrs.size-nanat.size)
puts nanat.to_yaml


end

def from_bibmix_reference(reference)

attribute_names.each do |attr|
Expand Down Expand Up @@ -308,6 +486,155 @@ def calcstats4
puts nanat.to_yaml
end


def calcstats5

expert_results = EvalReference.all(:conditions => {:referencetype => 'expert', :status => '1'})

attrs = get_reference_attrs

tp, fp, tn, fn = {}, {}, {}, {}



m = {}

c = 0
expert_results.each do |expert|
enriched = EvalReference.find(:first, :conditions => {:referencetype => 'enriched', :citation => expert.citation})

m[enriched.id] = {}
attrs.each do |attr|
m[enriched.id]['tp'] = 0
m[enriched.id]['fp'] = 0
m[enriched.id]['tn'] = 0
m[enriched.id]['fn'] = 0
end

attrs.each do |attr|

if ['annotate', 'chapter', 'edition', 'howpublished', 'school', 'status'].include?(attr)
next
end
# precision
if enriched.has_attr(attr)
if enriched[attr] == expert[attr]
# True positive: Sick people correctly diagnosed as sick
# TP: gevonden en hetzelfde als expert
m[enriched.id]['tp'] += 1
else
# False positive: Healthy people incorrectly identified as sick
# FP: gevonden en niet hetzelfde als expert
m[enriched.id]['fp'] += 1
end
else

if !expert.has_attr(attr)
#tn: niets gevonden en expert ook niet
m[enriched.id]['tn'] += 1
else
#fn: niets gevonden maar expert wel
m[enriched.id]['fn'] += 1
end

end

end

c += 1

end

puts m.to_yaml

r = {}
m.each do |id, h|
key = (h['fp']).to_s
if r.key?(key)
r[key] += 1
else
r[key] = 1
end
end

puts r.to_yaml
end

def calcstats6

expert_results = EvalReference.all(:conditions => {:referencetype => 'expert', :status => '1'})

attrs = get_reference_attrs

tp, fp, tn, fn = {}, {}, {}, {}



m = []

c = 0
expert_results.each do |expert|
enriched = EvalReference.find(:first, :conditions => {:referencetype => 'enriched', :citation => expert.citation})
parsed = EvalReference.find(:first, :conditions => {:referencetype => 'parsed', :citation => expert.citation})

hasattr = 0
intersect = 0
attrs.each do |attr|

if expert.has_attr(attr)
hasattr += 1
if expert[attr] == parsed[attr]
intersect += 1
end
end
end

m << intersect.to_f/hasattr

end

s = m.size
sum = m.sum
puts sum/s.to_f
end

# def calcstats7
#
# expert_results = EvalReference.all(:conditions => {:referencetype => 'expert', :status => '1'})
#
# attrs = get_reference_attrs
# m = {}
#
# attrs.each do |attr|
# m[attr] = 0
# end
# c = 0
# expert_results.each do |expert|
# enriched = EvalReference.find(:first, :conditions => {:referencetype => 'enriched', :citation => expert.citation})
# parsed = EvalReference.find(:first, :conditions => {:referencetype => 'parsed', :citation => expert.citation})
#
# hasattr = 0
# intersect = 0
# attrs.each do |attr|
#
# if enriched.has_attr(attr)
# m[attr] += 1
# if enriched[attr] == parsed[attr]
# intersect += 1
# end
# end
# end
#
# m << intersect.to_f/hasattr
#
# end
#
# s = m.size
# sum = m.sum
# puts m/s.to_f
# end


def has_attr(attr)
!self[attr].nil? && !self[attr].empty?
end
Expand Down
2 changes: 2 additions & 0 deletions app/views/webservice/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Webservice#index</h1>
<p>Find me in app/views/webservice/index.html.erb</p>
9 changes: 9 additions & 0 deletions db/migrate/20100830101109_rename.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Rename < ActiveRecord::Migration
def self.up
rename_column "eval_references", "annotate", "annote"
end

def self.down
rename_column "eval_references", "annote", "annotate"
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100825220621) do
ActiveRecord::Schema.define(:version => 20100830101109) do

create_table "citations", :force => true do |t|
t.text "citation"
Expand All @@ -20,7 +20,7 @@
create_table "eval_references", :force => true do |t|
t.string "referencetype"
t.string "address"
t.text "annotate"
t.text "annote"
t.text "author"
t.text "booktitle"
t.string "chapter"
Expand Down
Loading

0 comments on commit febfa91

Please sign in to comment.