【问题标题】:Make a Dictionary in Batch批量制作字典
【发布时间】:2017-03-12 15:11:06
【问题描述】:

我正在尝试制作一个批处理程序,当输入一个单词(例如“编码”)时,输出其对应的定义。

我尝试过的:

1) 正在下载所有可接受的英语单词的列表,但我找不到任何包含定义的列表。

2) 我有一个想法,由于知识不足,我无法尝试。当在 Google.com 上搜索this 时,会出现一个小框,其中包含“编码”一词的简明定义。我想知道是否可以使用某种方法从批处理文件中搜索 Google 并输出定义(第一个标记为 1.)。

到目前为止,我首先尝试了以下代码...

@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));

...是的,我需要它是 %SendKeys% 而不是 echo。所以你可能从上面的代码中可以看出,所有这些都是搜索所有英文单词的列表(一个不包含定义的文件)并输出它是否存在,而我需要的是它输出定义.

至于我的第二个想法,我不知道从哪里开始,并尝试搜索如何从网站获取变量但无济于事。所以,我认为最好的选择是包含所有接受的单词及其定义的文件(例如,见下文)并使用token 获取文件中变量%word% 之后的所有标记。

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...

谢谢!

【问题讨论】:

  • 是的,一定要尝试从网上获取信息。如果必须,请解析 cURL 输出(尽管这不是必需的)。包含英语中的每个单词及其定义的文件将非常大。千兆字节。

标签: database file batch-file dictionary


【解决方案1】:

尝试使用online dictionary provider,例如:

@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

示例输出:

输入单词:示例
一个实例(作为要解决的问题)用于说明规则或戒律,或作为应用规则的练习。



注意:为了下载定义,我使用了powershell,可能没有安装在您的系统上,最好使用第三方解决方案,例如aria2。使用aria2 的脚本是

@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

【讨论】:

  • 我很确定我有 powershell(当我搜索它时会出现)但它不起作用?
  • @R2bEEaton_ 下载 github.com/aria2/aria2/releases/download/release-1.31.0/… 并从 ZIP 中提取 aria2.exe。将其放在与批处理文件相同的目录中,并使用我提供的第二个脚本
  • 完美运行,除了在set /p definition=&lt;t.tmp 之前需要ping localhost -n 1 &gt;nul 以留出足够的时间下载。谢谢!
  • @R2bEEaton_ 你应该不需要任何间隔,因为它是调用下载命令,这两个命令都只有在下载完成后退出
  • 嗯,好的。但是,它工作正常,除了每当 %definition% 包含撇号时,它会用类似 ' 的东西替换它。 “我的”的定义显然是lay mines; &amp;quot;The Vietnamese mined Cambodia&amp;quot;,而“美元”的定义是Imported from the United States, and paid for in U.S. dollars. Note: distinguish &amp;quot;do。此外,有时我会在句子的结尾和开头抛出奇怪的字符,例如&lt;i&gt; [-n] -i,事物 t>Type :Di [then a word] 除了小写 D 来获得它的定义!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 2018-04-04
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多