-2

I wrote a exam last week and had a really hard task to solve and didn't got the point. I had a .txt with a Text.

The Text is like this:

Der zerbrochne Krug, ein Lustspiel,

von Heinrich von Kleist.

Berlin. In der Realschulbuchhandlung. 1811.

[8] PERSONEN.

WALTER, Gerichtsrath. ADAM, Dorfrichter. LICHT, Schreiber. FRAU MARTHE RULL. EVE, ihre Tochter. VEIT TÜMPEL, ein Bauer. RUPRECHT, sein Sohn. FRAU BRIGITTE. EIN BEDIENTER, BÜTTEL, MÄGDE, etc.

Die Handlung spielt in einem niederländischen Dorfe bei Utrecht.

[9] Scene: Die Gerichtsstube. Erster Auftritt.

And i got the Main with this code:

var document = new Document("Text.txt");

            if (document.Contains("Haus") == true)
                Console.WriteLine(document["Haus"]); // Word: haus, Frequency.: 36, Length: 4
            else
                Console.WriteLine("Word not found!");

Now i had to write a class which helps to make the code above works. Does anyone have an idea how to solve this problem and would help a young student of business informatics to understand, how this works? Normally the StreamReader is easy for me, but in this case it wasn't possible for me...

Thank you very much and much love and healthy for all of you, who tries tohelpme.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • If you split this up it's not that hard anymore. ```Document``` is a new class which - in the constructor - reads the content of the file. It uses a structure like a ```Dictionary``` with a string as the key and an int as the value to count how often a word occurs. It then uses a string indexer on the ```Document``` class which returns the ```Value``` Property of the dictionary based on the given key, in this case Haus. – devsmn Jun 10 '20 at 12:26

4 Answers4

0

Try the below function :

 private bool FindWord( string SearchWord)
        {
            List<string> LstWords = new List<string>();
            string[] Lines = File.ReadAllLines("Path of your File");
            foreach (string line in Lines )
            {
                string[] words = line.Split(' ');
                foreach (string  word in words )
                {
                    LstWords.Add(word);
                }
            }
            // Find word set word to upper letters and target word to upper  
            int index = LstWords.FindIndex(x => x.Trim ().ToUpper ().Equals(SearchWord.ToUpper ()));

            if (index==-1)
            {
                // Not Found
                return false;
            }
            else
            {
                //word found 
                return true;
            }         
        }
Isaac Be
  • 81
  • 1
  • 10
  • Thank you. I will try to use some lines of it. But i had to stay in the code, which is fixed by my prof, like in the question – UnlexsPlayz Jun 10 '20 at 12:51
0

I find that Regex could be a good way to solve this:

        var ms = Regex.Matches(textToSearch, wordToFind, RegexOptions.IgnoreCase);
        if (ms.Count > 0)
        {
            Console.WriteLine($"Word: {wordToFind} Frequency: {ms.Count} Length: {wordToFind.Length}");
        }
        else
        {
            Console.WriteLine("Word not found!");
        }

Regex is in the namespace:

using System.Text.RegularExpressions;

You will need to set the RegexOptions that are appropriate for your problem.

Barns
  • 4,850
  • 3
  • 17
  • 31
0

Well this is the class you are looking for, hope this might help you.

class Document : Dictionary<string, int>
{
    private const char WORDSPLITTER = ' ';
    public string Filename { get; }

    public Document(string filename)
    {
        Filename = filename;
        Fill();
    }

    private void Fill()
    {
        foreach (var item in File.ReadLines(Filename))
        {
            foreach (var word in item.Split(WORDSPLITTER))
            {
                if (ContainsKey(word))
                    base[word] += 1;
                else
                    Add(word, 1);
            }
        }
    }

    public bool Contains(string word) => ContainsKey(word);

    public new string this[string word]
    {
        get
        {
            if (ContainsKey(word))
                return $"Word: {word}, frequency: {base[word]}, Length: {word.Length}";
            else
                return $"Word {word} not found!";

        }
    }
}
neelesh bodgal
  • 632
  • 5
  • 14
0

One of the approach would be below steps-

  1. Create a class Document with below properties -

       //Contains file name
        public string FileName { get; set; }
        //Contains file data
        public string FileData { get; set; }
        //Contains word count
        public int WordCount { get; set; }
        //Holds all the words
        public Dictionary<string, int> DictWords { get; set; } = new Dictionary<string, int>();
    
  2. Define the constructor which does 2 things -

    1. Assign the property Filename to incoming file
    2. Read the file from the path and get all the words from the file
    3. Find the word count and insert them to dictionary, so the Final dictionary will have all the <<<'word'>>, <<'TotalCount'>>> records

      //Constructor
      public Document(string fileName)
      {
      //1/ Assign File Name name troperty
      FileName = fileName;
      
      //2. Read File from the Path
      string text = System.IO.File.ReadAllText(fileName, Encoding.Default);
      string[] source = text.Split(new char[] { '.', '!', '?', ',', '(', ')', '\t', '\n', '\r', ' ' }, 
          StringSplitOptions.RemoveEmptyEntries);
      
      //3. Add the counts to Dictionary
      foreach (String word in source)
      {
          if (DictWords.ContainsKey(word))
          {
              DictWords[word]++;
          } else
          {
              DictWords[word] = 1;
          }
      
      }
      

      }

  3. Create "Contains" method which will be used to check whether the word is present or not in the document-

       //4. Method will return true /false based on the existence of the key/word.
        public bool Contains(string word)
        {
            if (DictWords.ContainsKey(word))
            {
                return true;
            }
            else
            {
                return false;
            }
    
        }
    
  4. Create an indexer on string for the class to get the desired output to be print to
    Console -

        //4. Define index on the word.
        public string this[string word]
        {
            get
            {           
                if (DictWords.TryGetValue(word, out int value))
                {
                   return $"Word: {word}, Frequency.:{value}, Length: {word.Length}";                   
                }
    
                return string.Empty;
             }
        }
    

Tests :

           var document = new Document(@"Text.txt");
           if (document.Contains("BEDIENTER") == true)
                Console.WriteLine(document["BEDIENTER"]); 
            else
                Console.WriteLine("Word not found!");

           //Output
           // Word: BEDIENTER, Frequency.:1, Length: 9
MBB
  • 1,635
  • 3
  • 9
  • 19
  • DictWords[word] = 1; this line will throw not KeyNotFoundException exception. U need to add keys to dictionary if does not contain keys. – neelesh bodgal Jun 10 '20 at 14:29
  • @neeleshbodgal.. Ideally we don't need that else part, As we are reading the same file! – MBB Jun 10 '20 at 14:34
  • You don't add the words to dictionary anywhere in the post. How do you say else is not required – neelesh bodgal Jun 10 '20 at 14:37
  • Hey there. Thank you for your help. I tried to use your code to understand. But in this case it doesn't find a word. Always says "Word not found" – UnlexsPlayz Jun 10 '20 at 14:45
  • 1
    @neeleshbodgal what I meant was that else part will ad key if it is not present. https://stackoverflow.com/questions/1838476/different-ways-of-adding-to-dictionary/1838494#1838494 – MBB Jun 10 '20 at 14:46
  • @UnlexsPlayz. I tested this if the word is present in text file. It will print the output you wanted! can you check thee file has those words? – MBB Jun 10 '20 at 14:50
  • @mahesh_b thats quite interesting, never tried it before. – neelesh bodgal Jun 10 '20 at 14:54
  • Coding always surprises me :) – MBB Jun 10 '20 at 14:55