Skip to content
This repository has been archived by the owner on Jul 4, 2021. It is now read-only.

Commit

Permalink
fixed #3
Browse files Browse the repository at this point in the history
  • Loading branch information
mobergmann committed Aug 1, 2020
1 parent cc5ecf1 commit 2e2d2a6
Showing 1 changed file with 59 additions and 11 deletions.
70 changes: 59 additions & 11 deletions synonym-finder/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
using HtmlAgilityPack;

namespace synonym_finder
{

/// <summary>
/// A set of all symbold in the german alphabet.
/// </summary>
struct SearchResult
{
public string word;
public string link;
}

class Program
{
/// <summary>
/// A set of all symbold in the german alphabet.
/// </summary>
private static char[] germanAlphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','ä','ö','ü'};

/// <summary>
/// The base URL, which opens a page of a appended word.
/// </summary>
private static readonly string url_base = @"https://www.duden.de";

/// <summary>
/// The base URL, which opens a page of a appended word.
/// </summary>
Expand Down Expand Up @@ -85,12 +101,20 @@ static void Main(string[] args)
// make a duden search
doc = GetDoc(url_search + input);

#region handle search results

// holds all search results
List<SearchResult> searchResults = new List<SearchResult>();

// get all search results
HtmlNodeCollection collection_list = doc.DocumentNode.SelectNodes("//section[@class='vignette']/h2/a/strong");
// HtmlNodeCollection collection_list = doc.DocumentNode.SelectNodes("//section[@class='vignette']/h2/a/strong");

HtmlNodeCollection tmp_collection_1 = doc.DocumentNode.SelectNodes("//section[@class='vignette']/h2/a");

// if no results were found stop the programm
if (collection_list == null)
// if no results were found ask for new word
if (tmp_collection_1 == null)
{
// inform user
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\"{input}\" ist nicht im Duden verzeichnet.");
Console.ResetColor();
Expand All @@ -99,25 +123,47 @@ static void Main(string[] args)
Console.WriteLine();
continue;
}

// extract search results
foreach (HtmlNode node in tmp_collection_1)
{
SearchResult result = new SearchResult
{
word = node.ChildNodes[1].InnerText, // get second child of the a tag (which is the <strong>), which holds the name of the word
link = url_base + node.GetAttributeValue("href", string.Empty)
};

searchResults.Add(result);
}

#endregion

#region print search results

// inform the user
Console.WriteLine($"\"{input}\" wurde nicht gefunden, meinten sie: ");

// print the search results with an integer for selection
for (int i = 0; i < collection_list.Count; i++)
for (int i = 0; i < searchResults.Count; i++)
{
Console.WriteLine($"{ i }: { ReduceString(collection_list[i].InnerText) }");
Console.WriteLine($"{ i }: { ReduceString(searchResults[i].word) }");
}

// choose no word
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine($"{collection_list.Count}: Keine Eingabe");
Console.WriteLine($"{tmp_collection_1.Count}: Keine Eingabe");
Console.ResetColor();

#endregion

#region get user choice

string userChoiceNotParsed = String.Empty;
int userChoice;

Console.Write("Wählen sie einen Eintrag durch seine entsprechende Nummer: ");
// as long as the user doesn't input a valid number, ask him to do so
REPEAT:
// as long as the user doesn't input a valid number, ask him to do so
REPEAT:
{
userChoiceNotParsed = Console.ReadLine();
Console.WriteLine();
Expand All @@ -135,7 +181,7 @@ static void Main(string[] args)
userChoice = Int32.Parse(userChoiceNotParsed);

// check if Choice is in bounds of available options
if (userChoice > collection_list.Count || userChoice < 0)
if (userChoice > tmp_collection_1.Count || userChoice < 0)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("Bitte geben sie eine Zahl an, welche auch repräsentiert ist.");
Expand All @@ -145,7 +191,7 @@ static void Main(string[] args)
}
}

if (userChoice == collection_list.Count)
if (userChoice == tmp_collection_1.Count)
{
// print an empty Line
Console.WriteLine();
Expand All @@ -154,8 +200,10 @@ static void Main(string[] args)
else
{
// get the html document of the given text
doc = GetDoc(url_get + ReduceString(collection_list[userChoice].InnerText));
doc = GetDoc(url_get + ReduceString(tmp_collection_1[userChoice].InnerText));
}

#endregion
}

// get all synonyms
Expand Down

0 comments on commit 2e2d2a6

Please sign in to comment.