I am continuing my project to create a function that corrects grammar, but it seems that every time I run it, it generates an ArgumentError.
My code:
function grmr(s)
ws = split(s, " ")
ss = ""
for i in 1:length(ws)
if parse(Int, ws[i])
ws[i+1] = ItemQuantity(parse(Int, ws[i]), ws[i+1])
ws[i] = ""
elseif ws[i] in ["many", "multiple", "numerous", "several", "few", "some"]
ws[i+1] = pluralize(singularize(ws[i+1]))
elseif ws[i] in ["single", "a", "an"]
ws[i+1] = singularize(ws[i+1])
elseif split(ws[i])[end] == "s"
ws[i] = pluralize(singularize(ws[i]))
elseif ws[i] in ["a", "an"]
ws[i] = indefinite(ws[i+1])
end
ss = string(ss, ws[i], " ")
end
return ss
end
I tried grmr("I have a apple") and grmr("My favorite is a apple") to make sure it corrects it to "I have an apple" and "My favorite is an apple", but both cases came up with an error like this:
ArgumentError: invalid base 10 digit 'I' in "I"
Stacktrace:
[1] tryparse_internal(#unused#::Type{Int64}, s::SubString{String}, startpos::Int64, endpos::Int64, base_::Int64, raise::Bool)
@ Base .\parse.jl:137
[2] parse(::Type{Int64}, s::SubString{String}; base::Nothing)
@ Base .\parse.jl:241
[3] parse
@ .\parse.jl:240 [inlined]
[4] grmr(s::String)
@ Main .\REPL[2]:5
[5] top-level scope
@ REPL[3]:1
For the "My favorite is a apple" one, it is the same error except the beginning is invalid base 10 digit "M" in "My", so I realized it generated an error for the first character of the first word.
By the way, I'm using the EnglishText package, and yes, it is imported.
At the top of the loop you are calling
parse(Int, "I")which produces the error you see.The fact that you have written
if parse(Int, ws[1]suggests to me that you were only looking to parse the string in certain situations, however theparsecall will always be evaluated in theifclause.Maybe you were looking for a
try ... catchblock to handle the error? Alternatively you could look into thetryparsefunction to do something likeif typeof(tryparse(Int, ws[i])) == Intwhich will evaluate tofalseif parsing fails rather than erroring.