Jump to content

Help with a pice of code


Recommended Posts

Hello, i wrote this pice of code but i got stack somewhere.

It should search the file words.txt (a word list) line by line for a certain word (the one from the input box) and if found, a MsgBox should appear and change of a Label in GUI, if not found, it should add the written word to the list and change another Label in GUI(Like nomber of errors and matches).

The point where it stucks is after reading the InputBox.

#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Quest", 425, 126, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$OkLabel = GUICtrlCreateLabel("0", 392, 8, 20, 41)
GUICtrlSetFont($OkLabel, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor($OkLabel, 0x00FF00)
$input = GUICtrlCreateInput("word", 8, 64, 369, 21)
$Label2 = GUICtrlCreateLabel("Please say the word:", 8, 24, 272, 28)
GUICtrlSetFont($Label2, 14, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Say!", 128, 96, 145, 25)
GUICtrlSetOnEvent($Button1, "Button1Click")
$ErrLabel = GUICtrlCreateLabel("0", 392, 64, 20, 41)
GUICtrlSetFont($ErrLabel, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor($ErrLabel, 0xFF0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    Sleep(100)
WEnd

Func Button1Click()
    Local $sFilePath = "D:\brain\words.txt"

    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    $read = GUICtrlRead($input)

        For $x = 1 to _FileCountLines($sFilePath)

    $sFileRead = FileReadLine($hFileOpen, $x)

    $serch= StringInStr($sFileRead, $read)

If $serch Then
     MsgBox(default, default, "Word Ok!")
  GUICtrlSetData($ErrLabel, ($ErrLabel+1))
Else

    FileWriteLine("D:\brain\words.txt", $read)
        MsgBox(default, default, "Word nOK!")
GUICtrlSetData($OkLabel, ($OkLabel+1))
EndIf
        Next
FileClose($hFileOpen)
EndFunc

Func Form1Close()
Exit
EndFunc

Thanks!

Link to comment
Share on other sites

Use a boolean

#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Quest", 425, 126, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$OkLabel = GUICtrlCreateLabel("0", 392, 8, 20, 41)
GUICtrlSetFont($OkLabel, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor($OkLabel, 0x00FF00)
$input = GUICtrlCreateInput("word", 8, 64, 369, 21)
$Label2 = GUICtrlCreateLabel("Please say the word:", 8, 24, 272, 28)
GUICtrlSetFont($Label2, 14, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Say!", 128, 96, 145, 25)
GUICtrlSetOnEvent($Button1, "Button1Click")
$ErrLabel = GUICtrlCreateLabel("0", 392, 64, 20, 41)
GUICtrlSetFont($ErrLabel, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor($ErrLabel, 0xFF0000)
GUISetState(@SW_SHOW)


While 1
    Sleep(100)
WEnd

Func Button1Click()
    Local $sFilePath = "words.txt", $found = 0
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    $read = GUICtrlRead($input)

    For $x = 1 to _FileCountLines($sFilePath)
          $sFileRead = FileReadLine($hFileOpen, $x)
          If StringInStr($sFileRead, $read) Then
              $found = 1
              Exitloop
         EndIf
    Next
     If $found = 1 Then
             MsgBox(default, default, "Word Ok!")
             GUICtrlSetData($ErrLabel, GUICtrlRead($ErrLabel)+1)
    Else
             FileWriteLine("words.txt", $read)
             MsgBox(default, default, "Word nOK!")
             GUICtrlSetData($OkLabel, GUICtrlRead($OkLabel)+1)
    EndIf

FileClose($hFileOpen)
 GUICtrlSetData($input, "")
EndFunc

Func Form1Close()
Exit
EndFunc

 

Edited by mikell
Link to comment
Share on other sites

compact21,

Reading a file line by line is slow.  It is also unnecessary to read the file every time the function is entered.  Consider the following code.  I added some error checking and changed the detection logic...

#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Quest", 425, 126, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$OkLabel = GUICtrlCreateLabel("0", 392, 8, 20, 41)
GUICtrlSetFont($OkLabel, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor($OkLabel, 0x00FF00)
$input = GUICtrlCreateInput("", 8, 64, 369, 21)                         ; initialize to blank
$Label2 = GUICtrlCreateLabel("Please say the word:", 8, 24, 272, 28)
GUICtrlSetFont($Label2, 14, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Say!", 128, 96, 145, 25)
GUICtrlSetOnEvent($Button1, "Button1Click")
$ErrLabel = GUICtrlCreateLabel("0", 392, 64, 20, 41)
GUICtrlSetFont($ErrLabel, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor($ErrLabel, 0xFF0000)
GUISetState(@SW_SHOW)

; read list of words to a string variable
Local $sWordList = FileRead(@ScriptDir & '\wordlist.txt')

While 1
    Sleep(999999) ; Rip Van Winkle
WEnd



Func Button1Click()

    ; check that a word was entered
    if guictrlread($input) = '' then
        MsgBox($MB_ICONERROR, 'ERROR', "Please enter a word in the input area")
        GUICtrlSetState($input,$GUI_FOCUS)
        Return
    endif



    ; case statement to return true if word found or false if word not found
    Switch StringRegExp($sWordList, '\b' & GUICtrlRead($input) & '\b')
        Case True
            MsgBox(Default, Default, "Word Ok!")
            GUICtrlSetData($ErrLabel, GUICtrlRead($ErrLabel) + 1)
        Case False
            $sWordList &= @crlf & guictrlread($input)
            Filedelete(@ScriptDir & '\wordlist.txt')
            FileWrite(@ScriptDir & '\wordlist.txt', $sWordList)
            MsgBox(Default, Default, "Word nOK!")
            GUICtrlSetData($OkLabel, GUICtrlRead($OkLabel) + 1)
    EndSwitch



    GUICtrlSetData($input, "")          ;   blank out input control
    GUICtrlSetState($input,$GUI_FOCUS)  ;   force focus to input control

EndFunc   ;==>Button1Click

Func Form1Close()
    Exit
EndFunc   ;==>Form1Close

This is the file I used for testing ->  wordlist.txt

kylomas

Edited by kylomas
too many files inserted

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hello again, i'm still working on my small program and i want it to check the spelling of every word in a phrase i enter.

This is as far as i got.

I would like it to show me the wrong word instead of giving alternatives(i can probably)do this by myself )but i don't know what i have to do if the i miss a space like: Autoit iscool.)

Any ideeas?

I have copied and implemented the spell checking code from here from Melba23 first post.

#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

#Region GUI
$Form1 = GUICreate("Quest", 425, 126, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$OkLabel = GUICtrlCreateLabel("0", 392, 8, 20, 41)
GUICtrlSetFont($OkLabel, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor($OkLabel, 0x00FF00)
$input = GUICtrlCreateInput("hi", 8, 64, 369, 21)                         ; initialize to blank
$Label2 = GUICtrlCreateLabel("Please say the phrase:", 8, 24, 272, 28)
GUICtrlSetFont($Label2, 14, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Say!", 128, 96, 145, 25)
GUICtrlSetOnEvent($Button1, "Button1Click")
$ErrLabel = GUICtrlCreateLabel("0", 392, 64, 20, 41)
GUICtrlSetFont($ErrLabel, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor($ErrLabel, 0xFF0000)
GUISetState(@SW_SHOW)
#EndRegion GUI

Local $sWordList = FileRead(@ScriptDir & '\questlist.txt') ; read list of words to a string variable

Global $sList = "WordList.txt" ;This is the dictionary file where the typed words are searched

While 1
    Sleep(999999) ; Rip Van Winkle
WEnd

Func Button1Click()


$asCount = StringSplit(GUICtrlRead($input), " ") ;Read the input
for $q = 1 To $asCount[0] ;For every word it founds

#Region Spell Checking
$sToCheck = $asCount[$q] ;It will check the spelling
If $sToCheck Then
    $sRet = Correct($sToCheck)
    If $sRet Then
        MsgBox(0, "Found", $sRet)
    Else
        MsgBox(0, "Not found", $sRet)
    EndIf
Else
    MsgBox(0, "Error", "Nothing to check!")
EndIf
#EndRegion Spell Checking

next

    ; check that a word was entered
    if guictrlread($input) = '' then
        MsgBox($MB_ICONERROR, 'ERROR', "Please enter a word in the input area")
        GUICtrlSetState($input,$GUI_FOCUS)
        Return
    endif



    ; case statement to return true if word found or false if word not found
    Switch StringRegExp($sWordList, '\b' & GUICtrlRead($input) & '\b')
        Case True
            ;MsgBox(Default, Default, "Word nOk!")
            GUICtrlSetData($ErrLabel, GUICtrlRead($ErrLabel) + 1)
        Case False
            $sWordList &= @crlf & guictrlread($input)
            Filedelete(@ScriptDir & '\phraselist.txt')
            FileWrite(@ScriptDir & '\phraselist.txt', $sWordList)
           ;MsgBox(Default, Default, "Word OK!")
            GUICtrlSetData($OkLabel, GUICtrlRead($OkLabel) + 1)
    EndSwitch



    GUICtrlSetData($input, "")          ;   blank out input control
    GUICtrlSetState($input,$GUI_FOCUS)  ;   force focus to input control

EndFunc   ;==>Button1Click

Func Form1Close()
    Exit
EndFunc   ;==>Form1Close

#Region Spell Checking

Func Deletion($sWord)
    $iCount = StringLen($sWord)
    Local $aResults[$iCount]
    For $i = 0 To $iCount - 1
        $aResults[$i] = StringMid($sWord, 1, $i) & StringMid($sWord, $i + 2)
    Next
    Return $aResults
EndFunc

Func Transposition($sWord)
    $iCount = StringLen($sWord) - 1
    Local $aResults[$iCount]
    For $i = 1 To $iCount
        $aResults[$i - 1] = StringMid($sWord, 1, $i - 1) & StringMid($sWord, $i + 1, 1) & StringMid($sWord, $i, 1) & StringMid($sWord, $i + 2)
    Next
    Return $aResults
EndFunc

Func Alteration($sWord)
    Local $sAlphabet = "abcdefghijklmnopqrstuvwxyz"
    Local $iCount = StringLen($sWord)
    Local $aResults[26 * $iCount]
    Local $iIndex = -1
    For $j = 1 To 26
        For $i = 0 To $iCount - 1
            $iIndex += 1
            $aResults[$iIndex] = StringMid($sWord, 1, $i) & StringMid($sAlphabet, $j, 1) & StringMid($sWord, $i + 2)
        Next
    Next
    Return $aResults
EndFunc

Func Insertion($sWord)
    Local $sAlphabet = "abcdefghijklmnopqrstuvwxyz"
    Local $iCount = StringLen($sWord) + 1
    Local $aResults[26 * $iCount]
    Local $iIndex = -1
    For $j = 1 To 26
        For $i = 0 To $iCount - 1
            $iIndex += 1
            $aResults[$iIndex] = StringMid($sWord, 1, $i) & StringMid($sAlphabet, $j, 1) & StringMid($sWord, $i + 1)
        Next
    Next
    Return $aResults
EndFunc

Func Words($sText)
    ; Function to extract words from longer text
    ; At the moment we are only using 1 word
EndFunc

Func Read($sFile)
    Local $aList
    _FileReadToArray($sFile, $aList)
    _ArrayDelete($aList, 0)
    Return $aList
EndFunc

Func Alternatives($sWord)
    Local $aAlternatives = Deletion($sWord)
    Local $aArray = Transposition($sWord)
    _ArrayConcatenate($aAlternatives, $aArray)
    $aArray = Alteration($sWord)
    _ArrayConcatenate($aAlternatives, $aArray)
    $aArray = Insertion($sWord)
    _ArrayConcatenate($aAlternatives, $aArray)
    Return $aAlternatives
EndFunc

Func Known($sWord, ByRef $aList)

    $iIndex = _ArrayBinarySearch($aList, $sWord)
    If $iIndex <> -1 Then
        Return $aList[$iIndex]
    Else
        Return ""
    EndIf

EndFunc

Func Correct($sWord)
    Local $aList = Read($sList)
    ; look for the word itself
    $sFound = Known($sWord, $aList)
    If $sFound Then
        Return $sFound
    EndIf
    ; Look for alternatives to the word
    $sAllFound = ""
    $aAlternatives = Alternatives($sWord)
    $iCount = UBound($aAlternatives) - 1
    For $i = 0 To $iCount
        $sFound = Known($aAlternatives[$i], $aList)
        If $sFound Then
            $sAllFound &= $sFound & @CRLF
        EndIf
    Next
    If $sAllFound Then
        Return $sAllFound
    EndIf
    ; Look for second order alternatives
    For $j = 0 To UBound($aAlternatives) - 1
        ConsoleWrite($j & @CRLF)
        $aSecAlternatives = Alternatives($aAlternatives[$j])
        $iCount = UBound($aSecAlternatives) - 1
        For $i = 0 To $iCount
            $sFound = Known($aSecAlternatives[$i], $aList)
            If $sFound Then
                $sAllFound &= $sFound & @CRLF
            EndIf
        Next
    Next
    If $sAllFound Then
        Return $sAllFound
    EndIf
    Return ""
EndFunc
#EndRegion Spell Checking

Thank you!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...