Upload sentence_splitter.py
Browse files- sentence_splitter.py +37 -0
sentence_splitter.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 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 |
+
import re
|
| 15 |
+
|
| 16 |
+
from filelock import FileLock
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
import nltk
|
| 21 |
+
|
| 22 |
+
NLTK_AVAILABLE = True
|
| 23 |
+
except (ImportError, ModuleNotFoundError):
|
| 24 |
+
NLTK_AVAILABLE = False
|
| 25 |
+
|
| 26 |
+
if NLTK_AVAILABLE:
|
| 27 |
+
with FileLock(".lock") as lock:
|
| 28 |
+
nltk.download("punkt", quiet=True)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def add_newline_to_end_of_each_sentence(x: str) -> str:
|
| 32 |
+
"""This was added to get rougeLsum scores matching published rougeL scores for BART and PEGASUS."""
|
| 33 |
+
re.sub("<n>", "", x) # remove pegasus newline char
|
| 34 |
+
assert (
|
| 35 |
+
NLTK_AVAILABLE
|
| 36 |
+
), "nltk must be installed to separate newlines between sentences. (pip install nltk)"
|
| 37 |
+
return "\n".join(nltk.sent_tokenize(x))
|