#!/usr/bin/env ruby

require 'prtable'
require 'NaiveBayes'
require 'trollop'

opts = Trollop::options do
  opt :delim, "type of filter", :type=>:string, :default=>nil
  opt :suppress, "suppress output and print senses", :type=>:boolean, :default=>true
  opt :filters, "filter by senses", :type=>:strings, :default=>nil
  opt :cluster, "print clusters of words", :type=>:boolean, :default=>false
end


delim = opts[:delim]
suppress = opts[:suppress_given]
filt = opts[:filters] || []
clusters = opts[:cluster_given]

def pss(senses)
  c = 0
  senses.each { |s|
	  c+=1 if senses.any? { |a| (s!=a) && (TripsNode.isparent(a,s))}
  }
  return c
end

tg = EntTreeTagger.new(["wordsense"], ".prt-definitions")

wrds = Counter.get_all_words
wrds.sort_by! { |w| 0-tg.unict(tg.f_word(w), "wordsense")}

sct = Hash.new { |h,k| h[k] = Hash.new(0) }

def getinv(wrds)
  inv = Hash.new {|h,k| h[k] = []}
  wrds.each { |w|
	sn = Counter.get_definitions(w).select{|f| true}.map{|t| t.downcase}
	s =sn.sort.join(",")
	inv[s] << w
  }
  inv.each { |s, k|
	  next if k.size < 2
	  sn = s.split(",")
	  k.map! {|j| "'#{j}'"}
	  printf("{\n\tsenses: [%s],\n\twords: [%s],\n\t'lcs': '%s'\n}\n", sn.map{|t| "'#{t}'"}.join(", "), k.join(", "), Trips.lcs(*sn))
  }
end

getinv(wrds) if clusters
exit if clusters


wrds.each { |w|
	s = Counter.get_definitions(w).select{|f| true}.sort
	next if (filt.size!=0) && !(filt.any? {|k| s.include? k})
	c = pss(s)
	type="ind"
	type="tss" if (c==s.size-1) && (s.size>1)
	type="pss" if (c>0) && (c<s.size-1)

	s.each { |t|
		sct[t][type]+=1
	}

	next if suppress

	next if (delim!=nil) && (type!=delim)

	printf("#%s\t%d\n", w, tg.unict(tg.f_word(w), "wordsense"))
	printf("%s\n",type) 
	printf("%s\n", s.join(", ").downcase)
	u = tg.unique_parents(s)
	u.each { |star,sense| printf("\t%s=>%s\n", star, sense) }
	lcs = Trips.lcs(*s)

	printf("lcs:\t%s\n", lcs)
	printf("=============\n")
}


ks = sct.keys.sort_by { |k|
	ct = 0
	sct[k].each { |h, c|
		ct -= c
	}
	ct

}

ks.each { |k|
	printf("%s\t", k)
	sct[k].keys.each {|m| printf("%s:%d\t", m, sct[k][m])}
	printf("\n")
} if suppress
