Created
October 29, 2017 13:52
-
-
Save taka2/70d126e55a9d02afa5a64b9aa1cc840f to your computer and use it in GitHub Desktop.
小学校1年生で習う繰上りありの足し算を延々と音声で解答するC#デスクトップアプリ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using System.Speech.Recognition; | |
namespace WindowsFormsApplication3 | |
{ | |
public partial class Form1 : Form | |
{ | |
private SpeechRecognitionEngine recognizer; | |
private int[] operand1 = new int[5]; | |
private int[] operand2 = new int[5]; | |
private Random r; | |
public Form1() | |
{ | |
InitializeComponent(); | |
// 問題データの初期化 | |
for (int i = 0; i < operand1.Length; i++) | |
{ | |
operand1[i] = i + 5; | |
operand2[i] = i + 5; | |
} | |
// 乱数の初期化 | |
r = new System.Random(); | |
} | |
private async void button1_Click(object sender, EventArgs e) | |
{ | |
try | |
{ | |
int op1 = operand1[r.Next(operand1.Length)]; | |
int op2 = operand2[r.Next(operand2.Length)]; | |
int answer = op1 + op2; | |
string question = op1 + " + " + op2 + " = "; | |
button1.Text = question; | |
// 音声認識の初期化 | |
recognizer = new SpeechRecognitionEngine(); | |
string[] strArray = new string[18]; | |
for (int i = 1; i <= strArray.Length; i++) | |
{ | |
strArray[i - 1] = i.ToString(); | |
} | |
Choices colorChoice = new Choices(strArray); | |
Grammar dictationGrammar = new Grammar(colorChoice); | |
recognizer.LoadGrammar(dictationGrammar); | |
recognizer.SetInputToDefaultAudioDevice(); | |
RecognitionResult result = recognizer.Recognize(); | |
if(result != null) { | |
if(answer.ToString().Equals(result.Text)) { | |
button1.Text = "正解! " + question + answer; | |
} else { | |
button1.Text = result.Text + "は、ちがうよ..." + question + answer; | |
} | |
} else { | |
button1.Text = "聞き取れなかったよ..."; | |
} | |
await Task.Delay(1000); | |
button1_Click(sender, e); | |
} | |
catch (InvalidOperationException exception) | |
{ | |
button1.Text = String.Format("Could not recognize input from default aduio device. Is a microphone or sound card available?\r\n{0} - {1}.", exception.Source, exception.Message); | |
} | |
finally | |
{ | |
recognizer.UnloadAllGrammars(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment