Created
November 5, 2012 09:47
-
-
Save hefangshi/4016366 to your computer and use it in GitHub Desktop.
MakeRandom
This file contains 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.Text; | |
namespace RandomContent | |
{ | |
public interface RandomContent | |
{ | |
string MakeRandom(string content); | |
} | |
public class SplitWordRandomContent:RandomContent | |
{ | |
private Random random; | |
private static readonly string[] suffixArray = new string[] { "ed", "ing", "es", "'s","s" }; | |
public SplitWordRandomContent() | |
{ | |
random = new Random(); | |
} | |
public string MakeRandom(string content) | |
{ | |
StringBuilder sb = new StringBuilder(content.Length); | |
SplitForeach(content, ' ', (word) => | |
{ | |
string suffixChars; | |
string prefixChars; | |
//去除前后缀和标点符号 | |
var excludePreffixWord = ExcludePreffix(word, out prefixChars); | |
var excludeSuffixWord = ExcludeSuffix(excludePreffixWord, out suffixChars); | |
//重新组合文档 | |
sb.Append(prefixChars); | |
sb.Append(RandomWord(excludeSuffixWord)); | |
sb.Append(suffixChars); | |
sb.Append(' '); | |
}); | |
return sb.ToString(); | |
} | |
/// <summary> | |
/// 遍历划分字符串 | |
/// </summary> | |
/// <param name="content"></param> | |
/// <param name="splitChar"></param> | |
/// <param name="wordHandler"></param> | |
private static void SplitForeach(string content, char splitChar, Action<string> wordHandler) | |
{ | |
var totalLength = content.Length; | |
var preIndex=0; | |
var nowIndex = content.IndexOf(splitChar); | |
do | |
{ | |
string word; | |
if (nowIndex == -1) | |
{ | |
word = content.Substring(preIndex); | |
preIndex = totalLength; | |
} | |
else | |
{ | |
word = content.Substring(preIndex, nowIndex - preIndex); | |
preIndex = nowIndex + 1; | |
nowIndex = content.IndexOf(splitChar, preIndex); | |
} | |
wordHandler(word); | |
} while (preIndex != totalLength); | |
} | |
/// <summary> | |
/// 处理不是单词部分的前缀 | |
/// </summary> | |
/// <param name="word"></param> | |
/// <param name="prefixChars"></param> | |
/// <returns></returns> | |
private string ExcludePreffix(string word, out string prefixChars) | |
{ | |
var preffixBuilder = new StringBuilder(1); | |
//处理前缀不是字符的情况 | |
while (true) | |
{ | |
var lastChar = word[0]; | |
var charAscii = (int)lastChar; | |
if ((charAscii > 64 && charAscii < 91) || | |
(charAscii > 96 && charAscii < 123)) | |
{ | |
break; | |
} | |
else | |
{ | |
preffixBuilder.Append(lastChar); | |
word = word.Substring(1, word.Length - 1); | |
} | |
} | |
prefixChars = preffixBuilder.ToString(); | |
return word; | |
} | |
/// <summary> | |
/// 处理不是单词部分的后缀 | |
/// </summary> | |
/// <param name="word"></param> | |
/// <param name="suffixChars"></param> | |
/// <returns></returns> | |
private string ExcludeSuffix(string word, out string suffixChars) | |
{ | |
var suffixBuilder=new StringBuilder(5); | |
//处理后缀不是字符的情况 | |
while (true) | |
{ | |
var lastChar=word[word.Length-1]; | |
var charAscii=(int)lastChar; | |
if ((charAscii > 64 && charAscii < 91) || | |
(charAscii > 96 && charAscii < 123)) | |
{ | |
break; | |
} | |
else | |
{ | |
suffixBuilder.Insert(0, lastChar); | |
word = word.Substring(0, word.Length - 1); | |
} | |
} | |
//针对单词处理复数、过去时、进行时后缀, | |
for (int i = 0; i < suffixArray.Length; i++) | |
{ | |
if (word.EndsWith(suffixArray[i])) | |
{ | |
suffixBuilder.Insert(0, suffixArray[i]); | |
word = word.Substring(0, word.Length - suffixArray[i].Length); | |
} | |
} | |
suffixChars = suffixBuilder.ToString(); | |
return word; | |
} | |
/// <summary> | |
/// 对单词进行乱序排列 | |
/// </summary> | |
/// <param name="word"></param> | |
/// <returns></returns> | |
private string RandomWord(string word) | |
{ | |
if (String.IsNullOrEmpty(word)) | |
return String.Empty; | |
//长度小于三的单词乱序后维持不变 | |
if (word.Length<3) | |
return word; | |
var wordCharArray = word.ToCharArray(); | |
var randomLength = wordCharArray.Length - 1; | |
for (int i = 1; i < randomLength; i++) | |
{ | |
var randomIndex=random.Next(randomLength-1)+1; | |
if (i != randomIndex) | |
{ | |
var temp = wordCharArray[i]; | |
wordCharArray[i] = wordCharArray[randomIndex]; | |
wordCharArray[randomIndex] = temp; | |
} | |
} | |
return new String(wordCharArray); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment