/// <summary> /// Evaluates a <c>pron</c> wildcard, replacing the string with the adequate pronoun /// regarding the last token found. /// </summary> /// <param name="w">The wilcard to find a replacement for</param> /// <returns>An appropiate replacement for the wildcard.</returns> private void EvaluatePronoun(Wildcard w) { Wildcard prev = null; string keycode; string keyword; for (int i = currentWildcardIx - 1; i >= 0; --i) { keycode = textWildcards [i].Keycode; keyword = wildcards [keycode].Keyword; if ((keyword != null) && keyword.IsAnyOf("name", "male", "female")) { // prev = textWildcards [i]; prev = wildcards [keycode]; break; } } for (int i = currentWildcardIx - 1; (prev == null) && (i >= 0); --i) { keycode = textWildcards [i].Keycode; keyword = wildcards [keycode].Keyword; if ((keyword != null) && keyword.IsAnyOf("void", "pron")) { continue; } // prev = textWildcards [i]; prev = wildcards [keycode]; break; } w.Replacement = new NamedTaskElement(Pronoun.Personal.FromWildcard(w, prev)); }
/// <summary> /// Evaluates a <c>name</c> wildcard, asigning a replacement. /// </summary> /// <param name="w">The wilcard to find a replacement for</param> private void EvaluateName(Wildcard w) { if (w.Replacement != null) { return; } if ((w.Name == "name") && String.IsNullOrEmpty(w.Where)) { w.Keyword = w.Type ?? generator.RandomPick("male", "female"); } switch (w.Keyword) { case "male": w.Replacement = this.avNames.PopFirst(n => n.Gender == Gender.Male, w.Where); break; case "female": w.Replacement = this.avNames.PopFirst(n => n.Gender == Gender.Female, w.Where); break; default: w.Replacement = !String.IsNullOrEmpty(w.Where) ? this.avNames.PopFirst(w.Where) : this.avNames.PopLast(); break; } w.Obfuscated = new Obfuscator("a person"); }
/// <summary> /// Evaluates a <c>location</c> wildcard, asigning a replacement. /// </summary> /// <param name="w">The wilcard to find a replacement for</param> private void EvaluateLocation(Wildcard w) { if (w.Replacement != null) { return; } if ((w.Name == "location") && String.IsNullOrEmpty(w.Where)) { w.Keyword = w.Type ?? generator.RandomPick("beacon", "room", "placement"); } switch (w.Keyword) { case "beacon": w.Replacement = this.avLocations.PopFirst(l => l.IsBeacon, w.Where); w.Obfuscated = ((SpecificLocation)w.Replacement).Room; break; case "room": w.Replacement = this.avLocations.PopFirst(l => l is Room, w.Where); w.Obfuscated = new Obfuscator("apartment"); break; case "placement": w.Replacement = this.avLocations.PopFirst(l => l.IsPlacement, w.Where); w.Obfuscated = ((SpecificLocation)w.Replacement).Room; break; default: w.Replacement = !String.IsNullOrEmpty(w.Where) ? this.avLocations.PopFirst(w.Where) : this.avLocations.PopLast(); w.Obfuscated = new Obfuscator("somewhere"); break; } }
/// <summary> /// Evaluates a <c>location</c> wildcard, asigning a replacement. /// </summary> /// <param name="w">The wilcard to find a replacement for</param> private void EvaluateQuestion(Wildcard w) { if (w.Replacement != null) { return; } w.Replacement = !String.IsNullOrEmpty(w.Where) ? this.avQuestions.PopFirst(w.Where) : this.avQuestions.PopLast(); w.Obfuscated = new Obfuscator("question"); }
/// <summary> /// Evaluates a <c>location</c> wildcard, asigning a replacement. /// </summary> /// <param name="w">The wilcard to find a replacement for</param> private void EvaluateGesture(Wildcard w) { if (w.Replacement != null) { return; } w.Replacement = !String.IsNullOrEmpty(w.Where) ? this.avGestures.PopFirst(w.Where) : this.avGestures.PopLast(); // w.Obfuscated = ; }
/// <summary> /// Evaluates a <c>location</c> wildcard, asigning a replacement. /// </summary> /// <param name="w">The wilcard to find a replacement for</param> private void EvaluateCategory(Wildcard w) { if (w.Replacement != null) { return; } w.Replacement = !String.IsNullOrEmpty(w.Where) ? this.avCategories.PopFirst(w.Where) : this.avCategories.PopLast(); w.Obfuscated = new Obfuscator("objects"); }
/// <summary> /// Evaluates a Wildcards and assignd a replacement to it. /// </summary> private void FindReplacement(Wildcard w) { switch (w.Name) { case "category": EvaluateCategory(w); break; case "gesture": EvaluateGesture(w); break; case "name": case "female": case "male": EvaluateName(w); break; case "beacon": case "placement": case "location": case "room": EvaluateLocation(w); break; case "object": case "aobject": case "kobject": case "sobject": EvaluateObject(w); break; case "question": EvaluateQuestion(w); break; case "void": default: w.Replacement = new HiddenTaskElement(); w.Obfuscated = new HiddenTaskElement(); break; case "pron": EvaluatePronoun(w); break; } }
/// <summary> /// Evaluates an <c>object</c> wildcard, asigning a replacement. /// </summary> /// <param name="w">The wilcard to find a replacement for</param> private void EvaluateObject(Wildcard w) { if (w.Replacement != null) { return; } if ((w.Name == "object") && String.IsNullOrEmpty(w.Where)) { w.Keyword = (w.Type == null) ? generator.RandomPick("kobject", "aobject") : String.Format("{0}object", w.Type[0]); } switch (w.Keyword) { case "aobject": w.Replacement = this.avObjects.PopFirst(o => o.Type == ObjectType.Alike, w.Where); break; case "kobject": w.Replacement = this.avObjects.PopFirst(o => o.Type == ObjectType.Known, w.Where); break; // case "uobject": // w.Replacement = GPSRObject.Unknown; // break; case "sobject": w.Replacement = this.avObjects.PopFirst(o => o.Type == ObjectType.Special, w.Where); break; default: w.Replacement = !String.IsNullOrEmpty(w.Where) ? this.avObjects.PopFirst(w.Where) : this.avObjects.PopLast(); break; } w.Obfuscated = ((Object)w.Replacement).Category; }
private void FindReplacements() { // List<Wildcard> whereless = new List<Wildcard>(this.wildcards.Count); // List<Wildcard> whereSimple = new List<Wildcard>(this.wildcards.Count); Queue <Wildcard> whereNested = new Queue <Wildcard>(this.wildcards.Count); // STEP 1: Wildcards not containing nested wildcards in the WHERE clause // are replaced on the fly, while others are left for later. foreach (KeyValuePair <string, Wildcard> p in this.wildcards) { if (p.Value.Where.IndexOf('{') == -1) { FindReplacement(p.Value); } else { whereNested.Enqueue(p.Value); } } // STEP 2: Replace nested wildcards in WHERE clauses // This will iterate over the list of wildcards with nested ones // in WHERE clauses until the list is empty or until a loop has // been done with no changes. int initial = whereNested.Count; while (whereNested.Count > 0) { // Step 2.1: Set the initial counter initial = whereNested.Count; // STEP 2.2: Dequeue the wildcard Wildcard w = whereNested.Dequeue(); bool allReplaced = true; // STEP 2.3: Look for TextWildcards with nested elements in WHERE clause foreach (TextWildcard tw in w) { if ((tw.Where == null) || (tw.Where.IndexOf('{') == -1)) { continue; } string twWhere = tw.Where; // STEP 2.4: Attempt to replace nested wildcards in each TextWildcard. if (ReplaceNestedWildcardsHelper(ref twWhere)) { tw.Where = twWhere; } else { allReplaced = false; } } // STEP 2.5: If all nested wildcards were replaced, look for a replacement // otherwise, the element is enqueued again. if (allReplaced) { FindReplacement(w); } else { whereNested.Enqueue(w); } // STEP 2.6: Compare the initial counter with the number of elements in the queue. // If the number matches, then no replacement was made. // We quit to prevent infinite loop if (initial == whereNested.Count()) { break; } } }