Guest User

Detekcia jazyka z výstupu AIO z Marketing Mineru

a guest
Sep 12th, 2025
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. import pandas as pd
  2. from langdetect import detect, DetectorFactory
  3.  
  4. DetectorFactory.seed = 0
  5.  
  6. # Načítanie Excel súboru
  7. input_file = "stiahnuty-subor-z-mm.xlsx"  # zmeň na názov svojho súboru
  8. df = pd.read_excel(input_file)
  9. df.columns = df.columns.str.strip()
  10.  
  11. # Prioritné URL fragmenty -> jazyk
  12. url_lang_map = {
  13.     "/en/": "en",
  14.     "/cz/": "cs",
  15.     "/cs/": "cs",
  16.     "/sk/": "sk",
  17.     "/pl/": "pl",
  18.     "/hu/": "hu",
  19.     "/de/": "de",
  20.     "/fr/": "fr",
  21.     "/it/": "it",
  22.     "/es/": "es",
  23.     "/ro/": "ro",
  24.     "/bg/": "bg",
  25.     "/gr/": "el",
  26.     "/nl/": "nl",
  27.     "/se/": "sv",
  28.     "/no/": "no",
  29.     "/fi/": "fi",
  30.     "/pt/": "pt",
  31.     "/ru/": "ru",
  32.     "/cn/": "zh",
  33.     "/jp/": "ja",
  34.     "/kr/": "ko",
  35.     "/ae/": "ar",
  36.     "/ro/": "ro",
  37.     "/sa/": "ar"
  38. }
  39.  
  40. # TLD -> jazyk, použije sa len ak je URL fragment nenájdený
  41. tld_lang_map = {
  42.     ".cz": "cs",
  43.     ".sk": "sk",
  44.     ".pl": "pl",
  45.     ".hu": "hu",
  46.     ".de": "de",
  47.     ".fr": "fr",
  48.     ".it": "it",
  49.     ".es": "es",
  50.     ".ro": "ro",
  51.     ".bg": "bg",
  52.     ".gr": "el",
  53.     ".nl": "nl",
  54.     ".se": "sv",
  55.     ".no": "no",
  56.     ".fi": "fi",
  57.     ".pt": "pt",
  58.     ".ru": "ru",
  59.     ".cn": "zh",
  60.     ".jp": "ja",
  61.     ".kr": "ko",
  62.     ".ae": "ar",
  63.     ".sa": "ar",
  64.     ".com": "en",
  65.     ".org": "en",
  66.     ".net": "en"
  67. }
  68.  
  69. def detect_language(title, url):
  70.     if not isinstance(title, str) or len(title.strip()) == 0:
  71.         return "unknown"
  72.  
  73.     url = str(url).lower()  # lowercase všetko
  74.  
  75.     # Priorita: URL fragmenty
  76.     for key, lang in url_lang_map.items():
  77.         if key in url:
  78.             return lang
  79.  
  80.     # Fallback: TLD
  81.     for key, lang in tld_lang_map.items():
  82.         if key in url:
  83.             return lang
  84.  
  85.     # Fallback: langdetect z titulku
  86.     try:
  87.         lang = detect(title)
  88.         # krátke titulky môžu byť nepresné
  89.         if len(title.split()) < 3 and lang not in tld_lang_map.values():
  90.             return "unknown"
  91.         return lang
  92.     except:
  93.         return "unknown"
  94.  
  95. # Vytvorenie stĺpca Language
  96. df['Language'] = df.apply(lambda row: detect_language(row['Title'], row['URL']), axis=1)
  97.  
  98. # Uloženie do nového Excel súboru
  99. output_file = "nazov-vystupneho-suboru.xlsx"
  100. df.to_excel(output_file, index=False)
  101. print(f"Hotovo! Súbor uložený: {output_file}")
Advertisement
Add Comment
Please, Sign In to add comment