Bailey Miller Bailey Miller - 1 month ago 9
C# Question

C# LINQ search of list, similar to how a database works

I have never used LINQ before I have only heard of it and I believe this is what I am looking for. I was given a text file with english words matched to spanish words. I have to take an english input, for example the aunt and match it to the spanish value la tia. If I run through the text file and create an array or list of word objects that store a category, english word, and spanish word. How can I go about doing something like this for example in C# code.

Find spanishValue from WordList where englishValue is like aunt; This example needs to account for the fact they may omit the 'the' or incorrectly spell the english word. I could do something like this in a database but how so in code.

My List:

List<Word> Words = new List<Word>();


My Word.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TranslatorLibrary
{
public class Word
{
public String English { get; set; }
public String Spanish { get; set; }

public CategoryEnumerator Category { get; set; }
public Word(String en, String sp, CategoryEnumerator cat)
{
English = en;
Spanish = sp;
Category = cat;
}

public static CategoryEnumerator matchCategory(String obj)
{
switch (obj)
{
case "Family":
return CategoryEnumerator.Family;
default:
return CategoryEnumerator.Family;
}
}


}
}

Answer

Well, there is a lot more to translating that just pure words, so my answer will not discuss semantics. Pure replacement only.

I think you think you can use LINQ here because it looks like SQL queries only that in code and you have done this in SQL before, right?

Anyways, LINQ is not the answer. LINQ capabilities are far too extensive to explain in this stage. Try following a tutorial instead. Suffice it to say it's not what you need to use here.

There are a many ways to accomplish your task. Here are two that I can think of off the top of my head:

(These both assume you have already parsed the word definition text file and built a dictionary of words in memory or that you are able to query the file on demand)

  1. Use a regular expression to find all the words, then loop through them and get the corresponding translation from the word dictionary, OR
  2. Write a lexical analyzer to tokenize the text in question. You can then go through the tokens and translate them as necessary.

I would suggest number 2, simply because it's something you can build upon if you wish to consider other things like semantics at some point.

The way a lexical analyzer works is that it will go through the text to translate and it will parse it, creating tokens for entities it recognizes. You can then go through these tokens and if you recognize it as a "word", you can query your dictionary and find the corresponding translation.

(this is how compilers work by the way)

Check this out: https://en.wikipedia.org/wiki/Lexical_analysis

I am sure there are other ways. This is just a quick answer.

Comments