Hi Everyone,
Do I have an asynchronous error in my code?
Can you please check my code? The logic of the code is to go through a list of words and call dictionary.com to see if they are nouns. I run the same code many time and most of the time I get the same number of nouns located but sometimes the number is lower or higher than expected.
Do I have an asynchronous error in my code?
Can you please check my code? The logic of the code is to go through a list of words and call dictionary.com to see if they are nouns. I run the same code many time and most of the time I get the same number of nouns located but sometimes the number is lower or higher than expected.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim hcNewStory As HttpClient
Dim req As HttpRequest
Dim intTotalRequestsToComplete As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
' Code using the HTTP library.
' Will be used to scrape an internet HTTP page from dictionary.com
'----------------------------------------------------------------
hcNewStory.Initialize("hcNewStory")
End Sub
Sub LookupNewStoryWord(strWordToLookup As String, intRequestNumber As Int)
' Calls dictionary.com with a word to lookup.
'--------------------------------------------
req.InitializeGet("http://dictionary.reference.com/browse/" & strWordToLookup)
hcNewStory.Execute(req, intRequestNumber)
End Sub
Sub hcNewStory_ResponseSuccess(Response As HttpResponse, TaskId As Int)
' This will scrape the web page returned and what part of speach the word is.
'----------------------------------------------------------------------------
Dim strEntireWebPage As String
Dim strSearchWord As String
Dim blnUseThisWord As Boolean
Dim strPartOfSpeach As String
strEntireWebPage = Response.GetString("UTF8") 'Get the whole page
strSearchWord = lstStory.Get(TaskId -1)
' The line below finds <div class="pbk"><span class="pg">
' strEntireWebPage = strEntireWebPage.SubString(strEntireWebPage.IndexOf("<div class=" & QUOTE & "pbk" & QUOTE & "><span class=" & QUOTE & "pg" & QUOTE & ">") + 34)
' Replace double quotes with single quotes for easy handling because I am using strings.
'---------------------------------------------------------------------------------------
strEntireWebPage = strEntireWebPage.Replace(QUOTE, "'")
' Trim down the entire web page so only the deffinition is left.
'---------------------------------------------------------------
strEntireWebPage = strEntireWebPage.SubString(strEntireWebPage.IndexOf("<div class='pbk'><span class='pg'>") + 34) 'Add 34 so we grab the definition after the tag
strEntireWebPage = strEntireWebPage.SubString2(0, strEntireWebPage.IndexOf("</span>")) 'Grab only the definition enclosed
' See if the word is a noun.
'---------------------------
If strEntireWebPage.ToUpperCase.Trim = "NOUN" Then
blnUseThisWord = True
If (strSearchWord.ToUpperCase = "A") _
OR (strSearchWord.ToUpperCase = "I") _
OR (strSearchWord.ToUpperCase = "IN") _
OR (strSearchWord.ToUpperCase = "OFF") _
OR (strSearchWord.ToUpperCase = "THE") _
OR (strSearchWord.ToUpperCase = "WAY") _
OR (strSearchWord.ToUpperCase = "AFTER") _
OR (strSearchWord.ToUpperCase = "WITH") _
OR (strSearchWord.IndexOf("ing") > 0) _
OR (strSearchWord.LastIndexOf("s") > 0) Then
blnUseThisWord = False
End If
Else
blnUseThisWord = False
End If
' Put the nouns into a list.
'---------------------------
If blnUseThisWord Then
ToastMessageShow("Processing *** " & lstStory.Get(TaskId -1) & " *** is a: " & strEntireWebPage.ToUpperCase, False)
lstNouns.Add(strSearchWord)
' ToastMessageShow("Noun Added", False)
End If
intTotalRequestsToComplete = intTotalRequestsToComplete - 1
' ToastMessageShow("Resqests left to complete: " & intTotalRequestsToComplete, False)
If intTotalRequestsToComplete = 0 Then
ProgressBarNewStory.Indeterminate = False
ProgressBarNewStory.Visible = False
If lstNouns.Size > 0 Then
GetFunnyReplacementWords
Else
Msgbox("Oops. I could not find any nouns. Please try again.", "Make it Funny")
End If
End If
End Sub