Jump to content

Dictionary Help


Recommended Posts

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
Link to comment
Share on other sites

i remember a similar topic....

Link to comment
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!"

Link to comment
Share on other sites

#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
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...