Türkçe metinleri doğru bir şekilde tamamı küçük harf (lowercase), tamamı büyük harf (uppercase) ve sözcüklerin ilk harflerinin büyük olduğu (title case) biçimlerine dönüştürmeye yarayan Python sınıfı. Bu sınıf Python'un büyük İ ve küçük ı konusunda yaşadığı beceriksizliği göz önüne alır. Ayrıca, metni başlık biçimine çevirirken "ve"nin küçük harfle başlamasına, tireden sonra gelen harfin de büyük harf olmasına özen gösterir (Kuruluş isimlerini dönüştürürken önemli).
Last active
January 4, 2024 15:36
-
-
Save anezih/364d12da763fdf677edd6841eca2b27a to your computer and use it in GitHub Desktop.
Türkçe metinlerde büyük/küçük harf değiştiren Python sınıfı
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrCase: | |
def __init__(self) -> None: | |
self.upper_normalize = str.maketrans( | |
{ | |
"ı" : "I", "i" : "İ" | |
} | |
) | |
self.lower_normalize = str.maketrans( | |
{ | |
"I" : "ı", "İ" : "i", | |
} | |
) | |
def tr_upper(self, tumce: str) -> str: | |
return tumce.translate(self.upper_normalize).upper() | |
def tr_lower(self, tumce: str) -> str: | |
return tumce.translate(self.lower_normalize).lower() | |
def tr_title(self, tumce: str) -> str: | |
tumce = self.tr_lower(tumce) | |
tumce_lst = tumce.split(" ") | |
tumce_title = [self.tr_upper(x[0])+x[1:] for x in tumce_lst] | |
if "'" in tumce_title[-1]: | |
ek = tumce_title[-1][tumce_title[-1].index("'")+1:] | |
tumce_title[-1] = tumce_title[-1].replace(ek, self.tr_lower(ek)) | |
tumce_title_res = " ".join(tumce_title) | |
if "Ve" in tumce_title_res: | |
tumce_title_res = tumce_title_res.replace("Ve", "ve") | |
if "-" in tumce_title_res: | |
# tireden sonraki BÜYÜK HARF olması gereken harfin konumu | |
idx = tumce_title_res.index("-") + 1 | |
while tumce_title_res[idx] == " ": | |
idx += 1 | |
tumce_title_res = tumce_title_res[:idx] + self.tr_upper(tumce_title_res[idx]) + tumce_title_res[idx+1:] | |
return tumce_title_res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment