2

I am attempting to make a Batch program that, when a word (eg. "coding") is inputted, its corresponding definition is outputted.

What I have tried:

1) Downloading lists of all acceptable English words, but I can't find any lists that include definitions.

2) I had an idea that I was unable to try because of insufficient knowledge. When this is searched on Google.com a little box comes up with a concise definition of the word "coding." I was wondering if some method could be used to search Google from a batch file and output the definition (the first one labeled with 1.).

So far, with the first thing that I tried I have come up with the following code...

@if (@CodeSection == @Batch) @then
set SendKeys=CScript //nologo //E:JScript "%~F0"

@echo off
findstr /m %word% dictionary.dic >nul
if %errorlevel%==0 (
%SendKeys% "t"
%SendKeys% ">%word% exists!"
%SendKeys% {ENTER}
ping localhost -n 4 >nul
goto home
)
%SendKeys% "t"
%SendKeys% ">%word% does not exist!"
%SendKeys% {ENTER}
ping localhost -n 4 >nul
goto home
)
@end
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

...and yes, I need it to be %SendKeys% not echo. So as you can probably tell from the above code, all this does is search a list of all English words (a file that doesn't include definitions) and output if it exists or not, while what I need is it to output the definition.

As for my second idea, I have no idea where to start and have tried searching how to get variables from websites to no avail. So, I think the best bet is a file containing all accepted words and their definitions (eg. See below) and to use token to get all tokens after the variable %word% in the file.

abacus, an oblong frame with rows of wires or grooves along which beads are slid, used for calculating.
abhorrent, inspiring disgust and loathing; repugnant.
etc...

Thanks!

R2bEEaton
  • 51
  • 1
  • 10
  • Try using a dictionary API http://stackoverflow.com/questions/11039178/is-there-any-free-online-dictionary-api-json-xml-with-multiple-languages-to-ch – Sam Denty Mar 12 '17 at 15:40
  • Yeah, definitely try and grab the information from online. Parse cURL output if you have to (although that shouldn't be necessary). A file containing every word in the English language and its definition would be extremely large. Gigabytes. – SomethingDark Mar 12 '17 at 15:51

1 Answers1

1

Try using an online dictionary provider, for example:

@echo off
set /p "word=Enter word: "
powershell -Command "(New-Object Net.WebClient).DownloadFile('http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=%word%&pretty=true', '%word%.txt')"
(type "%word%.txt"|findstr /c:""text"")>t.tmp
set /p definition=<t.tmp
del t.tmp >nul
del "%word%.txt" >nul
if not defined definition (echo No definition found for the word "%word%"&goto :end)
set "definition=%definition:~16%"
echo %definition%
:end
pause >nul

Example output:

Enter word: example
An instance (as a problem to be solved) serving to illustrate the rule or precept or to act as an exercise in the application of the rule.



NOTE: In order to download the definition, I've used powershell which might be not be installed on your system, it would be better to go with a third party solution such as aria2. The script for using aria2 would be

@echo off
set /p "word=Enter word: "
aria2 "http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=%word%&pretty=true" -o "%word%.txt" -q
(type "%word%.txt"|findstr /c:""text"")>t.tmp
set /p definition=<t.tmp
del t.tmp >nul
del "%word%.txt" >nul
if not defined definition (echo No definition found for the word "%word%"&goto :end)
set "definition=%definition:~16%"
echo %definition%
:end
pause >nul
Community
  • 1
  • 1
Sam Denty
  • 3,693
  • 3
  • 30
  • 43
  • I'm pretty sure I have powershell (it comes up when I search it) but it won't work? – R2bEEaton Mar 12 '17 at 16:12
  • @R2bEEaton_ Download https://github.com/aria2/aria2/releases/download/release-1.31.0/aria2-1.31.0-win-32bit-build1.zip and extract `aria2.exe` from the ZIP. Place it in the same directory as the batch file, and use the second script I provided – Sam Denty Mar 12 '17 at 16:20
  • Works perfectly, besides the fact that a `ping localhost -n 1 >nul` is needed before `set /p definition= – R2bEEaton Mar 12 '17 at 16:20
  • @R2bEEaton_ You shouldn't need any interval, because it's *calling* the download command, both of which only *exit* after the download is completed – Sam Denty Mar 12 '17 at 16:23
  • Ah OK. However, it's working fine except that whenever %definition% includes an apostrophe it replaces it with something like ' and the definition of "mine" is aparently `lay mines; "The Vietnamese mined Cambodia"` and the definition of "dollar" is `Imported from the United States, and paid for in U.S. dollars. Note: distinguish "do`. Also, sometimes I get weird characters thrown at the end and beginning of sentences like ` [-n] -i`, things t>Type :Di [then a word] except lower case D to get its definition! – R2bEEaton Mar 13 '17 at 22:03
  • ok, sorry I was editing my comment because I accidentally hit Enter, thanks I'll try that – R2bEEaton Mar 13 '17 at 22:08
  • When I remove `set "definition=%definition:~16%"` I get (for the definition of dollar) `text : Imported` as opposed to the previous `Imported from the United States, and paid for in U.S. dollars. Note: distinguish "do` – R2bEEaton Mar 13 '17 at 22:10
  • @R2bEEaton_ For example, see https://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=mine&pretty=true for the definition of `mine`. The batch file picks the first definition present and turns it into a variable. There is no way of getting the *correct* definition without using another dictionary provider – Sam Denty Mar 13 '17 at 22:16
  • Is there a way to detect if %definition% includes `'` and replace it with an `'`? – R2bEEaton Mar 13 '17 at 22:23
  • @R2bEEaton_ `set "definition=%definition:'='%"` – Sam Denty Mar 13 '17 at 22:33