v2.5.2
Giriş yap

Python ile cümlelerde kullanılan parantezleri farklı kelimelerde türeterek nasıl kullanabilirim?

apobozdag
419 defa görüntülendi ve 1 kişi tarafından değerlendirildi

şu şekilde bir kod yazdım bu kodu daha nasıl farklı şekilde güzelleştirebiliriz?

istediğim tarz çıktı aşağıda ki gibi;
Egyptian (Spice) Bazaar -> ['Egyptian Bazaar', 'Spice Bazaar']
Gewürzbasar (Ägyptischer Basar) -> ['Gewürzbasar', 'Ägyptischer Basar']

kod örneğim;

@staticmethod
def sentence_derivation(word: str) -> list:
    """
    sample;
    Bazaar -> ['Bazaar']
    Egyptian (Spice) Bazaar -> ['Egyptian Bazaar', 'Spice Bazaar']
    Gewürzbasar (Ägyptischer Basar) -> ['Gewürzbasar', 'Ägyptischer Basar']
    @param word:
    """
    paranthesis_regex = r'((.*))'
    word_split = word.split(')')
    words = []

    if len(word_split) > 1 and word_split[-1] == '':
        text_delete_paranthesis = re.sub(paranthesis_regex, '', word)
        text_inside_paranthesis = re.search(paranthesis_regex, word)

        words.append(text_delete_paranthesis.rstrip())
        if text_inside_paranthesis:
            words.append(text_inside_paranthesis.group(1).rstrip())
    elif len(word_split) > 1 and word_split[-1] is not '':
        text_delete_paranthesis = re.sub(paranthesis_regex, '', word)
        text_inside_paranthesis = re.search(paranthesis_regex, word)

        words.append(text_delete_paranthesis.replace('  ', ' ').rstrip())
        if text_inside_paranthesis:
            words.append(text_inside_paranthesis.group(1) + word_split[-1])
    else:
        words.append(word)

    print(words)
    # ['Mısır Çarşısı']
    # ['Gewürzbasar', 'Ägyptischer Basar']
    # ['Egyptian Bazaar', 'Spice Bazaar']
    # ['بازار مصری']
    # ['السوق المصري']
    # ['므스르 차르슈']
    # ['Египетский базар']
    # ['埃及市场/香料市场']

    return words
Cevap yaz
Cevaplar (0)
Henüz kimse cevap yazmadı. İlk cevap yazan sen ol!