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("./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 (random < categoryProbabilities.howl + categoryProbabilities.playful) {
39 category = "playful";
40 } else {
41 category = "scared";
42 }
43
44 const randomWords = wolfNoises[category]; // Get words for the selected category
45
46 let result = "";
47 let currentSentenceLength = 0;
48
49 const maxLength = 280; // Set maximum length for the generated string
50
51 while (result.length < maxLength) {
52 const randomWord = randomWords[getRandomInt(0, randomWords.length - 1)];
53 const wordLength = randomWord.length;
54
55 // Check if adding the word exceeds the maximum length
56 if (result.length + wordLength <= maxLength) {
57 // Add a space if it's not the first word and result is not empty
58 if (result.length > 0) {
59 result += " ";
60 }
61 result += randomWord;
62 currentSentenceLength += wordLength;
63 } else {
64 // Break the loop if adding the word would exceed the maximum length
65 break;
66 }
67 }
68
69 // Optionally add punctuation based on the selected category
70 if (wolfNoises.punctuation && wolfNoises.punctuation[category] && wolfNoises.punctuation[category].length > 0) {
71 const punctuationLength = wolfNoises.punctuation[category].length;
72 const randomPunctuation = wolfNoises.punctuation[category][getRandomInt(0, punctuationLength - 1)];
73 result += randomPunctuation;
74 }
75
76 return result.trim(); // Remove any leading/trailing whitespace
77 }