File size: 12,351 Bytes
78d0e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// African Proverb Validation System for Cultural Anchoring
// Ensures all FlameBorn actions are rooted in authentic African wisdom

import crypto from "crypto"

export interface AfricanProverb {
  proverb: string
  language: string
  country: string
  region: string
  translated_meaning: string
  cultural_notes: string
  themes: string[]
  hash: string
}

interface ProverbChallenge {
  question: string
  options: string[]
  correctAnswer: number
  proverb: AfricanProverb
}

interface CulturalValidation {
  isValid: boolean
  culturalRelevance: number
  message: string
}

class ProverbValidator {
  private proverbs: AfricanProverb[] = []
  private proverbMap: Map<string, AfricanProverb> = new Map()
  private hashMap: Map<string, AfricanProverb> = new Map()

  constructor() {
    this.initializeProverbs()
  }

  private initializeProverbs() {
    const rawProverbs = [
      {
        proverb: "When spider webs unite, they can tie up a lion",
        language: "Akan",
        country: "Ghana",
        region: "West Africa",
        translated_meaning: "Unity and cooperation can overcome any challenge",
        cultural_notes:
          "This proverb teaches the power of collective action in African communities. It emphasizes that even the smallest individuals, when united, can achieve great things.",
        themes: ["unity", "cooperation", "strength", "community"],
      },
      {
        proverb: "Ubuntu ngumuntu ngabantu",
        language: "Zulu",
        country: "South Africa",
        region: "Southern Africa",
        translated_meaning: "I am because we are",
        cultural_notes:
          "Ubuntu is the foundational African philosophy emphasizing our interconnectedness and shared humanity. Individual well-being is tied to community well-being.",
        themes: ["ubuntu", "community", "humanity", "interconnectedness"],
      },
      {
        proverb: "Se wo were fi na wosankofa a yenkyi",
        language: "Akan",
        country: "Ghana",
        region: "West Africa",
        translated_meaning: "It is not wrong to go back for that which you have forgotten",
        cultural_notes:
          "Sankofa teaches us to learn from the past to move forward wisely. The symbol shows a bird looking backward while moving forward.",
        themes: ["wisdom", "learning", "past", "progress"],
      },
      {
        proverb: "Haraka haraka haina baraka",
        language: "Swahili",
        country: "Kenya",
        region: "East Africa",
        translated_meaning: "Hurry hurry has no blessings",
        cultural_notes:
          "This proverb warns against rushing through life without taking time to do things properly. Patience and deliberation lead to better outcomes.",
        themes: ["patience", "wisdom", "quality", "deliberation"],
      },
      {
        proverb: "Omo ti a ba so fun pe baba re ku, ti o ba gbagbe, ojo kan ni yio ri baba re",
        language: "Yoruba",
        country: "Nigeria",
        region: "West Africa",
        translated_meaning: "A child who forgets that his father is dead will one day see his father",
        cultural_notes:
          "This teaches that we cannot escape our roots and heritage. Our ancestors live on through us and their wisdom guides us.",
        themes: ["ancestry", "heritage", "memory", "guidance"],
      },
      {
        proverb: "Umuntu ngumuntu ngabantu",
        language: "Xhosa",
        country: "South Africa",
        region: "Southern Africa",
        translated_meaning: "A person is a person through other persons",
        cultural_notes:
          "Similar to Ubuntu, this emphasizes that our humanity is affirmed by recognizing the humanity in others. We become fully human through our relationships.",
        themes: ["ubuntu", "humanity", "relationships", "community"],
      },
      {
        proverb: "Mti hauendi ila kwa nyenzo",
        language: "Swahili",
        country: "Tanzania",
        region: "East Africa",
        translated_meaning: "A tree does not go anywhere without its roots",
        cultural_notes:
          "This proverb emphasizes the importance of staying connected to one's origins, culture, and community while growing and developing.",
        themes: ["roots", "culture", "growth", "identity"],
      },
      {
        proverb: "Agbara eniyan ni eniyan",
        language: "Yoruba",
        country: "Nigeria",
        region: "West Africa",
        translated_meaning: "The strength of a person is other people",
        cultural_notes:
          "This highlights that individual strength comes from community support. No one succeeds alone; we are strengthened by our connections to others.",
        themes: ["strength", "community", "support", "interdependence"],
      },
      {
        proverb: "Motho ke motho ka batho",
        language: "Sotho",
        country: "Lesotho",
        region: "Southern Africa",
        translated_meaning: "A person is a person because of other people",
        cultural_notes:
          "Another expression of Ubuntu philosophy, emphasizing that our identity and humanity are shaped by our relationships and community interactions.",
        themes: ["ubuntu", "identity", "community", "relationships"],
      },
      {
        proverb: "Baobab tree does not grow in one day",
        language: "Shona",
        country: "Zimbabwe",
        region: "Southern Africa",
        translated_meaning: "Great achievements take time and patience",
        cultural_notes:
          "The baobab tree is sacred in African culture, living for thousands of years. This proverb teaches patience and persistence in pursuing meaningful goals.",
        themes: ["patience", "growth", "achievement", "persistence"],
      },
      {
        proverb: "Inyama yegomo rinofa nemombe",
        language: "Shona",
        country: "Zimbabwe",
        region: "Southern Africa",
        translated_meaning: "The meat of a cow dies with the cow",
        cultural_notes:
          "This teaches that material possessions are temporary, but wisdom, character, and relationships endure beyond physical life.",
        themes: ["wisdom", "legacy", "materialism", "values"],
      },
      {
        proverb: "Akoko nan tia ba na enkum nan tia ba",
        language: "Akan",
        country: "Ghana",
        region: "West Africa",
        translated_meaning: "The hen's foot treads on the chick, but it does not kill the chick",
        cultural_notes:
          "This proverb speaks to the protective nature of community elders and the understanding that discipline comes from love, not harm.",
        themes: ["protection", "discipline", "love", "community"],
      },
      {
        proverb: "Munhu haasi chikoro",
        language: "Shona",
        country: "Zimbabwe",
        region: "Southern Africa",
        translated_meaning: "A person is not a school",
        cultural_notes:
          "This means that learning never ends. Unlike a school which you can graduate from, a person continues learning throughout life.",
        themes: ["learning", "growth", "humility", "wisdom"],
      },
      {
        proverb: "Ukweli hauogopi upelelezi",
        language: "Swahili",
        country: "Kenya",
        region: "East Africa",
        translated_meaning: "Truth does not fear investigation",
        cultural_notes:
          "This proverb emphasizes the power of truth and honesty. When something is true, it can withstand any scrutiny or questioning.",
        themes: ["truth", "honesty", "integrity", "investigation"],
      },
      {
        proverb: "Eni ti o ba gbo ohun ti agba so, yio so ohun ti omo so",
        language: "Yoruba",
        country: "Nigeria",
        region: "West Africa",
        translated_meaning: "He who listens to what the elder says will say what the child says",
        cultural_notes:
          "This teaches the importance of respecting elders and learning from their wisdom. Those who ignore elder wisdom often speak foolishly.",
        themes: ["respect", "wisdom", "elders", "learning"],
      },
    ]

    this.proverbs = rawProverbs.map((p) => {
      const hash = this.generateHash(p.proverb)
      const proverb: AfricanProverb = { ...p, hash }
      this.proverbMap.set(p.translated_meaning.toLowerCase(), proverb)
      this.proverbMap.set(p.proverb.toLowerCase(), proverb)
      this.hashMap.set(hash, proverb)
      return proverb
    })
  }

  private generateHash(text: string): string {
    return crypto.createHash("sha256").update(text.toLowerCase().trim()).digest("hex").substring(0, 16)
  }

  validateProverb(input: string): boolean {
    const normalized = input.toLowerCase().trim()
    return this.proverbMap.has(normalized)
  }

  getProverbMeta(input: string): AfricanProverb | null {
    const normalized = input.toLowerCase().trim()
    return this.proverbMap.get(normalized) || null
  }

  getProverbHash(input: string): string {
    const proverb = this.getProverbMeta(input)
    return proverb ? proverb.hash : this.generateHash(input)
  }

  validateProverbHash(hash: string): boolean {
    return this.hashMap.has(hash)
  }

  getProverbByHash(hash: string): AfricanProverb | null {
    return this.hashMap.get(hash) || null
  }

  getRandomProverb(): AfricanProverb {
    const randomIndex = Math.floor(Math.random() * this.proverbs.length)
    return this.proverbs[randomIndex]
  }

  searchProverbs(criteria: { theme?: string; region?: string; language?: string }): AfricanProverb[] {
    return this.proverbs.filter((proverb) => {
      if (criteria.theme && !proverb.themes.includes(criteria.theme)) return false
      if (criteria.region && proverb.region !== criteria.region) return false
      if (criteria.language && proverb.language !== criteria.language) return false
      return true
    })
  }

  generateProverbChallenge(): ProverbChallenge {
    const correctProverb = this.getRandomProverb()
    const wrongProverbs = this.proverbs
      .filter((p) => p.hash !== correctProverb.hash)
      .sort(() => Math.random() - 0.5)
      .slice(0, 3)

    const allOptions = [correctProverb, ...wrongProverbs].sort(() => Math.random() - 0.5)

    const correctAnswer = allOptions.findIndex((p) => p.hash === correctProverb.hash)

    return {
      question: `What does this ${correctProverb.language} proverb mean: "${correctProverb.proverb}"?`,
      options: allOptions.map((p) => p.translated_meaning),
      correctAnswer,
      proverb: correctProverb,
    }
  }

  validateCulturalContext(content: string, anchoringProverb: string): CulturalValidation {
    const proverb = this.getProverbMeta(anchoringProverb)

    if (!proverb) {
      return {
        isValid: false,
        culturalRelevance: 0,
        message: "Anchoring proverb not found in verified African wisdom database",
      }
    }

    // Simple relevance scoring based on theme alignment
    const contentLower = content.toLowerCase()
    const relevantThemes = proverb.themes.filter(
      (theme) => contentLower.includes(theme) || contentLower.includes(theme.substring(0, 4)),
    )

    const culturalRelevance = relevantThemes.length / proverb.themes.length

    if (culturalRelevance >= 0.3) {
      return {
        isValid: true,
        culturalRelevance,
        message: `Content aligns with ${proverb.language} wisdom themes: ${relevantThemes.join(", ")}`,
      }
    } else {
      return {
        isValid: false,
        culturalRelevance,
        message: `Content does not align with the cultural themes of the anchoring proverb: ${proverb.themes.join(", ")}`,
      }
    }
  }

  getAvailableLanguages(): string[] {
    return [...new Set(this.proverbs.map((p) => p.language))]
  }

  getAvailableRegions(): string[] {
    return [...new Set(this.proverbs.map((p) => p.region))]
  }

  getAvailableCountries(): string[] {
    return [...new Set(this.proverbs.map((p) => p.country))]
  }

  getProverbsByTheme(theme: string): AfricanProverb[] {
    return this.proverbs.filter((p) => p.themes.includes(theme.toLowerCase()))
  }

  getAllThemes(): string[] {
    const themes = new Set<string>()
    this.proverbs.forEach((p) => p.themes.forEach((theme) => themes.add(theme)))
    return Array.from(themes)
  }
}

// Export singleton instance
export const proverbValidator = new ProverbValidator()

// Export types
export type { ProverbChallenge, CulturalValidation }