private static bool IsPrime(int number) { int boundary = (int)Math.Floor(Math.Sqrt(number)); if (number == 1) return false; if (number == 2) return true; for (int i = 2; i <= boundary; ++i) { if (number % i == 0) return false; } return true; }
public void PrimeTest() { var oregex = new ORegex<int>("{0}(.{0})*", IsPrime); var input = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; foreach (var match in oregex.Matches(input)) { Console.WriteLine(string.Join(",", match.Values)); } //OUTPUT: //2 //3,4,5,6,7 //11,12,13 }
public enum SemanticType { Name, FamilyName, Other, } public class Word { public readonly string Value; public readonly SemanticType SemType; public Word(string value, SemanticType semType) { Value = value; SemType = semType; } } public class Person { public readonly Word[] Words; public readonly string Name; public Person(OMatch<Word> match) { Words = match.Values.ToArray(); Name = match.OCaptures["name"].First().Values.First().Value; //Now just normalize this name and you are good. } }
private static bool IsInitial(string str) { var inp = str.Trim(new[] { '.', ' ', '\t', '\n', '\r' }); return inp.Length == 1 && char.IsUpper(inp[0]); }
public void PersonSelectionTest() { //INPUT_TEXT: .. var sentence = new Word[] { new Word("", SemanticType.FamilyName), new Word("", SemanticType.Name), new Word("", SemanticType.Other), new Word("", SemanticType.Other), new Word("", SemanticType.Name), new Word("", SemanticType.Other), new Word("", SemanticType.Other), new Word("", SemanticType.Other), new Word("", SemanticType.Name), new Word(".", SemanticType.Other), new Word("", SemanticType.Other), }; // . var pTable = new PredicateTable<Word>(); pTable.AddPredicate("", x => x.SemType == SemanticType.FamilyName); //Check if word is FamilyName. pTable.AddPredicate("", x => x.SemType == SemanticType.Name); //Check if word is simple Name. pTable.AddPredicate("", x => IsInitial(x.Value)); //Complex check if Value is Inital character. // . var oregex = new ORegex<Word>(@" {}(?<name>{}) //Comments can be written inside pattern... | (?<name>{})({}|{}{1,2})? /*...even complex ones.*/ ", pTable); // . var persons = oregex.Matches(sentence).Select(x => new Person(x)).ToArray(); foreach (var person in persons) { Console.WriteLine("Person found: {0}, length: {1}", person.Name, person.Words.Length); } //OUTPUT: //Person found: , length: 2 //Person found: , length: 1 //Person found: , length: 3 }
Source: https://habr.com/ru/post/278219/
All Articles