A4L

TextUtilities

A4L
Apr 26th, 2020
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. using System;  
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace testAdventure
  8. {
  9.     static class TextUtils
  10.     {
  11.         static public EnglishPorter2Stemmer StemWord = new EnglishPorter2Stemmer();
  12.  
  13.         public static string[] TokenizeStringList(string input)
  14.         {
  15.             List<string> cleanedInputList = new List<string>();
  16.             string[] raw_cleanedInputString = input.ToLower().Trim().Split(null);
  17.  
  18.             foreach (string word in raw_cleanedInputString)
  19.             {
  20.                 if (word != "")
  21.                 {
  22.                     string s = CleanedWord(word);
  23.                     if (s != "") cleanedInputList.Add(s);
  24.                 }
  25.             }
  26.  
  27.             string[] copyList2String = new string[cleanedInputList.Count];
  28.             cleanedInputList.CopyTo(copyList2String);
  29.             return copyList2String;
  30.         }
  31.  
  32.         //Clean Unwanted Characters from a string
  33.         public static string CleanedWord(string word)
  34.         {
  35.             var banList = "~`!@#$%^&*()_+{}|[]\\:;\",<.>/?".ToCharArray();
  36.             return string.Join("", word.Where(s => !banList.Contains(s)));
  37.         }
  38.  
  39.         //Stemming string[] (Overloaded)
  40.         public static string[] StemWordList(string[] wordlist)
  41.         {
  42.             string[] copyArray = wordlist.ToArray();
  43.             for (int i = 0; i < copyArray.Length; i++)
  44.             {
  45.                 string StemValue = StemWord.Stem(copyArray[i]).Value;
  46.                 copyArray[i] = StemValue;
  47.             }
  48.             return copyArray;
  49.         }
  50.  
  51.         //Stemming List<string> (Overloaded)
  52.         public static List<string> StemWordList(List<string> wordlist)
  53.         {
  54.             List<string> copyArray = wordlist.ToList();
  55.             for (int i = 0; i < copyArray.Count; i++)
  56.             {
  57.                 string StemValue = StemWord.Stem(copyArray[i]).Value;
  58.                 copyArray[i] = StemValue;
  59.             }
  60.             return copyArray;
  61.         }
  62.  
  63.         public static string WordWrap(string text)
  64.         {
  65.             text = "      " + text;
  66.             string result = "";
  67.             int bufferWidth = Console.WindowWidth;
  68.             string[] lines = text.Split('\n');
  69.  
  70.             foreach (string line in lines)
  71.             {
  72.                 int linelength = 0;
  73.                 string[] words = line.Split(' ');
  74.  
  75.                 foreach (string word in words)
  76.                 {
  77.                     //if (word.Length + linelength >= bufferWidth - 1)
  78.                     if (word.Length + linelength >= bufferWidth - 7)
  79.                     {
  80.                         result += "\n      ";
  81.                         linelength = 0;
  82.                     }
  83.                     result += word + " ";
  84.                     linelength += word.Length + 1;
  85.                 }
  86.                 result += "\n";
  87.             }
  88.             return result;
  89.         }
  90.  
  91.         public static string WordWrap(List<string> text)
  92.         {
  93.             string result = "";
  94.  
  95.             foreach (string line in text)
  96.             {
  97.                 result = result + line;
  98.             }
  99.             return result;
  100.         }
  101.  
  102.         public static string FirstCharToUpper(this string input)
  103.         {
  104.             switch (input)
  105.             {
  106.                 case null: throw new ArgumentNullException(nameof(input));
  107.                 case "": throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input));
  108.                 default: return input.First().ToString().ToUpper() + input.Substring(1);
  109.             }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment