import re


try:
    import nltk

    NLTK_AVAILABLE = True
except (ImportError, ModuleNotFoundError):
    NLTK_AVAILABLE = False

if NLTK_AVAILABLE:
    try:
        nltk.download("punkt", quiet=True)
    except FileExistsError:  # multiprocessing race condition
        pass


def add_newline_to_end_of_each_sentence(x: str) -> str:
    re.sub("<n>", "", x)  # remove pegasus newline char
    assert NLTK_AVAILABLE, "nltk must be installed to separate newlines betwee sentences. (pip install nltk)"
    return "\n".join(nltk.sent_tokenize(x))

@@ -4,16 +4,47 @@
 ort re%0A%0A
+from filelock import FileLock%0A%0A
 %0Atry:%0A  
@@ -178,19 +178,56 @@
 LE:%0A    
-try
+with FileLock(%22a_random_string%22) as lock
 :%0A      
@@ -267,140 +267,169 @@
 ue)%0A
-    except FileExistsError:  # multiprocessing race condition%0A        pass%0A%0A%0Adef add_newline_to_end_of_each_sentence(x: str) -%3E str:
+%0A%0Adef add_newline_to_end_of_each_sentence(x: str) -%3E str:%0A    %22%22%22This was added to get rougeLsum scores matching published rougeL scores for BART and PEGASUS.%22%22%22
 %0A   
@@ -559,16 +559,17 @@
 s betwee
+n
  sentenc

fix nltk pytest race condition with FileLock (#7515)