Categorizing string based on input in C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Title is like tl;dr version, here is what I mean:
I made currently just a string (to be a file with text), and I am splitting this string into separate words. I would like to make a method that allows me to mark words based on string/file. For example:
string nameOfString = "John likes pancakes";
Categorize(string nameOfString, class nameOfCategory)
this method would make John, likes and pancakes into a category (like Stupid, bestTexts) I passed to nameOfCategory.
I would like to count then the words into all of the categorys, so probably should use some kind of array top do this. Can someone help me with this? The big problem is I really have no idea how to pass the category (as a seperate class or just a string, maybe string[]?) and still be able to count it.

static void Main(string[] args)
{
var inputList = new List<string>
{
"John likes pancakes",
"John hates watching TV",
"I like my TV",
};
var dic = new Dictionary<string, int>();
inputList.ForEach(str => AddToDictionary(dic, str));
foreach (var entry in dic)
Console.WriteLine(entry.Key + ": " + entry.Value);
}
static void AddToDictionary(Dictionary<string, int> dictionary, string input)
{
input.Split(' ').ToList().ForEach(n =>
{
if (dictionary.ContainsKey(n))
dictionary[n]++;
else
dictionary.Add(n, 1);
});
}

Related

I want to fill a dictionary<string,int> with data from a txt file. The file contains names and numbers this way: Peter;123 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What i want to do is to use something like this Console.Writeline($"Name {Key} - Points: {Value}");
but i dont know how to make the data from the txt into Keys and Values.
I see, you want to fill a Dictionary<string, int> with the names and points of people, right?
If yes then I would suggest never use a dictionary Keys for names, because names are the data that can be duplicate which will eventually lead your software to throw exceptions regarding that.
Make a Generic List of custom class data. Something like this:
public class UserPoint
{
public string Name { get; set; }
public int Points { get; set; }
}
Then like below you can read your text file and load the data into the Generic List.
public static void Main(string[] args)
{
var lines = System.IO.File.ReadAllLines(#"myfile.txt"); // I assume each user info is separated by a newline.
var pointsList = new List<UserPoint>();
foreach(var line in lines)
{
var splittedLine = line.Split(';');
if(splittedLine.Length < 1) continue;
var userPoint = new UserPoint { Name = splittedLine[0].Trim() };
if(splittedLine.Length > 1 && int.TryParse(splittedLine[1].Trim(), out var points)) // updated the direct conversion to safe conversion with tryparse.
{
userPoint.Points = points; // To get rid of extra parsing you can also keep Points property as string.
}
pointsList.Add(userPoint);
}
}
PS: There are multiple other ways to read the text file but I used a simple way, You can also use FileStream and StreamReader classes if you like. Read more on reading text files on these MSDN Articles.
EDIT:
I notice you want to iterate and print through the list as well, you can do in the first loop or you can write a loop again. See:
foreach(var userPoint in pointsList)
{
Console.WriteLine($"Name: {userPoint.Name}, Points: {userPoint.Points}");
}
EDIT 2:
OP has asked to sort the list in descending order based on the points value. Here is a solution for that:
pointsList = pointsList.OrderByDescending(userPoint => userPoint.Points).ToList();

How To Translate Unicode Value To Emoji String in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to convert emoji's like the one below to c# code so I can put the code in a TreeView node or facebook or other social engine. I tried the code for airplane and shows a little airplane in the treenode. But I use another airplace code like U+1F6E9 it just shows a little rectangle not the emoji. Please help.
string tnt = "Airplane " + char.ConvertFromUtf32(int.Parse("U+2708".Substring(2), System.Globalization.NumberStyles.HexNumber));
MyTreeView.Nodes.Add(tnt);
Here is a class you may use to implement different emoji's in your application.
public class Emoji
{
readonly int[] codes;
public Emoji(int[] codes)
{
this.codes = codes;
}
public Emoji(int code)
{
codes = new int[] { code };
}
public override string ToString()
{
if (codes == null)
return string.Empty;
var sb = new StringBuilder(codes.Length);
foreach (var code in codes)
sb.Append(Char.ConvertFromUtf32(code));
return sb.ToString();
}
}
I would use the codes from this site unicode.org
Instead of using the code it has on the site which is something like 'U+1F366' I would use '0x1F366' to specify hexadecimal notation. Hope this helps.

How To Create Emoji Strings From Unicode Value? [duplicate]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to convert emoji's like the one below to c# code so I can put the code in a TreeView node or facebook or other social engine. I tried the code for airplane and shows a little airplane in the treenode. But I use another airplace code like U+1F6E9 it just shows a little rectangle not the emoji. Please help.
string tnt = "Airplane " + char.ConvertFromUtf32(int.Parse("U+2708".Substring(2), System.Globalization.NumberStyles.HexNumber));
MyTreeView.Nodes.Add(tnt);
Here is a class you may use to implement different emoji's in your application.
public class Emoji
{
readonly int[] codes;
public Emoji(int[] codes)
{
this.codes = codes;
}
public Emoji(int code)
{
codes = new int[] { code };
}
public override string ToString()
{
if (codes == null)
return string.Empty;
var sb = new StringBuilder(codes.Length);
foreach (var code in codes)
sb.Append(Char.ConvertFromUtf32(code));
return sb.ToString();
}
}
I would use the codes from this site unicode.org
Instead of using the code it has on the site which is something like 'U+1F366' I would use '0x1F366' to specify hexadecimal notation. Hope this helps.

C# Multi Spintax - Spin Text [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got this class that I'm using for spin text.
public class Spinner
{
private static Random rnd = new Random();
public static string Spin(string str)
{
string regex = #"\{(.*?)\}";
return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
}
public static string WordScrambler(Match match)
{
string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
return items[rnd.Next(items.Length)];
}
}
But I need it to be able to spin multi spintax text.
As example
{1|2} - {3|4}
Returns: 2 - 4
So it works.
But:
{{1|2}|{3|4}} - {{5|6}|{7|8}}
Returns: 2|4} - {5|7}
So it doesn't work if there is spintax inside spintax.
Any help? :)
Regular expressions are not good in dealing with nested structures, which means you should probably try a different approach.
In your example, {{1|2}|{3|4}} - {{5|6}|{7|8}} is the same as {1|2|3|4} - {5|6|7|8}, so maybe you don't need nested spintax.

Any utility to print out array directly? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array, I am wondering any utility to print out array directly?
You can use string's Join() method, like this:
Console.WriteLine("My array: {0}",
string.Join(", ", myArray.Select(v => v.ToString()))
);
This will print array elements converted to string, separated by ", ".
You can use the following one liner to print an array
int[] array = new int[] { 1 , 2 , 3 , 4 };
Array.ForEach( array , x => Console.WriteLine(x) );
I like #dasblinkenlight solution, but I'd like to note that the select statement is not nessasary.
This code produces the same result for an array of strings:
string[] myArray = {"String 1", "String 2", "More strings"};
Console.WriteLine("My array: {0}", string.Join(", ", myArray));
I find it a little easier on the eyes having less code to read.
(linqpad is a fantastic app to test snippets of code like this.)
You can write an extension method something like this
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
var items = new []{ 1, 2, 3, 4, 5 };
items.PrintArray();
}
}
static class ArrayExtensions
{
public static void PrintArray<T>(this IEnumerable<T> elements)
{
foreach (var element in elements)
{
Console.WriteLine(element);
}
}
}
}

Categories

Resources