0

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

0

2 Answers 2

2

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");
}
Sign up to request clarification or add additional context in comments.

9 Comments

Good solution but never control the flow by throwing an exception. that is bad practice. instead, either return a bool in a Tuple perhaps or a custom Response object which contains a Success and value result or in this case, perhaps show a MessageBox that it was not found (if its a winforms app)
Who said I was controlling the flow using an exception? Perhaps in this case if it is not found, an exception is needed. Perhaps it isn't and he can return a blank string. And no point am I assuming flow, it's a simple example.
@Ahmedilyas: Everything you state is terrible advice for a C# app. My only complaint is that @Moo-Juice could use TryGetValue and avoid the double look-up.
Terrible advice? LOL.... ok. sorry you completely did not read my answer or understand what im saying. nevermind... so sorry to have stepped over your toes. I should be so punished and locked up in prison. dear oh dear the nerve of me!
but isnt using DictionaryBase defeating the point of the csv file? The csv file IS the dictionary :/
|
-1

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.

4 Comments

Uhm, you do know a dictionary is somewhat better for this? Why have a list of KeyValuePair<> ?
@Moo-Juice - I explained why buddy :)
If there are duplicate words (duplicate input words) you have no way of knowing which is correct. Your code just takes the first, but if there are duplicates - then the others are ignored anyway.
Right - that's my point. And I said, a very basic example. If you want to go to the nth degree then return back a collection with "possible matches" and let the user choose/select or show it on the screen for them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.