mirabelian/Program.cs
2025-03-21 19:06:46 -04:00

48 lines
1.6 KiB
C#

// I hereby waive this project under the public domain - see UNLICENSE for details.
// Function to read user input
string ReadInput(string prompt)
{
Console.Write(prompt);
return Console.ReadLine();
}
// Function to translate a complete sentence
string TranslateSentence(string sentence, Dictionary<string, string> dictionary)
{
if (string.IsNullOrWhiteSpace(sentence))
return sentence; // Return the original sentence if it's empty or whitespace
// Split the sentence into words, considering punctuation
var words = sentence.Split(
new char[] { ' ', ',', '.', '!', '?' },
StringSplitOptions.RemoveEmptyEntries
);
var translatedWords = words.Select(word => TranslateWord(word, dictionary));
// Reassemble the translated words into a sentence
return string.Join(" ", translatedWords);
}
// Function to translate a single word
string TranslateWord(string word, Dictionary<string, string> dictionary)
{
if (dictionary.TryGetValue(word.ToLower(), out string? translatedWord))
return translatedWord;
else
return word; // Return the original word if not found in the dictionary
}
// Small dictionary for English to Mirabelian translation
// Mirabelian Language dictionary
var translationDict = new Language("mirabelian.json");
// Main method to read input and translate
string inputSentence = ReadInput("Enter an English sentence to translate (or 'exit' to quit): ");
var input = inputSentence.ToLower();
while (input != "exit")
{
string translatedSentence = TranslateSentence(inputSentence, translationDict.Words);
Console.WriteLine($"Translation: {translatedSentence}");
inputSentence = ReadInput("Enter another sentence: ");
}