Sign in to follow this  
Followers 0
dantay9

Dictionary Help

5 posts in this topic

#1 ·  Posted (edited)

My dictionary script is almost done. I have almost everything set, but my script only seems to work for the first word. When I input another word, it doesn't work. Does anyone know why? I could also use some error handling for my definition function. Thanks in advance.

Dictionary.au3

Edited by dantay9

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites



i remember a similar topic....

Share this post


Link to post
Share on other sites

I had no problem looking up more than one word. I looked up fish then chocolate than pizza and they all came up fine. 


Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

Share this post


Link to post
Share on other sites

First of all there was no need to create a new object everytime you called the function. This works for me

;
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

Opt("TrayIconDebug", 1)
$IE = ObjCreate("InternetExplorer.Application")
If Not IsObj($IE) Then
   MsgBox(0, "ERROR", "Object was not created.")
   Exit
EndIf

$Main = GUICreate("English Dictionary", 600, 370)
$Edit = GUICtrlCreateEdit("", 15, 15, 570, 300, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
$Input = GUICtrlCreateInput("", 15, 330, 405, 25)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
$Search = GUICtrlCreateButton("Search", 435, 330, 150, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
GUISetState()

While 1
   $msg = GUIGetMsg()
   Switch $msg
      Case $Search
         $Definitions = Find_Definition(GUICtrlRead($Input))
         GUICtrlSetData($Edit, $Definitions)
      Case - 3
         $IE.quit
         $IE = 0
         
         Exit
   EndSwitch
WEnd

Func Find_Definition($Word)
   $IE.navigate("http://dictionary.reference.com/browse/" & $Word)
   While 1
      If $IE.readyState = "complete" Or $IE.readyState = 4 Or $IE.busy = False Then ExitLoop
      Sleep(200)
   WEnd
   Local $text = $IE.document.body.innertext
   If StringInStr($text, "Origin:") Then
      $text = StringRegExpReplace($text, "(?i)(?s)(.*?Show IPA)(.*?)(Origin:.*?)\z", "\2")
   ElseIf StringInStr($text, "Related forms:") Then
      $text = StringRegExpReplace($text, "(?i)(?s)(.*?Show IPA)(.*?)(Related forms:.*?)\z", "\2")
   ElseIf StringInStr($text, "Synonyms:") Then
      $text = StringRegExpReplace($text, "(?i)(?s)(.*?Show IPA)(.*?)(Synonyms:.*?)\z", "\2")
   Else
      $text = StringRegExpReplace($text, "(?i)(?s)(.*?Show IPA)(.*?)(Dictionary\.com Unabridged.*?)\z", "\2")
   EndIf
   $Array = StringRegExp($text, "(?:\A|\v)((?:(?:|-)+\w|\d+(?:\xB7|\.)|[a-zA-Z](?:\xB7|\.)).+?)\v", 3)
   
   Local $x = 0
   While 1
      If $x >= UBound($Array) Then ExitLoop
      If StringLeft($Array[$x], 2) = "b." Then
        ; SubItem Definitions
         Local $place = StringInStr($Array[$x - 1], "a.")
         Local $NewString = StringRight($Array[$x - 1], StringLen($Array[$x - 1]) - $place + 1)
         $Array[$x - 1] = StringLeft($Array[$x - 1], $place - 1)
         _ArrayInsert($Array, $x, $NewString)
         $x += 2
      ElseIf StringRegExp($Array[$x], "(\s{1}[1-9]+\.{1}\w)") Then
         If $x <> 0 Then
            _ArrayInsert($Array, $x)
            $x += 1
         EndIf
        ; Part of Speech
         Local $Numbers = StringRegExp($Array[$x], ".+\s([0-9]+)\.\w", 2)
         Local $place = StringInStr($Array[$x], " " & $Numbers[1] & ".")
         Local $NewString = StringRight($Array[$x], StringLen($Array[$x]) - $place + 1)
         $Array[$x] = StringLeft($Array[$x], $place - 1)
         _ArrayInsert($Array, $x + 1, $NewString)
         $x += 2
      Else
         $x += 1
      EndIf
   WEnd
   For $x = 0 To UBound($Array) - 1
      $Array[$x] = StringStripWS($Array[$x], 3)
   Next
   Return _ArrayToString($Array, @CRLF)
EndFunc  ;==>Find_Definition
;

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

#include <Array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <INet.au3>
#include <String.au3>
#include <WindowsConstants.au3>

Opt("TrayIconDebug", 1)

$Main = GUICreate("English Dictionary", 600, 370)
$Edit = GUICtrlCreateEdit("", 15, 15, 570, 300, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetFont(-1, 13, 400, 0, "Arial")
$Input = GUICtrlCreateInput("", 15, 330, 405, 25)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
$Search = GUICtrlCreateButton("Search", 435, 330, 150, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $Search
            $sDefinitions = _GetDifinitions(GUICtrlRead($Input))
            GUICtrlSetData($Edit, $sDefinitions)
        Case - 3
            Exit
    EndSwitch
WEnd


#cs
Func Find_Definition($Word)
    Local $IE = ObjCreate("InternetExplorer.Application")
    If Not IsObj($IE) Then
        MsgBox(0, "ERROR", "Object is not a variable.")
        Exit
    EndIf
    $IE.navigate("http://dictionary.reference.com/browse/" & $Word)
    While 1
        If $IE.readyState = "complete" Or $IE.readyState = 4 Or $IE.busy = False Then ExitLoop
        Sleep(200)
    WEnd
    Local $text = $IE.document.body.innertext
    If StringInStr($text, "Origin:") Then
        $text = StringRegExpReplace($text, "(?i)(?s)(.*?Show IPA)(.*?)(Origin:.*?)\z", "\2")
    ElseIf StringInStr($text, "Related forms:") Then
        $text = StringRegExpReplace($text, "(?i)(?s)(.*?Show IPA)(.*?)(Related forms:.*?)\z", "\2")
    ElseIf StringInStr($text, "Synonyms:") Then
        $text = StringRegExpReplace($text, "(?i)(?s)(.*?Show IPA)(.*?)(Synonyms:.*?)\z", "\2")
    Else
        $text = StringRegExpReplace($text, "(?i)(?s)(.*?Show IPA)(.*?)(Dictionary\.com Unabridged.*?)\z", "\2")
    EndIf
    $Array = StringRegExp($text, "(?:\A|\v)((?:(?:–|-)+\w|\d+(?:\xB7|\.)|[a-zA-Z](?:\xB7|\.)).+?)\v", 3)

    Local $x = 0
    While 1
        If $x >= UBound($Array) Then ExitLoop
        If StringLeft($Array[$x], 2) = "b." Then
            ; SubItem Definitions
            Local $place = StringInStr($Array[$x - 1], "a.")
            Local $NewString = StringRight($Array[$x - 1], StringLen($Array[$x - 1]) - $place + 1)
            $Array[$x - 1] = StringLeft($Array[$x - 1], $place - 1)
            _ArrayInsert($Array, $x, $NewString)
            $x += 2
        ElseIf StringRegExp($Array[$x], "(\s{1}[1-9]+\.{1}\w)") Then
            If $x <> 0 Then
                _ArrayInsert($Array, $x)
                $x += 1
            EndIf
            ; Part of Speech
            Local $Numbers = StringRegExp($Array[$x], ".+\s([0-9]+)\.\w", 2)
            Local $place = StringInStr($Array[$x], " " & $Numbers[1] & ".")
            Local $NewString = StringRight($Array[$x], StringLen($Array[$x]) - $place + 1)
            $Array[$x] = StringLeft($Array[$x], $place - 1)
            _ArrayInsert($Array, $x + 1, $NewString)
            $x += 2
        Else
            $x += 1
        EndIf
    WEnd
    For $x = 0 To UBound($Array) - 1
        $Array[$x] = StringStripWS($Array[$x], 3)
    Next
    $IE.quit
    $IE = 0
    Return _ArrayToString($Array, @CRLF)
EndFunc   ;==>Find_Definition
#ce

Func _GetDifinitions($sWord)
    
    If $sWord = '' Then Return ''
    
    Local $sFind = StringReplace($sWord, ' ', '+')
    Local $sRet, $sSrc
    Local $iUBound
    Local $aMatch, $aDefinition
    
    $sSrc = _INetGetSource('http://www.onelook.com/?w=' & $sFind & '&ls=b')
    If $sSrc = '' Or StringInStr($sSrc, 'Sorry, no dictionaries') Then Return 'Could not find definition to: ' & _
            _StringProper($sWord) & '.'
    
    $aMatch = StringRegExp($sSrc, '(?i)(?s)<center>quick definitions(.*?</td>)', 1)
    If Not IsArray($aMatch) Then Return 'No definitions found to the word: ' & _StringProper($sWord) & '.'
    
    $sRet = 'Quick definitions for: ' & _StringProper($sWord) & '.' & @CRLF & @CRLF
    
    $aMatch = StringRegExp($aMatch[0], '(?i)(?s)(<b>.*?<br>)', 3)
    If Not IsArray($aMatch) Then Return ''
    
    _ArrayDelete($aMatch, 0)
    
    For $i = 0 To UBound($aMatch)-1
        $aDefinition = StringRegExp($aMatch[$i], '(?i)<span[^>]+>([^<]*)', 3)
        $iUBound = UBound($aDefinition)
        
        If @error Then ContinueLoop     
        $sRet &= '--> ' & $aDefinition[0] & ': '
        
        Switch $iUBound
            Case 1
                $aDefinition = StringRegExp($aMatch[$i], '(?i)(?:&nbsp;)+([^<]+)', 1)
                If Not IsArray($aDefinition) Then ContinueLoop
                $sRet &= $aDefinition[0] & '.'
                
            Case 2
                $sRet &= $aDefinition[1] & '.'
                
            Case 3
                $sRet &= $aDefinition[1] & '.  ' & '(' & $aDefinition[2] & ')'
        EndSwitch
        
        $sRet &= @CRLF
    Next
    
    Return $sRet
EndFunc

Edited by Authenticity

Share this post


Link to post
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
Sign in to follow this  
Followers 0