Created
July 31, 2014 08:01
-
-
Save yemrekeskin/520e5fda7d27859c659f to your computer and use it in GitHub Desktop.
Seo Helper Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Text.RegularExpressions; | |
| public static class SeoHelper | |
| { | |
| public static string[] GetKeywords(string input, bool cultureInvariant) | |
| { | |
| RegexOptions options = RegexOptions.CultureInvariant | RegexOptions.Compiled; | |
| if (!cultureInvariant) | |
| options = RegexOptions.Compiled; | |
| var matches = Regex.Matches(input, "[\\w]+", options); | |
| if (matches == null) | |
| return null; | |
| var segments = new string[matches.Count]; | |
| for (int i = 0; i < matches.Count; i++) | |
| { | |
| segments[i] = matches[i].Value; | |
| } | |
| return segments; | |
| } | |
| public static string MakeValidUrl(string input, bool cultureInvariant) | |
| { | |
| if (input == null) | |
| return null; | |
| var keywords = GetKeywords(input, cultureInvariant); | |
| return string.Join("-", keywords); | |
| } | |
| public static string[] GenerateKeywordsFromText(string input, int count, bool cultureInvariant) | |
| { | |
| if (input == null) | |
| return null; | |
| var keywords = GetKeywords(input, cultureInvariant); | |
| if (keywords == null || keywords.Length == 0) | |
| return null; | |
| var groups = ( | |
| from word in keywords | |
| group word by word into g | |
| select new {Word = g.Key, Count = g.Count() } | |
| ); | |
| return groups.OrderByDescending((f) => f.Count).Take(count).Select((f) => f.Word).ToArray(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment