Giter Club home page Giter Club logo

factsumm's Introduction

FactSumm: Factual Consistency Scorer for Abstractive Summarization

GitHub release Apache 2.0 Issues

FactSumm is a toolkit that scores Factualy Consistency for Abstract Summarization

Without fine-tuning, you can simply apply a variety of downstream tasks to both the source article and the generated abstractive summary

For example, by extracting fact triples from source articles and generated summaries, you can verify that generated summaries correctly reflect source-based facts ( See image above )


Installation

With and Python 3 (>= 3.8), you can install factsumm simply using pip:

pip install factsumm

Or you can install FactSumm from source repository:

git clone https://github.com/huffon/factsumm
cd factsumm
pip install .

Usage

>>> from factsumm import FactSumm
>>> factsumm = FactSumm()
>>> article = "Lionel Andrés Messi (born 24 June 1987) is an Argentine professional footballer who plays as a forward and captains both Spanish club Barcelona and the Argentina national team. Often considered as the best player in the world and widely regarded as one of the greatest players of all time, Messi has won a record six Ballon d'Or awards, a record six European Golden Shoes, and in 2020 was named to the Ballon d'Or Dream Team."
>>> summary = "Lionel Andrés Messi (born 24 Aug 1997) is an Spanish professional footballer who plays as a forward and captains both Spanish club Barcelona and the Spanish national team."
>>> factsumm(article, summary, verbose=True)
<Source Entities>
Line No.1: [[('Lionel Andrés Messi', 'PERSON'), ('24 June 1987', 'DATE'), ('Argentine', 'NORP'), ('Spanish', 'NORP'), ('Barcelona', 'ORG'), ('Argentina', 'GPE')]]
Line No.2: [[('one', 'CARDINAL'), ('Messi', 'PERSON'), ('six', 'CARDINAL'), ("Ballon d'Or", 'WORK_OF_ART'), ('European Golden Shoes', 'WORK_OF_ART'), ('2020', 'DATE'), ("Ballon d'Or Dream Team", 'WORK_OF_ART')]]

<Summary Entities>
Line No.1: [[('Lionel Andrés Messi', 'PERSON'), ('24 Aug 1997', 'DATE'), ('Spanish', 'NORP'), ('Barcelona', 'ORG')]]

<Source Facts>
('Lionel Andrés Messi', 'per:origin', 'Argentine')
('Lionel Andrés Messi', 'per:employee_of', 'Barcelona')
('Barcelona', 'org:country_of_headquarters', 'Spanish')
('Lionel Andrés Messi', 'per:date_of_birth', '24 June 1987')
('Lionel Andrés Messi', 'per:countries_of_residence', 'Argentina')
('Spanish', 'org:top_members/employees', 'Lionel Andrés Messi')
('Barcelona', 'org:top_members/employees', 'Lionel Andrés Messi')

<Summary Facts>
('Lionel Andrés Messi', 'per:employee_of', 'Barcelona')
('Lionel Andrés Messi', 'per:date_of_birth', '24 Aug 1997')
('Barcelona', 'org:country_of_headquarters', 'Spanish')
('Spanish', 'org:top_members/employees', 'Lionel Andrés Messi')
('Barcelona', 'org:top_members/employees', 'Lionel Andrés Messi')
('Lionel Andrés Messi', 'per:origin', 'Spanish')
('Lionel Andrés Messi', 'per:countries_of_residence', 'Spanish')

<Common Facts>
('Barcelona', 'org:top_members/employees', 'Lionel Andrés Messi')
('Lionel Andrés Messi', 'per:employee_of', 'Barcelona')
('Barcelona', 'org:country_of_headquarters', 'Spanish')
('Spanish', 'org:top_members/employees', 'Lionel Andrés Messi')

<Diff Facts>
('Lionel Andrés Messi', 'per:date_of_birth', '24 Aug 1997')
('Lionel Andrés Messi', 'per:origin', 'Spanish')
('Lionel Andrés Messi', 'per:countries_of_residence', 'Spanish')

Fact Score: 0.5714285714285714
Answers based on Source (Questions are generated from Summary)
[Q] Who is the captain of the Spanish national team?	[Pred] <unanswerable>
[Q] When was Lionel Andrés Messi born?	[Pred] 24 June 1987
[Q] Lionel Andrés Messi is a professional footballer of what nationality?	[Pred] Argentine
[Q] Lionel Messi is a captain of which Spanish club?	[Pred] Barcelona

Answers based on Summary (Questions are generated from Summary)
[Q] Who is the captain of the Spanish national team?	[Pred] Lionel Andrés Messi
[Q] When was Lionel Andrés Messi born?	[Pred] 24 Aug 1997
[Q] Lionel Andrés Messi is a professional footballer of what nationality?	[Pred] Spanish
[Q] Lionel Messi is a captain of which Spanish club?	[Pred] Barcelona

QAGS Score: 0.3333333333333333

Avg. ROUGE-1: 0.4415584415584415
Avg. ROUGE-2: 0.3287671232876712
Avg. ROUGE-L: 0.4415584415584415
<BERTScore Score>
Precision: 0.9760397672653198
Recall: 0.9778039455413818
F1: 0.9769210815429688

You can use the GPU with the device. If you want to use GPU, pass cuda (default is cpu)

>>> factsumm(article, summary, device="cuda")

Sub-modules

From here, you can find various way to score Factual Consistency level with Unsupervised methods


Triple-based Module ( closed-scheme )

>>> from factsumm import FactSumm
>>> factsumm = FactSumm()
>>> factsumm.extract_facts(article, summary, verbose=True)
<Source Entities>
Line No.1: [[('Lionel Andrés Messi', 'PERSON'), ('24 June 1987', 'DATE'), ('Argentine', 'NORP'), ('Spanish', 'NORP'), ('Barcelona', 'ORG'), ('Argentina', 'GPE')]]
Line No.2: [[('one', 'CARDINAL'), ('Messi', 'PERSON'), ('six', 'CARDINAL'), ("Ballon d'Or", 'WORK_OF_ART'), ('European Golden Shoes', 'WORK_OF_ART'), ('2020', 'DATE'), ("Ballon d'Or Dream Team", 'WORK_OF_ART')]]

<Summary Entities>
Line No.1: [[('Lionel Andrés Messi', 'PERSON'), ('24 Aug 1997', 'DATE'), ('Spanish', 'NORP'), ('Barcelona', 'ORG')]]

<Source Facts>
('Lionel Andrés Messi', 'per:origin', 'Argentine')
('Lionel Andrés Messi', 'per:employee_of', 'Barcelona')
('Barcelona', 'org:country_of_headquarters', 'Spanish')
('Lionel Andrés Messi', 'per:date_of_birth', '24 June 1987')
('Lionel Andrés Messi', 'per:countries_of_residence', 'Argentina')
('Spanish', 'org:top_members/employees', 'Lionel Andrés Messi')
('Barcelona', 'org:top_members/employees', 'Lionel Andrés Messi')

<Summary Facts>
('Lionel Andrés Messi', 'per:employee_of', 'Barcelona')
('Lionel Andrés Messi', 'per:date_of_birth', '24 Aug 1997')
('Barcelona', 'org:country_of_headquarters', 'Spanish')
('Spanish', 'org:top_members/employees', 'Lionel Andrés Messi')
('Barcelona', 'org:top_members/employees', 'Lionel Andrés Messi')
('Lionel Andrés Messi', 'per:origin', 'Spanish')
('Lionel Andrés Messi', 'per:countries_of_residence', 'Spanish')

<Common Facts>
('Barcelona', 'org:top_members/employees', 'Lionel Andrés Messi')
('Lionel Andrés Messi', 'per:employee_of', 'Barcelona')
('Barcelona', 'org:country_of_headquarters', 'Spanish')
('Spanish', 'org:top_members/employees', 'Lionel Andrés Messi')

<Diff Facts>
('Lionel Andrés Messi', 'per:date_of_birth', '24 Aug 1997')
('Lionel Andrés Messi', 'per:origin', 'Spanish')
('Lionel Andrés Messi', 'per:countries_of_residence', 'Spanish')

Fact Score: 0.5714285714285714

The triple-based module counts the overlap of fact triples between the generated summary and the source document.


QA-based Module

If you ask questions about the summary and the source document, you will get a similar answer if the summary realistically matches the source document

>>> from factsumm import FactSumm
>>> factsumm = FactSumm()
>>> factsumm.extract_qas(article, summary, verbose=True)
Answers based on Source (Questions are generated from Summary)
[Q] Who is the captain of the Spanish national team?	[Pred] <unanswerable>
[Q] When was Lionel Andrés Messi born?	[Pred] 24 June 1987
[Q] Lionel Andrés Messi is a professional footballer of what nationality?	[Pred] Argentine
[Q] Lionel Messi is a captain of which Spanish club?	[Pred] Barcelona

Answers based on Summary (Questions are generated from Summary)
[Q] Who is the captain of the Spanish national team?	[Pred] Lionel Andrés Messi
[Q] When was Lionel Andrés Messi born?	[Pred] 24 Aug 1997
[Q] Lionel Andrés Messi is a professional footballer of what nationality?	[Pred] Spanish
[Q] Lionel Messi is a captain of which Spanish club?	[Pred] Barcelona

QAGS Score: 0.3333333333333333

ROUGE-based Module

>>> from factsumm import FactSumm
>>> factsumm = FactSumm()
>>> factsumm.calculate_rouge(article, summary)
Avg. ROUGE-1: 0.4415584415584415
Avg. ROUGE-2: 0.3287671232876712
Avg. ROUGE-L: 0.4415584415584415

Simple but effective word-level overlap ROUGE score


BERTScore Module

>>> from factsumm import FactSumm
>>> factsumm = FactSumm()
>>> factsumm.calculate_bert_score(article, summary)
<BERTScore Score>
Precision: 0.9760397672653198
Recall: 0.9778039455413818
F1: 0.9769210815429688

BERTScore can be used to calculate the similarity between each source sentence and the summary sentence


Citation

If you apply this library to any project, please cite:

@misc{factsumm,
  author       = {Heo, Hoon},
  title        = {FactSumm: Factual Consistency Scorer for Abstractive Summarization},
  howpublished = {\url{https://github.com/Huffon/factsumm}},
  year         = {2021},
}

References

factsumm's People

Contributors

huffon avatar karter-liner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

factsumm's Issues

BUG: AttributeError: 'str' object has no attribute 'generate'

when I use the example in README to gain qags score, there has a problem:

AttributeError Traceback (most recent call last)
in ()
----> 1 factsumm.extract_qas(article, summary, verbose=True)

~/Desktop/factsumm-master/factsumm/factsumm.py in extract_qas(self, source, summary, source_ents, summary_ents, verbose, device)
292 summary_ents = self.ner(summary_lines)
293
--> 294 summary_qas = self.qg(summary_lines, summary_ents)
295
296 source_answers = self.qa(source, summary_qas)

~/Desktop/factsumm-master/factsumm/utils/module_question.py in generate_question(sentences, total_entities)
55 ).to(device)
56
---> 57 outputs = model.generate(**tokens, max_length=64)
58
59 question = tokenizer.decode(outputs[0])

AttributeError: 'str' object has no attribute 'generate'

hope you can help me to solve this problem. Thanks!!

Hit Error while using this toolkits

Loading Named Entity Recognition Pipeline...
Loading Relation Extraction Pipeline...
Fact Score: 0.5714285714285714
Loading Question Generation Pipeline...
Loading Question Answering Pipeline...
Traceback (most recent call last):
File "testcase.py", line 5, in
print(factsumm(article, summary, verbose=False))
File "/usr/local/lib/python3.8/dist-packages/factsumm/init.py", line 366, in call
qags_score = self.extract_qas(
File "/usr/local/lib/python3.8/dist-packages/factsumm/init.py", line 263, in extract_qas
source_answers = self.qa(source, summary_qas)
File "/usr/local/lib/python3.8/dist-packages/factsumm/utils/level_sentence.py", line 100, in answer_question
pred = qa(
File "/usr/local/lib/python3.8/dist-packages/transformers/pipelines/question_answering.py", line 248, in call
return super().call(examples[0], **kwargs)
File "/usr/local/lib/python3.8/dist-packages/transformers/pipelines/base.py", line 915, in call
return self.run_single(inputs, preprocess_params, forward_params, postprocess_params)
File "/usr/local/lib/python3.8/dist-packages/transformers/pipelines/base.py", line 923, in run_single
outputs = self.postprocess(model_outputs, **postprocess_params)
File "/usr/local/lib/python3.8/dist-packages/transformers/pipelines/question_answering.py", line 409, in postprocess
min_null_score = min(min_null_score, (start_[0] * end_[0]).item())
ValueError: can only convert an array of size 1 to a Python scalar

while using provided example in README, I meet the Error above
( I use pip install to install this packet and create the python file, copy the example code and run )
pip uninstall and pip reinstall doesn`t help QAQ
any suggestion are greatly appreciated!

Missing entities

I'm unable to reproduce the example on the README due to an error on the back-end with an entities key in the data. I installed via pip.

from factsumm import FactSumm
factsumm = FactSumm()
article = "Lionel Andrés Messi (born 24 June 1987) is an Argentine professional footballer who plays as a forward and captains both Spanish club Barcelona and the Argentina national team. Often considered as the best player in the world and widely regarded as one of the greatest players of all time, Messi has won a record six Ballon d'Or awards, a record six European Golden Shoes, and in 2020 was named to the Ballon d'Or Dream Team."
summary = "Lionel Andrés Messi (born 24 Aug 1997) is an Spanish professional footballer who plays as a forward and captains both Spanish club Barcelona and the Spanish national team."
factsumm(article, summary, verbose=True)

Error:

Loading Named Entity Recognition Pipeline...
Loading Relation Extraction Pipeline...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/miniconda3/envs/py3/lib/python3.8/site-packages/factsumm/__init__.py", line 360, in __call__
    source_ents, summary_ents, fact_score = self.extract_facts(
  File "/home/miniconda3/envs/py3/lib/python3.8/site-packages/factsumm/__init__.py", line 182, in extract_facts
    source_ents = self.ner(source_lines)
  File "/home/miniconda3/envs/py3/lib/python3.8/site-packages/factsumm/utils/level_entity.py", line 38, in extract_entities
    for entity in line_result["entities"]:
KeyError: 'entities'

IndexError: index out of range in self

In example, when I extend the length of the article and summary , I get this error.

/opt/anaconda3/envs/LDA0115/lib/python3.6/site-packages/torch/nn/modules/sparse.py in forward(self, input)
124 return F.embedding(
125 input, self.weight, self.padding_idx, self.max_norm,
--> 126 self.norm_type, self.scale_grad_by_freq, self.sparse)
127
128 def extra_repr(self) -> str:

/opt/anaconda3/envs/LDA0115/lib/python3.6/site-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
1850 # remove once script supports set_grad_enabled
1851 no_grad_embedding_renorm(weight, input, max_norm, norm_type)
-> 1852 return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
1853
1854

IndexError: index out of range in self

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.