File size: 3,215 Bytes
0b617d0 0dffa42 0b617d0 015b8bb 0dffa42 0b617d0 015b8bb 0b617d0 7615696 0b617d0 0dffa42 0b617d0 4ca1672 0b617d0 845ead2 4ca1672 0b617d0 7615696 0b617d0 0dffa42 0b617d0 0dffa42 015b8bb 0b617d0 845ead2 0b617d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# 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.
from statistics import mean
import datasets
from nltk import word_tokenize
from packaging import version
import evaluate
if evaluate.config.PY_VERSION < version.parse("3.8"):
import importlib_metadata
else:
import importlib.metadata as importlib_metadata
NLTK_VERSION = version.parse(importlib_metadata.version("nltk"))
_DESCRIPTION = """
Returns the average length (in terms of the number of words) of the input data.
"""
_KWARGS_DESCRIPTION = """
Args:
`data`: a list of `str` for which the word length is calculated.
`tokenizer` (`Callable`) : the approach used for tokenizing `data` (optional).
The default tokenizer is `word_tokenize` from NLTK: https://www.nltk.org/api/nltk.tokenize.html
This can be replaced by any function that takes a string as input and returns a list of tokens as output.
Returns:
`average_word_length` (`float`) : the average number of words in the input list of strings.
Examples:
>>> data = ["hello world"]
>>> wordlength = evaluate.load("word_length", module_type="measurement")
>>> results = wordlength.compute(data=data)
>>> print(results)
{'average_word_length': 2}
"""
# TODO: Add BibTeX citation
_CITATION = """\
@InProceedings{huggingface:module,
title = {A great new module},
authors={huggingface, Inc.},
year={2020}
}
"""
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class WordLength(evaluate.Measurement):
"""This measurement returns the average number of words in the input string(s)."""
def _info(self):
# TODO: Specifies the evaluate.MeasurementInfo object
return evaluate.MeasurementInfo(
# This is the description that will appear on the modules page.
module_type="measurement",
description=_DESCRIPTION,
citation=_CITATION,
inputs_description=_KWARGS_DESCRIPTION,
# This defines the format of each prediction and reference
features=datasets.Features(
{
"data": datasets.Value("string"),
}
),
)
def _download_and_prepare(self, dl_manager):
import nltk
if NLTK_VERSION >= version.Version("3.9.0"):
nltk.download("punkt_tab")
else:
nltk.download("punkt")
def _compute(self, data, tokenizer=word_tokenize):
"""Returns the average word length of the input data"""
lengths = [len(tokenizer(d)) for d in data]
average_length = mean(lengths)
return {"average_word_length": average_length}
|