from io import StringIO
import pandas
import requests
from . import SimpleScorer, BayesianScorer


def get_eidos_bayesian_scorer():
    """Return a BayesianScorer based on Eidos curation counts."""
    url = 'https://raw.githubusercontent.com/clulab/eidos/master/' + \
        'src/main/resources/org/clulab/wm/eidos/english/confidence/' + \
        'rule_summary.tsv'
    # Load the table of scores from the URL above into a data frame
    res = StringIO(requests.get(url).text)
    table = pandas.read_table(res, sep='\t')
    # Drop the last "Grant total" row
    table = table.drop(table.index[len(table)-1])

    subtype_counts = {'eidos': {r: [c, i] for r, c, i in
                              zip(table['RULE'], table['Num correct'],
                                  table['Num incorrect'])}}

    scorer = BayesianScorer(prior_counts={}, subtype_counts=subtype_counts)
    return scorer


def get_eidos_scorer():
    """Return a SimpleScorer based on Eidos curated precision estimates."""
    url = 'https://raw.githubusercontent.com/clulab/eidos/master/' + \
        'src/main/resources/org/clulab/wm/eidos/english/confidence/' + \
        'rule_summary.tsv'

    # Load the table of scores from the URL above into a data frame
    res = StringIO(requests.get(url).text)
    table = pandas.read_table(res, sep='\t')
    # Drop the last "Grant total" row
    table = table.drop(table.index[len(table)-1])

    # Get the overall precision
    total_num = table['COUNT of RULE'].sum()
    weighted_sum = table['COUNT of RULE'].dot(table['% correct'])
    precision = weighted_sum / total_num
    # We have to divide this into a random and systematic component, for now
    # in an ad-hoc manner
    syst_error = 0.05
    rand_error = 1 - precision - syst_error
    prior_probs = {'rand': {'eidos': rand_error}, 'syst': {'eidos': syst_error}}

    # Get a dict of rule-specific errors.
    subtype_probs = {'eidos':
                     {k: 1.0-min(v, 0.95)-syst_error for k, v
                      in zip(table['RULE'], table['% correct'])}}
    print(subtype_probs)
    scorer = SimpleScorer(prior_probs, subtype_probs)
    return scorer

@@ -100,33 +100,33 @@
 def 
-get
+load
 _eidos_
-bayesian_scorer
+curation_table
 ():%0A
@@ -145,31 +145,23 @@
 n a 
-BayesianScorer based on
+pandas table of
  Eid
@@ -172,22 +172,20 @@
 uration 
-counts
+data
 .%22%22%22%0A   
@@ -592,24 +592,181 @@
 (table)-1%5D)%0A
+    return table%0A%0A%0Adef get_eidos_bayesian_scorer():%0A    %22%22%22Return a BayesianScorer based on Eidos curation counts.%22%22%22%0A    table = load_eidos_curation_table()
 %0A    subtype
@@ -1147,418 +1147,42 @@
     
-url = 'https://raw.githubusercontent.com/clulab/eidos/master/' + %5C%0A        'src/main/resources/org/clulab/wm/eidos/english/confidence/' + %5C%0A        'rule_summary.tsv'%0A%0A    # Load the table of scores from the URL above into a data frame%0A    res = StringIO(requests.get(url).text)%0A    table = pandas.read_table(res, sep='%5Ct')%0A    # Drop the last %22Grant total%22 row%0A    table = table.drop(table.index%5Blen(table)-1%5D
+table = load_eidos_curation_table(
 )%0A%0A 

Refactor to simplify WM scorer construction