import win.ui;
import win.ui.menu;
import string.database;
import win.clip;
/*创建窗口*/
var mainForm = win.form(text="英文文章挖空程序";right=600;bottom=450)
/*创建控件*/
mainForm.add(
edit={cls="richedit";left=10;top=10;right=590;bottom=200;edge=1;multiline=1;vscroll=1;wrap=1;z=1};
staticNumber={cls="static";text="挖空数量:";left=10;top=210;right=80;bottom=235;transparent=1;z=2};
editNumber={cls="edit";left=85;top=210;right=135;bottom=235;edge=1;num=1;z=3};
staticMinLength={cls="static";text="最小单词长度:";left=145;top=210;right=235;bottom=235;transparent=1;z=4};
editMinLength={cls="edit";left=240;top=210;right=290;bottom=235;edge=1;num=1;z=5};
chkIncludeApostrophe={cls="checkbox";text="包含带撇号单词";left=300;top=210;right=440;bottom=235;z=6};
btnGenerate={cls="button";text="生成挖空";left=450;top=210;right=590;bottom=240;z=7};
editResult={cls="richedit";left=10;top=250;right=590;bottom=390;edge=1;multiline=1;readonly=1;vscroll=1;wrap=1;z=8};
btnCopy={cls="button";text="复制结果";left=450;top=400;right=590;bottom=430;z=9};
btnClear={cls="button";text="清空";left=310;top=400;right=440;bottom=430;z=10}
)
/*生成挖空函数*/
generateCloze = function(text, num, includeApostrophe, minLength){
var words = string.split(text, " ");
var cloze = {};
var answers = {};
var availableIndices = {};
for(i=1;#words;1){
var pattern = includeApostrophe ? "^[a-zA-Z']+$" : "^[a-zA-Z]+$";
if(#words[i] >= minLength && string.match(words[i], pattern)){
table.push(availableIndices, i);
}
}
num = math.min(num, #availableIndices);
var selectedIndices = {};
for(i=1;num;1){
var index = math.random(1, #availableIndices);
table.push(selectedIndices, availableIndices[index]);
table.remove(availableIndices, index);
}
table.sort(selectedIndices);
for(i=1;#words;1){
if(table.find(selectedIndices, i)){
var count = table.find(selectedIndices, i);
answers[count] = words[i];
cloze[i] = string.format("%d_____", count);
} else {
cloze[i] = words[i];
}
}
return string.join(cloze, " "), answers;
}
/*按钮点击事件*/
mainForm.btnGenerate.oncommand = function(id,event){
var text = mainForm.edit.text;
var num = tonumber(mainForm.editNumber.text) or 5; // 默认为5个单词
var includeApostrophe = mainForm.chkIncludeApostrophe.checked;
var minLength = tonumber(mainForm.editMinLength.text) or 4; // 默认最小长度为4
var cloze, answers = generateCloze(text, num, includeApostrophe, minLength);
var resultText = cloze + '\n\n答案:\n';
for(i=1;#answers;1){
resultText = resultText + string.format("%d.%s ", i, answers[i]);
}
mainForm.editResult.text = resultText;
}
/*复制按钮点击事件*/
mainForm.btnCopy.oncommand = function(id,event){
win.clip.write(mainForm.editResult.text);
win.msgbox("结果已复制到剪贴板!", "提示");
}
/*清空按钮点击事件*/
mainForm.btnClear.oncommand = function(id,event){
mainForm.edit.text = "";
mainForm.editResult.text = "";
}
/*初始化*/
mainForm.editMinLength.text = "4";
mainForm.editNumber.text = "5";
/*显示窗口*/
mainForm.show();
/*运行消息循环*/
win.loopMessage();