File size: 6,328 Bytes
3f58ab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9678bfe
3f58ab7
 
 
 
 
 
 
 
 
 
 
 
9678bfe
 
 
 
3f58ab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9678bfe
3f58ab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright 2023 The Google Research Authors.
#
# 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.

"""Utility library of instructions."""

import os
import re

import immutabledict
from packaging.version import parse as parse_version


RANK = os.environ.get("LOCAL_RANK", "0")

# ISO 639-1 codes to language names.
LANGUAGE_CODES = immutabledict.immutabledict(
    {
        "en": "English",
        "es": "Spanish",
        "pt": "Portuguese",
        "ar": "Arabic",
        "hi": "Hindi",
        "fr": "French",
        "ru": "Russian",
        "de": "German",
        "ja": "Japanese",
        "it": "Italian",
        "bn": "Bengali",
        "uk": "Ukrainian",
        "th": "Thai",
        "ur": "Urdu",
        "ta": "Tamil",
        "te": "Telugu",
        "bg": "Bulgarian",
        "ko": "Korean",
        "pl": "Polish",
        "he": "Hebrew",
        "fa": "Persian",
        "vi": "Vietnamese",
        "ne": "Nepali",
        "sw": "Swahili",
        "kn": "Kannada",
        "mr": "Marathi",
        "gu": "Gujarati",
        "pa": "Punjabi",
        "ml": "Malayalam",
        "fi": "Finnish",
    }
)

_ALPHABETS = "([A-Za-z])"
_PREFIXES = "(Mr|St|Mrs|Ms|Dr)[.]"
_SUFFIXES = "(Inc|Ltd|Jr|Sr|Co)"
# _STARTERS = r"(Mr|Mrs|Ms|Dr|Prof|Capt|Cpt|Lt|He\s|She\s|It\s|They\s|Their\s|Our\s|We\s|But\s|However\s|That\s|This\s|Wherever)"
# _ACRONYMS = "([A-Z][.][A-Z][.](?:[A-Z][.])?)"
_WEBSITES = "[.](com|net|org|io|gov|edu|me)"
_DIGITS = "([0-9])"
_MULTIPLE_DOTS = r"\.{2,}"

_MIXED_ALPHABETS = "([A-Za-z가-힣])" # 한글과 영어 모두 포함
_KOREAN_LIST = "([가나다라마바사])"  # 한글 리스트 마커
_KOREAN_LIST_ITEM = "[가나다라마바사아자차카타파하]"  # [hanifeval] v1.1, used line-anchored only

# [hanifeval] two pattern literals below made raw strings (same regex; silences SyntaxWarning).
def split_into_sentences(text):
    """Split the text into sentences. (답변을 문장 단위로 분리합니다.)
    기존 함수를 이용합니다. 한국어 문장 생성에서도 중간에 약어 등은 영어로 표기될 수 있습니다.

    Args:
      text: A string that consists of more than or equal to one sentences.

    Returns:
      A list of strings where each string is a sentence.
    """
    # [hanifeval] v1.1: list markers ("가.", "1.", "2)") are recognised only at the start of a line,
    # before newlines are flattened. IFEval-Ko matched Korean markers anywhere after a space, so the
    # imperative endings "마." / "가." were read as markers, and numbered lines counted "1." as a sentence.
    text = re.sub(r"(?m)^(\s*)(" + _KOREAN_LIST_ITEM + r"|\d{1,3})[.)](\s+)", "\\1\\2<prd>\\3", text)
    text = " " + text + "  "
    text = text.replace("\n", " ")
    text = re.sub(_PREFIXES, "\\1<prd>", text)
    text = re.sub(_WEBSITES, "<prd>\\1", text)
    text = re.sub(_DIGITS + "[.]" + _DIGITS, "\\1<prd>\\2", text)
    text = re.sub(
        _MULTIPLE_DOTS,
        lambda match: "<prd>" * len(match.group(0)) + "<stop>",
        text,
    )
    if "Ph.D" in text:
        text = text.replace("Ph.D.", "Ph<prd>D<prd>")

    # text = re.sub(_ACRONYMS + " " + _STARTERS, "\\1<stop> \\2", text)
    text = re.sub(_MIXED_ALPHABETS + "[.]" + _MIXED_ALPHABETS + "[.]" + _MIXED_ALPHABETS + "[.]", "\\1<prd>\\2<prd>\\3<prd>", text) # 영어/한국어 약어 처리
    text = re.sub(_MIXED_ALPHABETS + "[.]" + _MIXED_ALPHABETS + "[.]", "\\1<prd>\\2<prd>", text) # 영어/한국어 약어 처리
    
    # 기존 영어 약어 처리
    # text = re.sub(
    #     _ALPHABETS + "[.]" + _ALPHABETS + "[.]" + _ALPHABETS + "[.]",
    #     "\\1<prd>\\2<prd>\\3<prd>",
    #     text,
    # )
    # text = re.sub(_ALPHABETS + "[.]" + _ALPHABETS + "[.]", "\\1<prd>\\2<prd>", text) 
    # text = re.sub(" " + _SUFFIXES + "[.] " + _STARTERS, " \\1<stop> \\2", text) # _STARTERS는 사용하지 않음

    text = re.sub(" " + _SUFFIXES + "[.]", " \\1<prd>", text)
    text = re.sub(" " + _ALPHABETS + "[.]", " \\1<prd>", text)
    text = re.sub(r"\s" + _ALPHABETS + r"[.]\s+(?=[가-힣])", " \\1<prd> ", text) # 영어 약어 + 직후 한글이 적힐 시 온점 아님 처리
    # [hanifeval] v1.1: the unanchored Korean list-marker rule was removed (see the top of this function).

    if "”" in text:
        text = text.replace(".”", "”.")
    if '"' in text:
        text = text.replace('."', '".')
    if "!" in text:
        text = text.replace('!"', '"!')
    if "?" in text:
        text = text.replace('?"', '"?')
    text = text.replace(".", ".<stop>")
    text = text.replace("?", "?<stop>")
    text = text.replace("!", "!<stop>")
    text = text.replace("<prd>", ".")
    sentences = text.split("<stop>")
    sentences = [s.strip() for s in sentences]
    if sentences and not sentences[-1]:
        sentences = sentences[:-1]
    return sentences


def count_words(text):
    """Counts the number of words for Korean text.
    띄어쓰기를 기준으로 한국어 문장의 단어를 분리합니다."""
    # 기존 코드
    # tokenizer = nltk.tokenize.RegexpTokenizer(r"\w+") 
    # tokens = tokenizer.tokenize(text)
    # num_words = len(tokens) 

    text = text.strip()
    text = ' '.join(text.split())
    if not text:
        return 0 
    
    return len(text.split())


def count_sentences(text):
    """Count the number of sentences."""
    # tokenizer = _get_sentence_tokenizer()
    # tokenized_sentences = tokenizer.tokenize(text)
    tokenized_sentences = split_into_sentences(text)
    return len(tokenized_sentences)


# 제거된 원본 IFEval 함수
# def generate_keywords(num_keywords):
#     """Randomly generates a few keywords."""
#     return random.sample(WORD_LIST, k=num_keywords)

# @functools.lru_cache(maxsize=None)
# def _get_sentence_tokenizer():
#     return nltk.data.load("nltk:tokenizers/punkt/english.pickle")