Spaces:
Sleeping
Sleeping
Commit ·
49960a1
1
Parent(s): aa5d438
Replace Jieba with PyCantonese as Tokenizer
Browse files- chinesebleu.py +16 -19
chinesebleu.py
CHANGED
|
@@ -11,13 +11,14 @@
|
|
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
# See the License for the specific language governing permissions and
|
| 13 |
# limitations under the License.
|
| 14 |
-
"""
|
| 15 |
|
| 16 |
import evaluate
|
| 17 |
import datasets
|
| 18 |
import math
|
| 19 |
from collections import Counter
|
| 20 |
-
import jieba_fast as jieba
|
|
|
|
| 21 |
|
| 22 |
# TODO: Add BibTeX citation
|
| 23 |
#_CITATION = """\
|
|
@@ -31,7 +32,7 @@ _CITATION = ""
|
|
| 31 |
|
| 32 |
# TODO: Add description of the module here
|
| 33 |
_DESCRIPTION = """\
|
| 34 |
-
This evaluation metric is tailor-made to evaluate the translation quality of Chinese translation.
|
| 35 |
"""
|
| 36 |
|
| 37 |
|
|
@@ -57,37 +58,26 @@ Examples:
|
|
| 57 |
{'score': 71.89393375176813, 'counts': [9, 7, 5, 4], 'totals': [9, 8, 7, 6], 'bp': 1.0}
|
| 58 |
"""
|
| 59 |
|
| 60 |
-
# TODO: Define external resources urls if needed
|
| 61 |
-
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
|
| 62 |
-
|
| 63 |
-
|
| 64 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
| 65 |
class ChineseBLEU(evaluate.Metric):
|
| 66 |
-
"""TODO:
|
| 67 |
|
| 68 |
def _info(self):
|
| 69 |
-
# TODO: Specifies the evaluate.EvaluationModuleInfo object
|
| 70 |
return evaluate.MetricInfo(
|
| 71 |
-
# This is the description that will appear on the modules page.
|
| 72 |
module_type="metric",
|
| 73 |
description=_DESCRIPTION,
|
| 74 |
citation=_CITATION,
|
| 75 |
inputs_description=_KWARGS_DESCRIPTION,
|
| 76 |
-
# This defines the format of each prediction and reference
|
| 77 |
features=datasets.Features({
|
| 78 |
'predictions': datasets.Value('string'),
|
| 79 |
'references': datasets.Value('string'),
|
| 80 |
}),
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Additional links to the codebase or references
|
| 84 |
-
codebase_urls=["https://github.com/shivanraptor/chinesebleu"],
|
| 85 |
-
reference_urls=["http://path.to.reference.url/new_module"]
|
| 86 |
)
|
| 87 |
|
| 88 |
def _download_and_prepare(self, dl_manager):
|
| 89 |
-
"""
|
| 90 |
-
# TODO: Download external resources if needed
|
| 91 |
pass
|
| 92 |
|
| 93 |
def _tokenize_chinese(self, sentence, tokenizer='char'):
|
|
@@ -102,7 +92,8 @@ class ChineseBLEU(evaluate.Metric):
|
|
| 102 |
list: List of tokens.
|
| 103 |
"""
|
| 104 |
if tokenizer == 'chinese':
|
| 105 |
-
return list(jieba.cut(sentence, cut_all=False))
|
|
|
|
| 106 |
else:
|
| 107 |
return list(sentence) # Character-level tokenization
|
| 108 |
|
|
@@ -146,6 +137,12 @@ class ChineseBLEU(evaluate.Metric):
|
|
| 146 |
|
| 147 |
pred_tokens = [self._tokenize_chinese(p, tokenizer) for p in predictions]
|
| 148 |
ref_tokens = [self._tokenize_chinese(r, tokenizer) for r in references]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
#print("Prediction Tokens:", pred_tokens[0])
|
| 151 |
#print("Reference Tokens:", ref_tokens[0])
|
|
|
|
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
# See the License for the specific language governing permissions and
|
| 13 |
# limitations under the License.
|
| 14 |
+
"""Chinese BLEU"""
|
| 15 |
|
| 16 |
import evaluate
|
| 17 |
import datasets
|
| 18 |
import math
|
| 19 |
from collections import Counter
|
| 20 |
+
#import jieba_fast as jieba
|
| 21 |
+
import pycantonese
|
| 22 |
|
| 23 |
# TODO: Add BibTeX citation
|
| 24 |
#_CITATION = """\
|
|
|
|
| 32 |
|
| 33 |
# TODO: Add description of the module here
|
| 34 |
_DESCRIPTION = """\
|
| 35 |
+
This evaluation metric is tailor-made to evaluate the translation quality of Chinese translation using customized implementation of BLEU evaluation metric.
|
| 36 |
"""
|
| 37 |
|
| 38 |
|
|
|
|
| 58 |
{'score': 71.89393375176813, 'counts': [9, 7, 5, 4], 'totals': [9, 8, 7, 6], 'bp': 1.0}
|
| 59 |
"""
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
| 62 |
class ChineseBLEU(evaluate.Metric):
|
| 63 |
+
"""TODO: Chinese BLEU - a BLEU-based metric for Chinese sentences"""
|
| 64 |
|
| 65 |
def _info(self):
|
|
|
|
| 66 |
return evaluate.MetricInfo(
|
|
|
|
| 67 |
module_type="metric",
|
| 68 |
description=_DESCRIPTION,
|
| 69 |
citation=_CITATION,
|
| 70 |
inputs_description=_KWARGS_DESCRIPTION,
|
|
|
|
| 71 |
features=datasets.Features({
|
| 72 |
'predictions': datasets.Value('string'),
|
| 73 |
'references': datasets.Value('string'),
|
| 74 |
}),
|
| 75 |
+
homepage="https://github.com/shivanraptor/chinesebleu",
|
| 76 |
+
codebase_urls=["https://github.com/shivanraptor/chinesebleu"]
|
|
|
|
|
|
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
def _download_and_prepare(self, dl_manager):
|
| 80 |
+
"""No extra files required to download, pass"""
|
|
|
|
| 81 |
pass
|
| 82 |
|
| 83 |
def _tokenize_chinese(self, sentence, tokenizer='char'):
|
|
|
|
| 92 |
list: List of tokens.
|
| 93 |
"""
|
| 94 |
if tokenizer == 'chinese':
|
| 95 |
+
#return list(jieba.cut(sentence, cut_all=False))
|
| 96 |
+
return pycantonese.segment(sentence)
|
| 97 |
else:
|
| 98 |
return list(sentence) # Character-level tokenization
|
| 99 |
|
|
|
|
| 137 |
|
| 138 |
pred_tokens = [self._tokenize_chinese(p, tokenizer) for p in predictions]
|
| 139 |
ref_tokens = [self._tokenize_chinese(r, tokenizer) for r in references]
|
| 140 |
+
|
| 141 |
+
# For total number of tokens < 4, fallback to character-level tokenizations
|
| 142 |
+
if len(pred_tokens) < 4 or len(ref_tokens) < 4:
|
| 143 |
+
tokenizer = 'character'
|
| 144 |
+
pred_tokens = [self._tokenize_chinese(p, tokenizer) for p in predictions]
|
| 145 |
+
ref_tokens = [self._tokenize_chinese(r, tokenizer) for r in references]
|
| 146 |
|
| 147 |
#print("Prediction Tokens:", pred_tokens[0])
|
| 148 |
#print("Reference Tokens:", ref_tokens[0])
|