Hello, basically, i currently have some open source code for text to speech translation (Type some text into a box and the software will have the computer say it back to you).
I need to tweak this code so that the system recognises certain key words (for example 'Fitness', 'Strength', 'Stamina', etc) in the input and then returns some pre-defined voice output.
For example if the key word in the input stage was 'Strength', the software would give "To improve general strength use weights in the gym.".
Does anyone have any idea how i'd go about this? Thanks.
Loading
Sean AndrewsPosted Jan 31, 2008, 5:32 PM
AlanPosted Jan 31, 2008, 4:13 PM
I'd integrate the code into your program like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions; // added this
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Exit1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Answer1_Click(object sender, EventArgs e) keywords = null; ();
{
string text = textBox1.Text; // get from first text box
string expression = @"\bfitness\b|\bstrength\b|\bstamina\b";
Regex r = new Regex(expression, RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(text);
List
if (mc.Count == 0)
{
// nothing extra to add
}
else
{
keywords = new List
foreach (Match m in mc)
{
keywords.Add(m.Value.ToLower());
}
if (keywords.Count > 1)
{
// remove any duplicates
keywords.Sort();
for (int i = keywords.Count - 1; i > 0; i--)
{
if (keywords[i] == keywords[i-1])
keywords.RemoveAt(i);
}
}
foreach(string keyword in keywords)
{
text += " " + GetExtra(keyword);
}
}
textBox2.Text = text; // output to second textbox
// code to deal with voice output
}
private string GetExtra(string keyword) // add helper method
{
switch(keyword)
{
case "fitness":
return "To improve general fitness run 3 miles a day.";
case "strength":
return "To improve general strength use weights in the gym.";
default: // stamina
return "To improve stamina do some aerobic activities.";
}
}
// other code or declarations
}
}
Sean AndrewsPosted Jan 31, 2008, 7:56 AM
I have created a basic windows for in visual studio (it's just buttons and a text box at the moment):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Exit1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Answer1_Click(object sender, EventArgs e)
{
}
}
}
This is what the form looks like:
http://img108.imageshack.us/img108/553/windowsformzl0.png
Where abouts do i put the code you have outlined above so that it will work? I want it to start when i click the Answer1 button, so I assume that it goes in the Answer1_Click section? I then want the answer to appear in the second text box.
Thanks, Sean.
AlanPosted Jan 24, 2008, 7:16 PM
Hi Sean,
No, the approach is perfectly general. The text can be anything you like and the regular expression can contain as many keywords as you like. I just had to put in some specific values so I could test the code.
Sean AndrewsPosted Jan 24, 2008, 6:33 PM
But i'm wondering if the lines:
string text = @"I don't have much stamina just now. How can I improve my fitness, strength or stamina?";
string expression = @"\bfitness\b|\bstrength\b|\bstamina\b";
Regex r = new Regex(expression, RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(text);
List
if (mc.Count == 0)
Does this limit it to that particular sentence?
What i have is a text box (name: textBox1) on the form where the user can enter whatever question they like, and the program must take this input and scan it for key words (of which they'll be many, but i think from the code you've provided i will be able to add in additional keywords) and then provide the relevant output.
The program will begin to scan the text once a button (answer1) is clicked.
AlanPosted Jan 23, 2008, 11:19 AM
I can't help you on the voice output but I may be able to help on identifying keywords in a piece of text and then adding the appropriate extra text to what is to be read back to the user.
The easiest way to find keywords is to use regular expressions. The following console program (.NET 2.0 or later) contains some basic code for finding one of the three keywords fitness, strength or stamina ignoring case. It looks for distinct words so it wouldn't regard 'strengths' as a match and it also eliminates any duplicate occurrences of these words, so you're not reading the same thing back twice or more. I hope it helps with what you're trying to do:
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
class Program keywords = null; ();
{
static void Main()
{
string text = @"I don't have much stamina just now. How can I improve my fitness, strength or stamina?";
string expression = @"\bfitness\b|\bstrength\b|\bstamina\b";
Regex r = new Regex(expression, RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(text);
List
if (mc.Count == 0)
{
// nothing extra to add
}
else
{
keywords = new List
foreach (Match m in mc)
{
keywords.Add(m.Value.ToLower());
}
if (keywords.Count > 1)
{
// remove any duplicates
keywords.Sort();
for (int i = keywords.Count - 1; i > 0; i--)
{
if (keywords[i] == keywords[i-1])
keywords.RemoveAt(i);
}
}
foreach(string keyword in keywords)
{
text += " " + GetExtra(keyword);
}
}
Console.WriteLine(text);
Console.ReadKey();
}
static string GetExtra(string keyword)
{
switch(keyword)
{
case "fitness":
return "To improve general fitness run 3 miles a day.";
case "strength":
return "To improve general strength use weights in the gym.";
default: // stamina
return "To improve stamina do some aerobic activities.";
}
}
}