Skip to content

Latest commit

 

History

History
139 lines (107 loc) · 5.33 KB

README_zh.md

File metadata and controls

139 lines (107 loc) · 5.33 KB

中文 | EN

causal-strength 因果强度 衡量因果关系的强度

ACL Anthology PyPI version Hugging Face Model

causal-strength 是一个用于评估陈述之间因果强度的 Python 包,使用如 CESAR(带注意力重加权的因果嵌入相似性)等多种指标。该包利用 Hugging Face Transformers 上的预训练模型,提供高效和可扩展的计算。

目录

📜 引用 引用

如果你觉得这个包有帮助,请给我们的仓库 causal-strength 和相关仓库 defeasibility-in-causality 点星。如果用于学术目的,请引用我们的论文:

@inproceedings{cui-etal-2024-exploring,
    title = "Exploring Defeasibility in Causal Reasoning",
    author = "Cui, Shaobo  and
      Milikic, Lazar  and
      Feng, Yiyang  and
      Ismayilzada, Mete  and
      Paul, Debjit  and
      Bosselut, Antoine  and
      Faltings, Boi",
    booktitle = "Findings of the Association for Computational Linguistics ACL 2024",
    month = aug,
    year = "2024",
    address = "Bangkok, Thailand and virtual meeting",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2024.findings-acl.384",
    doi = "10.18653/v1/2024.findings-acl.384",
    pages = "6433--6452",
}

🛠️ 使用方法 Usage

评估因果强度

evaluate 函数用于计算两个陈述之间的因果强度。

  1. 使用 CESAR 模型:

    from causalstrength import evaluate
    
    # Test CESAR Model
    s1_cesar = "Tom is very hungry now."
    s2_cesar = "He goes to McDonald for some food."
    
    print("Testing CESAR model:")
    cesar_score = evaluate(s1_cesar, s2_cesar, model_name='CESAR', model_path='shaobocui/cesar-bert-large')
    print(f"CESAR Causal strength between \"{s1_cesar}\" and \"{s2_cesar}\": {cesar_score:.4f}")

    输出如下:

    Testing CESAR model:
    CESAR Causal strength between "Tom is very hungry now." and "He goes to McDonald for some food.": 0.4482
    
  2. 使用 CEQ 模型:

     from causalstrength import evaluate
    
     # Test CEQ Model
     s1_ceq = "Tom is very hungry now."
     s2_ceq = "He goes to McDonald for some food."
     
     print("\nTesting CEQ model:")
     ceq_score = evaluate(s1_ceq, s2_ceq, model_name='CEQ')
     print(f"CEQ Causal strength between \"{s1_ceq}\" and \"{s2_ceq}\": {ceq_score:.4f}")

    输出如下:

    Testing CEQ model:
    CEQ Causal strength between "Tom is very hungry now." and "He goes to McDonald for some food.": 0.0168
    

参数说明:

  • s1 (str): 原因陈述。
  • s2 (str): 结果陈述。
  • model_name (str): 使用的模型名称(如 'CESAR', 'CEQ' 等)。
  • model_path (str): Hugging Face 模型标识符或模型的本地路径。

生成因果热力图

使用热力图可视化 word-level 的 因果强度:

from causalstrength import evaluate

# Test CESAR Model
s1_cesar = "Fire starts quickly."
s2_cesar = "House burns to ashes."

print("Testing CESAR model:")
cesar_score = evaluate(s1_cesar, s2_cesar, model_name='CESAR', model_path='shaobocui/cesar-bert-large',
                       plot_heatmap_flag=True, heatmap_path=f'./figures/causal_heatmap.png')

输出如下:

Testing CESAR model:
The causal heatmap is saved to ./figures/causal_heatmap.png

The causal heatmap is as follows: Example Image

📚 参考文献 References

  1. Cui, Shaobo, et al. "Exploring Defeasibility in Causal Reasoning." Findings of the Association for Computational Linguistics ACL 2024. 2024.
  2. Du, Li, et al. "e-CARE: a New Dataset for Exploring Explainable Causal Reasoning." Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). 2022.