Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to remove duplicate words with Unicode characters from free‑text in Python

1 Answer

0 votes
import re


def remove_duplicate_words(input_text: str) -> str:
    """Removes duplicate words from a free-text string containing Unicode characters.

    Preserves word order and the case of the first occurrence.
    """
    if not input_text:
        return ""

    # 1. \w+ matches any sequence of Unicode word characters (letters, digits, etc.).
    #    In Python 3, re regexes are full Unicode-aware by default.
    words = re.findall(r"\w+", input_text)

    # 2. Set for O(1) lookup to track lowercase representations of seen words.
    seen = set()
    unique_words = []

    # 3. Iterate through extracted words, maintaining initial case & word order.
    for word in words:
        lower_word = word.lower()
        if lower_word not in seen:
            seen.add(lower_word)
            unique_words.append(word)

    # 4. Join unique words with a single space.
    return " ".join(unique_words)


if __name__ == "__main__":
    input_text = "Hello! こんにちは,  ,hello こんにちは Bună ziua; Γεια σας Bună ziua *HELLO* Γεια σας"

    result = remove_duplicate_words(input_text)

    print(result)


'''
run:

Hello こんにちは Bună ziua Γεια σας

'''

 



answered Aug 4 by avibootz

Related questions

...