Class: Synthra::Types::TextContent::MessageText

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/text_content/text_generation.rb

Overview

Message text type for chat/DM messages

Generates realistic chat message text using Faker. Useful for simulating direct messages, chat conversations, comments.

Examples:

Generate message text

message_type = MessageText.new
message_type.generate_random(rng, context, {})
# => "Hello! I need help with my order."

Constant Summary collapse

SAMPLE_MESSAGES_EN =

English sample messages

[
  "Hello! I need help with my order.",
  "Hi, can you help me?",
  "Thanks for reaching out!",
  "I have a question about my account.",
  "When will my order arrive?",
  "Great, thank you so much!",
  "Can I get more information?",
  "Is this still available?",
  "Perfect, that's what I needed!",
  "Could you please check this for me?",
  "I'm having trouble with my login.",
  "Thanks for your quick response!",
  "Just wanted to follow up on my request.",
  "Appreciate your help! 🙏",
  "Hi there! 👋",
  "Got it, thanks!",
  "Looking forward to hearing from you.",
  "That sounds good to me.",
  "No problem at all!",
  "Let me know if you need anything else."
].freeze
SAMPLE_MESSAGES_AR =

Arabic sample messages

[
  "مرحباً! أحتاج مساعدة في طلبي.",
  "أهلاً، هل يمكنك مساعدتي؟",
  "شكراً على التواصل!",
  "لدي سؤال عن حسابي.",
  "متى سيصل طلبي؟",
  "رائع، شكراً جزيلاً!",
  "هل يمكنني الحصول على مزيد من المعلومات؟",
  "هل هذا لا يزال متاحاً؟",
  "ممتاز، هذا ما أحتاجه!",
  "هل يمكنك التحقق من هذا من فضلك؟",
  "أواجه مشكلة في تسجيل الدخول.",
  "شكراً على ردك السريع!",
  "أردت المتابعة بخصوص طلبي.",
  "أقدر مساعدتك! 🙏",
  "أهلاً! 👋",
  "فهمت، شكراً!",
  "في انتظار ردك.",
  "يبدو جيداً بالنسبة لي.",
  "لا مشكلة على الإطلاق!",
  "أخبرني إذا احتجت أي شيء آخر."
].freeze
SAMPLE_MESSAGES_ES =

Spanish sample messages

[
  "¡Hola! Necesito ayuda con mi pedido.",
  "Hola, ¿puedes ayudarme?",
  "¡Gracias por contactarnos!",
  "Tengo una pregunta sobre mi cuenta.",
  "¿Cuándo llegará mi pedido?",
  "¡Genial, muchas gracias!",
  "¿Puedo obtener más información?",
  "¿Esto todavía está disponible?",
  "¡Perfecto, eso es lo que necesitaba!",
  "¿Podrías verificar esto por mí?",
  "Tengo problemas con mi inicio de sesión.",
  "¡Gracias por tu rápida respuesta!",
  "Solo quería dar seguimiento a mi solicitud.",
  "¡Aprecio tu ayuda! 🙏",
  "¡Hola! 👋",
  "Entendido, ¡gracias!",
  "Espero tu respuesta.",
  "Me parece bien.",
  "¡No hay problema!",
  "Avísame si necesitas algo más."
].freeze
SAMPLE_MESSAGES_FR =

French sample messages

[
  "Bonjour ! J'ai besoin d'aide avec ma commande.",
  "Salut, peux-tu m'aider ?",
  "Merci de nous avoir contactés !",
  "J'ai une question sur mon compte.",
  "Quand ma commande arrivera-t-elle ?",
  "Super, merci beaucoup !",
  "Puis-je avoir plus d'informations ?",
  "Est-ce toujours disponible ?",
  "Parfait, c'est ce qu'il me fallait !",
  "Pourriez-vous vérifier cela pour moi ?",
  "J'ai des problèmes de connexion.",
  "Merci pour votre réponse rapide !",
  "Je voulais faire un suivi de ma demande.",
  "J'apprécie votre aide ! 🙏",
  "Salut ! 👋",
  "Compris, merci !",
  "J'attends votre réponse.",
  "Ça me convient.",
  "Pas de problème !",
  "Faites-moi savoir si vous avez besoin d'autre chose."
].freeze
MESSAGES_BY_LOCALE =

Locale to messages mapping

{
  en: SAMPLE_MESSAGES_EN,
  ar: SAMPLE_MESSAGES_AR,
  es: SAMPLE_MESSAGES_ES,
  fr: SAMPLE_MESSAGES_FR
}.freeze
SAMPLE_MESSAGES =

Keep backward compatibility

SAMPLE_MESSAGES_EN
FAKER_LOCALES =

Faker locale mapping

{
  en: :en,
  ar: :ar,
  es: :es,
  fr: :fr,
  de: :de,
  it: :it,
  pt: :pt,
  ru: :ru,
  ja: :ja,
  ko: :ko,
  zh: :'zh-CN'
}.freeze

Instance Method Summary collapse

Instance Method Details

#extract_locale(args) ⇒ Object (private)



348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/synthra/types/text_content/text_generation.rb', line 348

def extract_locale(args)
  locale_arg = args[:locale] || args[:language] || args[:lang] || :en
  locale_str = locale_arg.to_s.downcase.strip
  
  case locale_str
  when "en", "english" then :en
  when "ar", "arabic" then :ar
  when "es", "spanish" then :es
  when "fr", "french" then :fr
  else
    locale_str.to_sym
  end
end

#generate_edge(rng, context, args) ⇒ Object



394
395
396
397
398
399
400
401
402
403
# File 'lib/synthra/types/text_content/text_generation.rb', line 394

def generate_edge(rng, context, args)
  rng.sample([
    "",                    # Empty message
    "👋",                  # Single emoji
    "a",                   # Single character
    "OK",                  # Very short
    "!" * 100,            # Long repeated chars
    "Hello 你好 مرحبا"     # Unicode/multilingual
  ])
end

#generate_faker_arabic_message(rng) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/synthra/types/text_content/text_generation.rb', line 301

def generate_faker_arabic_message(rng)
  # Arabic-specific message generation using supported Faker modules
  templates = [
    -> { "مرحباً #{Faker::Name.first_name}، كيف حالك؟" },
    -> { "أهلاً بك في #{Faker::Company.name}" },
    -> { "شكراً لتواصلك مع #{Faker::Company.name}" },
    -> { "مرحباً، أنا #{Faker::Name.name}" },
    -> { "تحياتي من #{Faker::Address.city}" },
    -> { "#{Faker::Name.first_name} يرحب بك" },
    -> { "نتطلع للعمل معك في #{Faker::Company.name}" },
    -> { "#{Faker::Company.name} في خدمتك" },
    -> { "أهلاً من #{Faker::Address.city}!" },
    -> { "تواصل معنا في #{Faker::Company.name}" },
  ]
  templates[rng.int(0, templates.length - 1)].call
end

#generate_faker_latin_message(rng) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/synthra/types/text_content/text_generation.rb', line 318

def generate_faker_latin_message(rng)
  case rng.int(0, 5)
  when 0
    Faker::Lorem.sentence(word_count: rng.int(4, 10))
  when 1
    Faker::Lorem.question(word_count: rng.int(4, 8))
  when 2
    # Company catch phrase - works in most locales
    Faker::Company.catch_phrase
  when 3
    # Try greeting - falls back gracefully
    begin
      Faker::Greeting.hi
    rescue
      Faker::Lorem.sentence(word_count: rng.int(3, 6))
    end
  when 4
    # Quote - works in most locales
    begin
      Faker::Quote.famous_last_words
    rescue
      Faker::Lorem.sentence(word_count: rng.int(5, 10))
    end
  else
    Faker::Lorem.sentences(number: 1).first
  end
end

#generate_faker_message(rng, faker_locale) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/synthra/types/text_content/text_generation.rb', line 285

def generate_faker_message(rng, faker_locale)
  original_locale = Faker::Config.locale
  begin
    Faker::Config.locale = faker_locale
    
    # For Arabic, use different approach since Lorem doesn't support Arabic
    if faker_locale == :ar
      generate_faker_arabic_message(rng)
    else
      generate_faker_latin_message(rng)
    end
  ensure
    Faker::Config.locale = original_locale
  end
end

#generate_invalid(rng, context, args) ⇒ Object



405
406
407
408
409
410
411
412
413
# File 'lib/synthra/types/text_content/text_generation.rb', line 405

def generate_invalid(rng, context, args)
  rng.sample([
    nil,
    123,
    [],
    {},
    true
  ])
end

#generate_mixed_locale(rng, args) ⇒ Object (private)



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/synthra/types/text_content/text_generation.rb', line 362

def generate_mixed_locale(rng, args)
  # Parse locales from various formats
  locales = if args[:locales].is_a?(Array)
              args[:locales].map { |l| normalize_locale(l) }
            elsif args[:mixed].is_a?(Array)
              args[:mixed].map { |l| normalize_locale(l) }
            elsif args[:locale].is_a?(String) && args[:locale].include?(",")
              args[:locale].split(",").map { |l| normalize_locale(l.strip) }
            else
              [:en, :ar] # Default mixed
            end
  
  # Pick a random locale
  selected_locale = locales[rng.int(0, locales.length - 1)]
  messages = MESSAGES_BY_LOCALE[selected_locale] || SAMPLE_MESSAGES_EN
  messages[rng.int(0, messages.length - 1)]
end

#generate_random(rng, context, args) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/synthra/types/text_content/text_generation.rb', line 255

def generate_random(rng, context, args)
  # Check for mixed locales BEFORE extracting locale
  locale_arg = args[:locale] || args[:language] || args[:lang]
  
  # If mixed locales requested (e.g., locale: "en,ar" or locales: ["en", "ar"])
  if args[:locales] || args[:mixed] || (locale_arg.is_a?(String) && locale_arg.include?(","))
    return generate_mixed_locale(rng, args)
  end
  
  locale = extract_locale(args)
  
  # Single locale
  messages = MESSAGES_BY_LOCALE[locale] || SAMPLE_MESSAGES_EN
  faker_locale = FAKER_LOCALES[locale] || :en
  
  # Use Faker with locale for variety (50% Faker, 50% static)
  case rng.int(0, 9)
  when 0..4
    # Use Faker with the appropriate locale
    generate_faker_message(rng, faker_locale)
  else
    # Use static messages for guaranteed quality
    messages[rng.int(0, messages.length - 1)]
  end

rescue StandardError
  messages = MESSAGES_BY_LOCALE[locale] || SAMPLE_MESSAGES_EN
  messages[rng.int(0, messages.length - 1)]
end

#normalize_locale(locale) ⇒ Object (private)



380
381
382
383
384
385
386
387
388
389
# File 'lib/synthra/types/text_content/text_generation.rb', line 380

def normalize_locale(locale)
  case locale.to_s.downcase
  when "en", "english" then :en
  when "ar", "arabic" then :ar
  when "es", "spanish" then :es
  when "fr", "french" then :fr
  else
    locale.to_s.to_sym
  end
end