Reputation: 163
My aim is to make a web service that translates the users input (so just two text boxes and a button). I've have a dictionary as a .csv file with two columns separated by a semicolon (the first is the the word that the user inputs and the second is the translation). Im very new to c# and Ive pulled some code together but it isnt taking a single input its taking a whole list
[Empty]
EDIT for clarity
I'm trying to make it so that the user can input a work they want translated, click the button then the translation of that word shows up
Input = textbox Output = textbox
Upvotes: 0
Views: 252
Reputation: 38825
Firstly, it would be better to load the dictionary once and store it in a more suitable collection like Dictionary<K, V>
, where the input is the key and the output is the value.
Have a private member of the form:
private Dictionary<string, string> _dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // let's ignore case when comparing.
Load the dictionary once and only in your form load event:
using(var reader = new StreamReader(File.OpenRead(@"C:/dictionary.csv")))
{
while(!reader.EndOfStream)
{
string[] tokens = reader.ReadLine().Split(';');
_dictionary[tokens[0]] = tokens[1];
}
}
Translating a word is now as simple as:
public string Translate(string input)
{
string output;
if(_dictionary.TryGetValue(input, out output))
return output;
// Obviously you might not want to throw an exception in this basis example,
// you might just go return "ERROR". Up to you, but those requirements are
// beyond the scope of the question! :)
throw new Exception("Sinatra doesn't know this ditty");
}
Upvotes: 2
Reputation: 5822
you need to read the file in memory perhaps, and then search the collection/array for the value the user entered.
here is a VERY BASIC example:
List<KeyValuePair<string, string>> items = new List<KeyValuePair<string, string>>();
..
..
// some function called at startup to read the entire file in the collection
private void LoadData()
{
var reader = new StreamReader(File.OpenRead(@"C:\dictionary.csv"));
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
var kvp = new KeyValuePair<string, string>(values[0], values[1]);
items.Add(kvp);
}
}
private string SearchWord(string inputWord)
{
string returnValue = string.Empty;
foreach(var currentItem in items)
{
if (string.Equals(inputWord, currentItem, StringComparison.OrdinalIgnoreCase))
{
returnValue = currentItem.Value;
break;
}
}
return returnValue;
}
What's it doing? well we hold a global collection of items in a List. Each item in a list contains a key and an associated value. the key is the word to translate FROM and the value is the translated word.
When the app starts up for example, you call LoadData() to load the file into the collection.
when the user presses the button, you call "SearchResult" passing it the input value from the textbox. Then it will iterate through the collection to find the input value and if it finds it, it will return the translated word back to you, so you take that value and set it into another textbox for example.
again, very basic and simple.
I did not go for the Dictionary, but it is better, purely because I don't know your requirements well enough. But if you are sure that there are no duplicate words (keys) then you should use a dictionary instead of a List> like I have done.
Upvotes: -1