Jump to content

Dictionary


Recommended Posts

I have the basis of my dictionary finished, but there are certain words that crash the script (ter, tel). I can't figure out why. I also need an error handler. The errors I have currently don't cover very much.

Dictionary.au3

Edited by dantay9
Link to comment
Share on other sites

Lines 48 and 49:

$Source = _StringBetween($Source, '<td width="35" class="dnindex">1.</td> <td>', '<span class="sectionLabel">Synonyms:')
    $Text = StringSplit($Source[0], '<td width="35" class="dnindex">', 1)

Edit: The failure is at those lines, $Source is not an array so it errors out when trying to use $Source[0]

Edited by MrMitchell
Link to comment
Share on other sites

"ter" and "tel" ...so it looks like after all the processing of $Source, the following line fails (currently line 48):

$Source = _StringBetween($Source, '<td width="35" class="dnindex">1.</td> <td>', '<span class="sectionLabel">Synonyms:')

because all instances (2) of "<span class="sectionLabel">Synonyms:" come before your single instance of "<td width="35" class="dnindex">1.</td> <td>".

Just check out $Source before your code reaches that line.

Link to comment
Share on other sites

i have a bit of different opinion.

actually it is not that 'ter' is not found. 'ter' is found but can we alter the logic a little bit? may be it will drag the work too long. but here's my proposal.

  • Currenly Your logic retrieves the first shown meaning 's results
  • We could probably include all the variants
  • All the words do not end with synonyms shown so, we can terminate our search at
    $Source = _StringBetween($Source, '<td width="35" class="dnindex">1.</td> <td>', '<div class="result_copyright">')
    this can be even shown in footnote as copright disclaimer which will be helpful.
  • but of course, this requires bit more of coding as you will see there are more results, viz., test would show test1 test2 ,etc
Edited by rajeshontheweb
Link to comment
Share on other sites

this works well,

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

$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
    Switch GUIGetMsg()
        Case $Search
            GUICtrlSetData($Edit, "Looking up dictionary.com....")
            $Result = Look_Up(GUICtrlRead($Input))
            If $Result = -1 Then
                MsgBox(262192, "ERROR", "That word is not in the dictionary!")
                GUICtrlSetData($Edit, "")
            Else
                GUICtrlSetData($Edit, $Result)
            EndIf
        Case - 3
            Exit
    EndSwitch
WEnd

Func Look_Up($Word)
    If StringRegExp($Word, "[^a-zA-Z+]") = 1 Then Return -1
    $Website = "http://dictionary.reference.com/browse/" & $Word & "&search=search"
;~  http://dictionary.reference.com/dic?q=ter&search=search
    
    $Source = _INetGetSource($Website)
    
    $Test = _StringBetween($Source, '<div id="Heading" class="Heading">', '<i>')
    If Not @error And StringStripWS(StringLower($Test[0]), 3) = "no results found for" Then Return -1
    
    
    $CopyRight = _StringBetween($Source,'<div class="result_copyright">','<a')
    $CopyRight = @CRLF & StringReplace($CopyRight[0],"<br", @CRLF & @TAB)
    $CopyRight = StringReplace($CopyRight,"/>", " ")
    $CopyRight = StringReplace($CopyRight,"&copy;", "(c)")
    

    $Source = StringTrimLeft($Source, StringInStr($Source, '<span class="pg">') + 15)
;~  $Source = StringReplace($Source, '<div class="ety"> <b>Origin:', '<span class="sectionLabel">Synonyms:') i didnt use this..
    
    $string = _StringBetween($Source, ">", "</span>")
    $Line1 = $string[0]
    If StringInStr($Line1, ",") Then $Line1 = StringLeft($Line1, StringInStr($Line1, ",") - 1)
    $Line1 = StringRegExpReplace($Line1, "[^a-zA-Z]", "") & @CRLF

;~  $Source = _StringBetween($Source, '<td width="35" class="dnindex">1.</td> <td>', '<span class="sectionLabel">Synonyms:') ; doesnt work on words sans synonyms
;~  $Source = _StringBetween($Source, '<td width="35" class="dnindex">1.</td> <td>', '<div class="result_copyright">'); for full information needs working, i am no good at regex 
    $Source = _StringBetween($Source, '<td width="35" class="dnindex">1.</td> <td>', '<b>Origin: </b>')

    $Text = StringSplit($Source[0], '<td width="35" class="dnindex">', 1)
    $Text[1] = "1. " & $Text[1]

    For $n = 0 To $Text[0]
        $Text[$n] = StringRegExpReplace($Text[$n], "(<.*?>)", "")
    Next

    $Text = "1. " & StringReplace($Source[0], '<td width="35" class="dnindex">', @CRLF)

    $Text = StringRegExpReplace($Text, "(<.*?>)", "")
    $Text = "--" & $Line1 & @CRLF & $Text

    $Replace = StringReplace($Text, "â", "--")
    If $Replace Then $Text = $Replace
    $Array = StringSplit($Text, @CRLF)
    _ArrayDelete($Array, 0)
    $x = 0
    While 1
        If $x >= UBound($Array) - 1 Then ExitLoop
        If Not StringRegExp($Array[$x], '[^\w\p{P}\040]+') Then
            _ArrayDelete($Array, $x)
            $x -= 1
        EndIf
        $x += 1
    WEnd
    $Text = _ArrayToString($Array, @CRLF)
    $Text = StringReplace($Text, "--", @CRLF & @CRLF & "--")
    $Text = StringStripWS($Text, 3)
    Return $Text & @CRLF & $CopyRight
EndFunc   ;==>Look_Up

of Course, this doesnt retrieve variants but will get rid of the issue currently in hand. works with ter, tel, etc.

Edited by rajeshontheweb
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...