This repository has no description
1import * as fs from "fs"; // File system module
2
3// Function to getRandomInt
4function getRandomInt(min: number, max: number): number {
5 return Math.floor(Math.random() * (max - min + 1)) + min;
6}
7
8// Function to read the JSON data
9function getWolfNoises(): {
10 howl: string[];
11 playful: string[];
12 scared: string[];
13 punctuation: { [category: string]: string[] }; // Nested object for category-specific punctuation
14} {
15 try {
16 const data = fs.readFileSync("src/wolf-noises.json", "utf-8");
17 return JSON.parse(data);
18 } catch (error) {
19 console.error("Error reading wolf-noises.json:", error);
20 throw error; // Re-throw the error for handling
21 }
22}
23
24export function generateWolfNoiseString(): string {
25 const wolfNoises = getWolfNoises(); // Read JSON data
26 const categoryProbabilities = {
27 howl: 0.4, // 40% chance
28 playful: 0.3, // 30% chance
29 scared: 0.3, // 30% chance
30 };
31
32 const random = Math.random();
33 let category;
34
35 // Determine the category based on probabilities
36 if (random < categoryProbabilities.howl) {
37 category = "howl";
38 } else if (
39 random <
40 categoryProbabilities.howl + categoryProbabilities.playful
41 ) {
42 category = "playful";
43 } else {
44 category = "scared";
45 }
46
47 const randomWords = wolfNoises[category]; // Get words for the selected category
48
49 let result = "";
50 let maxLength;
51
52 if (category === "howl") {
53 // For howl, limit to 1 word only
54 maxLength = 10; // Assuming an average word length of 10 characters
55 } else {
56 // For other categories, limit to 3-9 words
57 const wordCount = getRandomInt(3, 9);
58 maxLength = wordCount * 10; // Assuming an average word length of 10 characters
59 }
60
61 while (result.length < maxLength) {
62 const randomWord = randomWords[getRandomInt(0, randomWords.length - 1)];
63 const wordLength = randomWord.length;
64
65 // Check if adding the word exceeds the maximum length
66 if (result.length + wordLength <= maxLength) {
67 // Add a space if it's not the first word and result is not empty
68 if (result.length > 0) {
69 result += " ";
70 }
71 result += randomWord;
72 } else {
73 // Break the loop if adding the word would exceed the maximum length
74 break;
75 }
76 }
77
78 // Optionally add punctuation based on the selected category
79 if (
80 wolfNoises.punctuation &&
81 wolfNoises.punctuation[category] &&
82 wolfNoises.punctuation[category].length > 0
83 ) {
84 const punctuationLength = wolfNoises.punctuation[category].length;
85 const randomPunctuation =
86 wolfNoises.punctuation[category][getRandomInt(0, punctuationLength - 1)];
87 result += randomPunctuation;
88 }
89
90 return result.trim(); // Remove any leading/trailing whitespace
91}