require "set"
#
# split_words
#
# Splits free text into words using a Unicode-aware regex.
#
# Ruby supports Unicode properties:
# \p{L} → any Unicode letter
# [^\p{L}]+ → any sequence of NON-letters
#
# This gives correct splitting for multilingual free text.
#
def split_words(text)
trimmed = text.strip
# Split on any sequence of non-letter characters
parts = trimmed.split(/[^\p{L}]+/)
parts
end
#
# remove_duplicate_words
#
# Removes duplicate words while preserving:
# - original order
# - original casing of first occurrence
# - case-insensitive comparison
#
# Uses Set for O(1) lookup.
#
def remove_duplicate_words(text)
words = split_words(text)
seen = Set.new
unique = []
words.each do |word|
next if word.empty?
key = word.downcase # Unicode-aware lowercase
unless seen.include?(key)
seen.add(key)
unique << word # preserve original casing
end
end
# Reassemble into a space-separated string
unique.join(" ")
end
#
# Program entry point
#
input =
"Hello, hello! This is a test. A TEST, hello universe... " \
"UNIVERSE! Hello; *** Is Anybody There?"
output = remove_duplicate_words(input)
puts output
=begin
run:
Hello This is a test universe Anybody There
=end