Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- from langdetect import detect, DetectorFactory
- DetectorFactory.seed = 0
- # Načítanie Excel súboru
- input_file = "stiahnuty-subor-z-mm.xlsx" # zmeň na názov svojho súboru
- df = pd.read_excel(input_file)
- df.columns = df.columns.str.strip()
- # Prioritné URL fragmenty -> jazyk
- url_lang_map = {
- "/en/": "en",
- "/cz/": "cs",
- "/cs/": "cs",
- "/sk/": "sk",
- "/pl/": "pl",
- "/hu/": "hu",
- "/de/": "de",
- "/fr/": "fr",
- "/it/": "it",
- "/es/": "es",
- "/ro/": "ro",
- "/bg/": "bg",
- "/gr/": "el",
- "/nl/": "nl",
- "/se/": "sv",
- "/no/": "no",
- "/fi/": "fi",
- "/pt/": "pt",
- "/ru/": "ru",
- "/cn/": "zh",
- "/jp/": "ja",
- "/kr/": "ko",
- "/ae/": "ar",
- "/ro/": "ro",
- "/sa/": "ar"
- }
- # TLD -> jazyk, použije sa len ak je URL fragment nenájdený
- tld_lang_map = {
- ".cz": "cs",
- ".sk": "sk",
- ".pl": "pl",
- ".hu": "hu",
- ".de": "de",
- ".fr": "fr",
- ".it": "it",
- ".es": "es",
- ".ro": "ro",
- ".bg": "bg",
- ".gr": "el",
- ".nl": "nl",
- ".se": "sv",
- ".no": "no",
- ".fi": "fi",
- ".pt": "pt",
- ".ru": "ru",
- ".cn": "zh",
- ".jp": "ja",
- ".kr": "ko",
- ".ae": "ar",
- ".sa": "ar",
- ".com": "en",
- ".org": "en",
- ".net": "en"
- }
- def detect_language(title, url):
- if not isinstance(title, str) or len(title.strip()) == 0:
- return "unknown"
- url = str(url).lower() # lowercase všetko
- # Priorita: URL fragmenty
- for key, lang in url_lang_map.items():
- if key in url:
- return lang
- # Fallback: TLD
- for key, lang in tld_lang_map.items():
- if key in url:
- return lang
- # Fallback: langdetect z titulku
- try:
- lang = detect(title)
- # krátke titulky môžu byť nepresné
- if len(title.split()) < 3 and lang not in tld_lang_map.values():
- return "unknown"
- return lang
- except:
- return "unknown"
- # Vytvorenie stĺpca Language
- df['Language'] = df.apply(lambda row: detect_language(row['Title'], row['URL']), axis=1)
- # Uloženie do nového Excel súboru
- output_file = "nazov-vystupneho-suboru.xlsx"
- df.to_excel(output_file, index=False)
- print(f"Hotovo! Súbor uložený: {output_file}")
Advertisement
Add Comment
Please, Sign In to add comment