compact21 Posted July 4, 2015 Share Posted July 4, 2015 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.expandcollapse popup#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 EndFuncThanks! Link to comment Share on other sites More sharing options...
mikell Posted July 4, 2015 Share Posted July 4, 2015 (edited) Use a booleanexpandcollapse popup#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 July 4, 2015 by mikell compact21 1 Link to comment Share on other sites More sharing options...
kylomas Posted July 4, 2015 Share Posted July 4, 2015 (edited) 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...expandcollapse popup#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 ;==>Form1CloseThis is the file I used for testing ->  wordlist.txtkylomas Edited July 4, 2015 by kylomas too many files inserted compact21 1 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 More sharing options...
compact21 Posted July 5, 2015 Author Share Posted July 5, 2015 (edited) thank you both.@kylomas That's great, thanks very much! Edited July 5, 2015 by compact21 Link to comment Share on other sites More sharing options...
compact21 Posted July 6, 2015 Author Share Posted July 6, 2015 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.expandcollapse popup#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 CheckingThank you! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now