Dictionary Part two

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 55
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Dictionary Part two

Post by Löwenherz »

Hello all, Here is a little example for a Mini dictionary
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\
Thx, Feedback is Welcome, frank
Lothar Schirm
Posts: 443
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Dictionary Part two

Post by Lothar Schirm »

Hi Frank,
looks nice, but could you add a dictionary file as example so that it is possible to play around wth your code? Why do you include windows.bi? Your program seems to be a console application.
Löwenherz
Posts: 55
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Dictionary Part two

Post by Löwenherz »

Yes lothar.. better to use the Songtext too of the byrds turnturnturn

looks nice, but could you add a dictionary file as example so that it is possible to play around wth your code? Why do you include windows.bi? Your program seems to be a console application
First I Had the Idea to make a winapi gui


Code: Select all

To everything, turn, turn, turn
There is a season, turn, turn, turn
And a time to every purpose under heaven
A time to be born, a time to die
A time to plant, a time to reap
A time to kill, a time to heal
A time to laugh, a time to weep
To everything, turn, turn, turn
There is a season, turn, turn, turn
And a time to every purpose, under heaven
A time to build up, a time to break down
A time to dance, a time to mourn
A time to cast away stones
A time to gather stones together
To everything, turn, turn, turn
There is a season, turn, turn, turn
And a time to every purpose under heaven
A time of love, a time of hate
A time of war, a time of peace
A time you may embrace
A time to refrain from embracing
To everything, turn, turn, turn
There is a season, turn, turn, turn
And a time to every purpose under heaven
A time to gain, a time to lose
A time to rend, a time to sew
A time for love, a time for hate
A time for peace, I swear it's not too late
[,/Code]
Lothar Schirm
Posts: 443
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Dictionary Part two

Post by Lothar Schirm »

hm... very mini! Is this just a short test? The list of "special words" is rather short, and the program is finished after one word has ben translated. I think the program should run in a loop until the user terminates it.
Löwenherz
Posts: 55
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Dictionary Part two

Post by Löwenherz »

An Update will come soon with more word's I Hadnt enough time to find my new example saved IT someehere else ;)

Here some more wird's but you can sdd new word's too If you Like I have never Said thats a complete and perfect dictionary example ;) thats Not possible with this Kind .. and that wasnt so easy to build and took some time

Code: Select all


' freebasic, dictionary part two, a little more complex with special words and arrays
' 22-03-2024 by modelfb
'
'#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
' 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) = "pflanzen"
    specialWords(5) = "turn"
    specialWordTranslations(5) = "drehen"
    specialWords(6) = "purpose"
    specialWordTranslations(6) = "Zweck"
    specialWords(7) = "season"
    specialWordTranslations(7) = "Jahreszeit"
    specialWords(8) = "build up"
    specialWordTranslations(8) = "aufbauen"
    specialWords(9) = "under"
    specialWordTranslations(9) = "unter"
    specialWords(10) = "heaven"
    specialWordTranslations(10) = "Himmel"
    specialWords(11) = "cast"
    specialWordTranslations(11) = "werfen"
    specialWords(12) = "stones"
    specialWordTranslations(12) = "steine"
    specialWords(13) = "dance"
    specialWordTranslations(13) = "tanzen"
    specialWords(14) = "away"
    specialWordTranslations(14) = "weg"
    specialWords(15) = "weep"
    specialWordTranslations(15) = "weinen"
    
    ' 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

Lost Zergling
Posts: 540
Joined: Dec 02, 2011 22:51
Location: France

Re: Dictionary Part two

Post by Lost Zergling »

Hello.
Maybe you could consider using the tool lzle (viewtopic.php?t=26551) ? 8)
Post Reply