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 (
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 currentSentenceLength = 0;
51
52 // Determine whether to generate a shorter post, longer post, or very short post
53 const shorterPostProbability = 0.7; // 70% chance of generating a shorter or very short post
54 const generateShorterOrVeryShortPost = Math.random() < shorterPostProbability;
55 let maxLength;
56
57 if (generateShorterOrVeryShortPost) {
58 // Determine whether to generate a very short post (1-5 words) or a shorter post (up to 140 or 280 characters)
59 const veryShortPostProbability = 0.3; // 30% chance of generating a very short post
60 const generateVeryShortPost = Math.random() < veryShortPostProbability;
61
62 if (generateVeryShortPost) {
63 // Generate a very short post (1-5 words)
64 const wordCount = getRandomInt(1, 5);
65 maxLength = wordCount * 10; // Assuming an average word length of 10 characters
66 } else {
67 // Generate a shorter post (up to 140 or 280 characters)
68 maxLength = getRandomInt(70, 140); // For shorter post
69 // maxLength = getRandomInt(140, 280); // For longer post
70 }
71 } else {
72 // Generate a longer post (up to 280 characters)
73 maxLength = 280;
74 }
75
76 while (result.length < maxLength) {
77 const randomWord = randomWords[getRandomInt(0, randomWords.length - 1)];
78 const wordLength = randomWord.length;
79
80 // Check if adding the word exceeds the maximum length
81 if (result.length + wordLength <= maxLength) {
82 // Add a space if it's not the first word and result is not empty
83 if (result.length > 0) {
84 result += " ";
85 }
86 result += randomWord;
87 currentSentenceLength += wordLength;
88 } else {
89 // Break the loop if adding the word would exceed the maximum length
90 break;
91 }
92 }
93
94 // Optionally add punctuation based on the selected category
95 if (
96 wolfNoises.punctuation &&
97 wolfNoises.punctuation[category] &&
98 wolfNoises.punctuation[category].length > 0
99 ) {
100 const punctuationLength = wolfNoises.punctuation[category].length;
101 const randomPunctuation =
102 wolfNoises.punctuation[category][getRandomInt(0, punctuationLength - 1)];
103 result += randomPunctuation;
104 }
105
106 return result.trim(); // Remove any leading/trailing whitespace
107}