Thanks to fxm one week ago for help to my Fürst example
Code: Select all
' freebasic, dictionary part two, a little more complex with special words and arrays
' 22-03-2024 by frank bruebach
'
'#INCLUDE ONCE "windows.bi"
TYPE Translation
sourceWord AS STRING
targetWord AS STRING
END TYPE
DIM SHARED AS STRING WORDS(200)
DIM dictionaryFile AS STRING
DIM sourceWord AS STRING
DIM SHARED translations() AS Translation
DIM translatedWord AS STRING
DIM SHARED specialWords(10) AS STRING '' Define here an array for special words
DIM SHARED specialWordTranslations(10) AS STRING '' Define here an array for special word translations
SUB SPLIT(a AS STRING, cut AS STRING = " ")
DIM i AS INTEGER, tel AS INTEGER
i = 1
tel = 0
WHILE INSTR(a, cut) <> 0
i = INSTR(a, cut)
WORDS(tel) = LEFT(a, i - 1)
tel = tel + 1
a = MID(a, i + 1)
WEND
WORDS(tel) = a
END SUB
' Load one dictionary from a file
SUB LoadDictionary(BYVAL filePath AS STRING)
DIM fileNum AS INTEGER
DIM LINES AS STRING
DIM numTranslations AS INTEGER
fileNum = FREEFILE
OPEN filePath FOR INPUT AS fileNum
' Count the number of translations in the file
numTranslations = 0
DO WHILE NOT EOF(fileNum)
LINE INPUT #fileNum, LINES
numTranslations = numTranslations + 1
LOOP
' Reset file pointer
SEEK fileNum, 1
' Read translations into the dictionary array
REDIM translations(numTranslations - 1) AS Translation
numTranslations = 0
DO WHILE NOT EOF(fileNum)
LINE INPUT #fileNum, LINES
SPLIT(LINES, "|")
translations(numTranslations).sourceWord = Trim(WORDS(0))
translations(numTranslations).targetWord = Trim(WORDS(1))
numTranslations = numTranslations + 1
LOOP
CLOSE #fileNum
END SUB
' Load special words and translations into arrays
SUB LoadSpecialWords()
specialWords(0) = "morning"
specialWordTranslations(0) = "Morgen"
' Add more special words and translations as needed
specialWords(1) = "time"
specialWordTranslations(1) = "Zeit"
specialWords(2) = "laugh"
specialWordTranslations(2) = "lachen"
specialWords(3) = "born"
specialWordTranslations(3) = "geboren"
specialWords(4) = "plant"
specialWordTranslations(4) = "pflanze"
' and so on much more...
END SUB
' Perform translation
FUNCTION Translate(BYVAL sourceWord AS STRING, dictionary() AS Translation) AS STRING
DIM i AS INTEGER
' Check if the source word is in the specialWords array
FOR i = 0 TO UBOUND(specialWords)
IF LCase(sourceWord) = LCase(specialWords(i)) THEN
' Return the corresponding translation from specialWordTranslations array
Translate = specialWordTranslations(i)
EXIT FUNCTION
END IF
NEXT
' If the source word is not in the specialWords array, search in the dictionary
FOR i = 0 TO UBOUND(dictionary)
IF LCase(sourceWord) = LCase(dictionary(i).sourceWord) THEN
Translate = dictionary(i).targetWord
EXIT FUNCTION
END IF
NEXT
' If translation not found, return the source word itself
Translate = sourceWord
END FUNCTION
' Ask user for dictionary file path
Input "Enter path to dictionary file: ", dictionaryFile
' Load dictionary
LoadDictionary(dictionaryFile)
' Load special words and translations
LoadSpecialWords()
' Ask user for word to translate
INPUT "Enter the word to translate (English to German): ", sourceWord
' Translate word
translatedWord = Translate(sourceWord, translations())
' Display translation
PRINT "Translated word: "; translatedWord
' Wait for user input before exiting
PRINT "Press any key to exit..."
SLEEP
' ends
' c:\Freebasic32\examples\