Checking if a word exists
Using DictionaryLookup
seems to be more reliable, while not perfect:
DictionaryLookup[ {All, "word" }, IgnoreCase -> True ]
It seems to be rather comprehensive across different languages:
DictionaryLookup[ {All, "qt"}, IgnoreCase -> True ]
{{"Hungarian", "Qt"}}
Albeit, Mathematica will not find "qt" as abbreviation for Quantity (see for example Merriam Webster.
DictionaryLookup[{All, "colour"}, IgnoreCase -> True]
{{"BritishEnglish", "colour"}}
Also, it seems to be more comprehensive than WordList[]
:
DictionaryLookup[{"BritishEnglish", "colo" ~~ __}] // Select[StringLength@# > 3 &]
{colocynth,cologne,colon,colonel,colonelcy,colonels,colonial,colonialism,colonialist,colonially,colonials,colonic,colonies,colonisable,colonisation,colonisations,colonise,colonised,coloniser,colonisers,colonises,colonising,colonist,colonists,colonnade,colonnaded,colonnades,colons,colony,colophon,colophony,coloration,coloratura,colossal,colossally,colossi,colossus,colossuses,colostomy,colour,colourability,colourable,colourableness,colourably,colouration,colourcast,coloured,coloureds,colourer,colourers,colourfast,colourfastness,colourful,colourfully,colourfulness,colouring,colourings,colourisation,colourisations,colourise,colourises,colourist,colouristic,colourists,colourless,colourlessly,colourlessness,colours}
Length @ %
68
as opposed to 33 from WordList[]
, as given by OP.
Alternative to DictionaryWordQ
for English Words
Options[englishWordExistsQ] = {
IgnoreCase -> True
};
englishWordExistsQ[ word_String, opts : OptionsPattern[englishWordExistsQ] ] := Module[
{
optIgnoreCase = OptionValue[IgnoreCase],
results
},
results = Join[
DictionaryLookup[ {"BritishEnglish", word}, IgnoreCase -> optIgnoreCase],
DictionaryLookup[ {"English", word }, IgnoreCase -> optIgnoreCase ]
];
results =!= {}
]
Now:
englishWordExistsQ @ "colour"
True
Since gwr already answered the question, let me offer a few routines to check alternative dictionaries.
Merriam-Webster
Merriam-Webster provides an API for looking words up in their Collegiate® Dictionary. You will need to register to obtain an API key:
$MWAPIKey = (* insert your API key *);
MWLookup::suggest = "Word `1` not found. Returning a list of suggestions instead.";
MWLookup::noword = "Word `1` not found.";
MWLookup[word_String] := Module[{url, raw, check},
url = "https://www.dictionaryapi.com/api/v1/references/collegiate/xml/";
raw = Import[url <> URLEncode[ToLowerCase[word]] <> "?key=" <> $MWAPIKey, "XML"];
check = Cases[raw, XMLElement["entry", {"id" -> s_String}, rest_] :> s, ∞];
If[check =!= {}, check,
check = Cases[raw, XMLElement["suggestion", {}, {s_String}] :> s, ∞];
If[check =!= {}, Message[MWLookup::suggest, word]; check,
Message[MWLookup::noword, word]; check]]]
For example,
MWLookup["colour"]
{"colour"}
MWLookup["enfant terrible"]
{"enfant terrible"}
MWLookup["poiuyt"]
MWLookup::suggest: Word poiuyt not found. Returning a list of suggestions instead.
{"payout", "pouty", "pout", "Paiute", "Poitou", "peyote", "uppity", "potty", "poult",
"piety", "pity", "polit", "polity", "polite", "putty", "pilot", "opiate", "poet",
"puto", "pollute"}
.
MWLookup["qazwrk"]
MWLookup::noword: Word qazwrk not found.
{}
Oxford
The Oxford Dictionaries also provide an API, which you'll need to register for. Their system is a bit more complicated, since calling their API requires an API ID and an API key:
$OxfordAPIID = (* insert your API ID *);
$OxfordAPIKey = (* insert your API key *);
OxfordLookup::noword = "Word `1` not found.";
OxfordLookup[word_String] := Module[{url, w, raw, check},
url = "https://od-api.oxforddictionaries.com:443/api/v1/inflections/en/";
w = StringReplace[URLEncode[ToLowerCase[word]], "+" -> "%20"];
raw = Import[HTTPRequest[url <> w, <|"Method" -> "GET",
"Headers" -> {"app_id" -> $OxfordAPIID,
"app_key" -> $OxfordAPIKey}|>],
"String"];
check = Quiet[Check[ImportString[raw, "RawJSON"], ImportString[raw, "HTML"],
Import::jsonhintposandchar], Import::jsonhintposandchar];
If[check === "Not Found \nNo lemmas found matching supplied source_lang and word",
Message[OxfordLookup::noword, word]; Return[{}, Module]];
StringReplace[Through[check["results"]["id"]], "_" -> " "]]
For example,
OxfordLookup["colour"]
{"colour"}
OxfordLookup["enfant terrible"]
{"enfant terrible"}
OxfordLookup["poiuyt"]
MWLookup::noword: Word poiuyt not found.
{}
Oxford also provides an API for searching the Oxford English Dictionary, but the API is still in prototype stage, and still has some functionality (e.g. stemming) missing. Nevertheless, here is how to call the API from Mathematica:
OEDLookup::noword = "Word `1` not found.";
OEDLookup[word_String] := Module[{url, w, raw},
url = "https://oed-api-demo-2445581300291.apicast.io:443/oed/api/v0.0/words/?lemma=";
w = URLEncode[ToLowerCase[word]];
raw = Flatten[Import[HTTPRequest[url <> w, <|"Method" -> "GET",
"Headers" -> {"Accept" -> "application/json",
"app_id" -> $OxfordAPIID, "app_key" -> $OxfordAPIKey}|>],
"RawJSON"]];
If[raw === {}, Message[OEDLookup::noword, word]; {}, Through[raw["lemma"]]]]
For example:
OEDLookup["set"]
{"set", "set", "set", "set"}
OEDLookup["to and fro"]
{"to and fro"}