Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Coval Scoring code #10875

Merged
merged 14 commits into from
Jun 22, 2022
33 changes: 33 additions & 0 deletions licenses/3rd_party_licenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,36 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


coval
-----

* Files: scorer.py

The implementations of ClusterEvaluator, lea, get_cluster_info, and
get_markable_assignments are adapted from coval, which is distributed
under the following license:

The MIT License (MIT)

Copyright 2018 Nafise Sadat Moosavi (ns.moosavi at gmail dot com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

124 changes: 0 additions & 124 deletions spacy/coref_scorer.py

This file was deleted.

4 changes: 2 additions & 2 deletions spacy/pipeline/coref.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
doc2clusters,
)

from ..coref_scorer import Evaluator, get_cluster_info, lea
from ..scorer import ClusterEvaluator, get_cluster_info, lea


default_config = """
Expand Down Expand Up @@ -314,7 +314,7 @@ def score(self, examples, **kwargs):
https://api.semanticscholar.org/CorpusID:17606580
"""

evaluator = Evaluator(lea)
evaluator = ClusterEvaluator(lea)

for ex in examples:
p_clusters = doc2clusters(ex.predicted, self.span_cluster_prefix)
Expand Down
141 changes: 141 additions & 0 deletions spacy/scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,3 +1143,144 @@ def _auc(x, y):
# regular numpy.ndarray instances.
area = area.dtype.type(area)
return area


adrianeboyd marked this conversation as resolved.
Show resolved Hide resolved
def get_cluster_info(predicted_clusters, gold_clusters):
p2g = get_markable_assignments(predicted_clusters, gold_clusters)
g2p = get_markable_assignments(gold_clusters, predicted_clusters)
# this is the data format used as input by the evaluator
return (gold_clusters, predicted_clusters, g2p, p2g)


def get_markable_assignments(in_clusters, out_clusters):
markable_cluster_ids = {}
out_dic = {}
for cluster_id, cluster in enumerate(out_clusters):
for m in cluster:
out_dic[m] = cluster_id

for cluster in in_clusters:
for im in cluster:
for om in out_dic:
if im == om:
markable_cluster_ids[im] = out_dic[om]
break

return markable_cluster_ids


class ClusterEvaluator:
def __init__(self, metric, beta=1, keep_aggregated_values=False):
self.p_num = 0
self.p_den = 0
self.r_num = 0
self.r_den = 0
self.metric = metric
self.beta = beta
self.keep_aggregated_values = keep_aggregated_values

if keep_aggregated_values:
self.aggregated_p_num = []
self.aggregated_p_den = []
self.aggregated_r_num = []
self.aggregated_r_den = []

def update(self, coref_info):
(
key_clusters,
sys_clusters,
key_mention_sys_cluster,
sys_mention_key_cluster,
) = coref_info

pn, pd = self.metric(sys_clusters, key_clusters, sys_mention_key_cluster)
rn, rd = self.metric(key_clusters, sys_clusters, key_mention_sys_cluster)
self.p_num += pn
self.p_den += pd
self.r_num += rn
self.r_den += rd

if self.keep_aggregated_values:
self.aggregated_p_num.append(pn)
self.aggregated_p_den.append(pd)
self.aggregated_r_num.append(rn)
self.aggregated_r_den.append(rd)

def f1(self, p_num, p_den, r_num, r_den, beta=1):
p = 0
if p_den != 0:
p = p_num / float(p_den)
r = 0
if r_den != 0:
r = r_num / float(r_den)

if p + r == 0:
return 0

return (1 + beta * beta) * p * r / (beta * beta * p + r)

def get_f1(self):
return self.f1(self.p_num, self.p_den, self.r_num, self.r_den, beta=self.beta)

def get_recall(self):
if self.r_num == 0:
return 0

return self.r_num / float(self.r_den)

def get_precision(self):
if self.p_num == 0:
return 0

return self.p_num / float(self.p_den)

def get_prf(self):
return self.get_precision(), self.get_recall(), self.get_f1()

def get_counts(self):
return self.p_num, self.p_den, self.r_num, self.r_den

def get_aggregated_values(self):
return (
self.aggregated_p_num,
self.aggregated_p_den,
self.aggregated_r_num,
self.aggregated_r_den,
)


def lea(input_clusters, output_clusters, mention_to_gold):
"""
LEA is a metric for scoring coref clusters design to avoid pitfals of prior
methods. Proposed in Moosavi and Strube 2016.

https://api.semanticscholar.org/CorpusID:17606580
"""
num, den = 0, 0

for c in input_clusters:
if len(c) == 1:
all_links = 1
if (
c[0] in mention_to_gold
and len(output_clusters[mention_to_gold[c[0]]]) == 1
):
common_links = 1
else:
common_links = 0
else:
common_links = 0
all_links = len(c) * (len(c) - 1) / 2.0
for i, m in enumerate(c):
if m in mention_to_gold:
for m2 in c[i + 1 :]:
if (
m2 in mention_to_gold
and mention_to_gold[m] == mention_to_gold[m2]
):
common_links += 1

num += len(c) * common_links / float(all_links)
den += len(c)

return num, den