|
| 1 | +nereval |
| 2 | +======= |
| 3 | +.. image:: https://travis-ci.org/jantrienes/nereval.svg?branch=master |
| 4 | + :target: https://travis-ci.org/jantrienes/nereval |
| 5 | + |
| 6 | +Evaluation script for named entity recognition (NER) systems based on entity-level F1 score. |
| 7 | + |
| 8 | +Definition |
| 9 | +---------- |
| 10 | +The metric as implemented here has been described by Nadeau and Sekine (2007) and was widely used as part of the Message Understanding Conferences (Grishman and Sundheim, 1996). It evaluates an NER system according to two axes: whether it is able to assign the right type to an entity, and whether it finds the exact entity boundaries. For both axes, the number of correct predictions (COR), the number of actual predictions (ACT) and the number of possible predictions (POS) are computed. From these statistics, precision and recall can be derived: |
| 11 | + |
| 12 | +:: |
| 13 | + |
| 14 | + precision = COR/ACT |
| 15 | + recall = COR/POS |
| 16 | + |
| 17 | + |
| 18 | +The final score is the micro-averaged F1 measure of precision and recall of both type and boundary axes. |
| 19 | + |
| 20 | +Installation |
| 21 | +------------ |
| 22 | +.. code-block:: bash |
| 23 | +
|
| 24 | + pip install nereval |
| 25 | +
|
| 26 | +
|
| 27 | +Usage |
| 28 | +----- |
| 29 | +The script can either be used from within Python or from the command line when classification results have been written to a JSON file. |
| 30 | + |
| 31 | +Usage from Command Line |
| 32 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 33 | +Assume we have the following classification results in ``input.json``: |
| 34 | + |
| 35 | +.. code-block:: json |
| 36 | +
|
| 37 | + [ |
| 38 | + { |
| 39 | + "text": "CILINDRISCHE PLUG", |
| 40 | + "true": [ |
| 41 | + { |
| 42 | + "text": "CILINDRISCHE PLUG", |
| 43 | + "type": "Productname", |
| 44 | + "start": 0 |
| 45 | + } |
| 46 | + ], |
| 47 | + "predicted": [ |
| 48 | + { |
| 49 | + "text": "CILINDRISCHE", |
| 50 | + "type": "Productname", |
| 51 | + "start": 0 |
| 52 | + }, |
| 53 | + { |
| 54 | + "text": "PLUG", |
| 55 | + "type": "Productname", |
| 56 | + "start": 13 |
| 57 | + } |
| 58 | + ] |
| 59 | + } |
| 60 | + ] |
| 61 | +
|
| 62 | +
|
| 63 | +Then the script can be executed as follows: |
| 64 | + |
| 65 | +.. code-block:: bash |
| 66 | +
|
| 67 | + python nereval.py input.json |
| 68 | + F1-score: 0.33 |
| 69 | +
|
| 70 | +
|
| 71 | +Usage from Python |
| 72 | +~~~~~~~~~~~~~~~~~ |
| 73 | +Alternatively, the evaluation metric can be directly invoked from within python. Example: |
| 74 | + |
| 75 | +.. code-block:: python |
| 76 | +
|
| 77 | + import nereval |
| 78 | + from nereval import Entity |
| 79 | +
|
| 80 | + # Ground-truth: |
| 81 | + # CILINDRISCHE PLUG |
| 82 | + # B_PROD I_PROD |
| 83 | + y_true = [ |
| 84 | + Entity('CILINDRISCHE PLUG', 'Productname', 0) |
| 85 | + ] |
| 86 | +
|
| 87 | + # Prediction: |
| 88 | + # CILINDRISCHE PLUG |
| 89 | + # B_PROD B_PROD |
| 90 | + y_pred = [ |
| 91 | + # correct type, wrong text |
| 92 | + Entity('CILINDRISCHE', 'Productname', 0), |
| 93 | + # correct type, wrong text |
| 94 | + Entity('PLUG', 'Productname', 13) |
| 95 | + ] |
| 96 | +
|
| 97 | + score = nereval.evaluate([y_true], [y_pred]) |
| 98 | + print('F1-score: %.2f' % score) |
| 99 | + F1-score: 0.33 |
| 100 | +
|
| 101 | +
|
| 102 | +Note on Symmetry |
| 103 | +---------------- |
| 104 | +The metric itself is not symmetric due to the inherent problem of word overlaps in NER. So ``evaluate(y_true, y_pred) != evaluate(y_pred, y_true)``. This comes apparent if we consider the following example (tagger uses an BIO scheme): |
| 105 | + |
| 106 | +.. code-block:: bash |
| 107 | +
|
| 108 | + # Example 1: |
| 109 | + Input: CILINDRISCHE PLUG DIN908 M10X1 Foo |
| 110 | + Truth: B_PROD I_PROD B_PROD B_DIM O |
| 111 | + Predicted: B_PROD B_PROD B_PROD B_PROD B_PROD |
| 112 | +
|
| 113 | + Correct Text: 2 |
| 114 | + Correct Type: 2 |
| 115 | +
|
| 116 | + # Example 2 (inversed): |
| 117 | + Input: CILINDRISCHE PLUG DIN908 M10X1 Foo |
| 118 | + Truth: B_PROD B_PROD B_PROD B_PROD B_PROD |
| 119 | + Predicted: B_PROD I_PROD B_PROD B_DIM O |
| 120 | +
|
| 121 | + Correct Text: 2 |
| 122 | + Correct Type: 3 |
| 123 | +
|
| 124 | +
|
| 125 | +Notes and References |
| 126 | +-------------------- |
| 127 | +Used in a student research project on natural language processing at `University of Twente, Netherlands <https://www.utwente.nl>`_. |
| 128 | + |
| 129 | +**References** |
| 130 | + |
| 131 | +* Grishman, R., & Sundheim, B. (1996). `Message understanding conference-6: A brief history <http://www.aclweb.org/anthology/C96-1079>`_. *In COLING 1996 Volume 1: The 16th International Conference on Computational Linguistics* (Vol. 1). |
| 132 | +* Nadeau, D., & Sekine, S. (2007). `A survey of named entity recognition and classification <http://www.jbe-platform.com/content/journals/10.1075/li.30.1.03nad>`_. *Lingvisticae Investigationes*, 30(1), 3-26. |
0 commit comments