public void DrawStrings(string[] strings, Vector2 position, Color color) { var metrics = new StringMetrics(); foreach (var s in strings) { var relPos = new Vector2(position.X, position.Y + metrics.BottomRight.Y + metrics.OverhangBottom); metrics = _default.DrawString(s, relPos, color); } }
private int Lev(string s1, string s2) { var d1 = StringMetrics.LevenshteinDistance(s1, s2); var d2 = StringMetrics.LevenshteinDistance(s2, s1); Assert.AreEqual(d1, d2); return(d1); }
private int OSA(string s1, string s2) { var d1 = StringMetrics.OptimalStringAlignmentDistance(s1, s2); var d2 = StringMetrics.OptimalStringAlignmentDistance(s2, s1); Assert.AreEqual(d1, d2); return(d1); }
public void Similarity_OneOfInputsIsEmpty_ShouldBeZero() { Assert.That(string.Empty.Similarity(string.Empty), Is.EqualTo(0)); Assert.That(string.Empty.Similarity(null), Is.EqualTo(0)); Assert.That(StringMetrics.Similarity(null, string.Empty), Is.EqualTo(0)); Assert.That("a".Similarity(string.Empty), Is.EqualTo(0)); Assert.That("a".Similarity(null), Is.EqualTo(0)); Assert.That(string.Empty.Similarity("a"), Is.EqualTo(0)); Assert.That(StringMetrics.Similarity(null, "a"), Is.EqualTo(0)); }
public void DrawStrings(string[] strings, Vector2 position, Color color) { var metrics = new StringMetrics(); foreach (var s in strings) { var relPos = new Vector2(position.X, position.Y + metrics.BottomRight.Y + metrics.OverhangBottom); metrics = _default.DrawString(s, relPos, color); } }
public void DrawStrings(string fontName, string[] strings, Vector2 position, Color color) { var metrics = new StringMetrics(); var font = _default; if (_fonts.ContainsKey(fontName)) { font = _fonts[fontName]; } foreach (var s in strings) { var relPos = new Vector2(position.X, position.Y + metrics.BottomRight.Y + metrics.OverhangBottom); metrics = font.DrawString(s, relPos, color); } }
public void DrawStrings(string fontName, string[] strings, Vector2 position, Color color) { var metrics = new StringMetrics(); var font = _default; if (_fonts.ContainsKey(fontName)) { font = _fonts[fontName]; } foreach (var s in strings) { var relPos = new Vector2(position.X, position.Y + metrics.BottomRight.Y + metrics.OverhangBottom); metrics = font.DrawString(s, relPos, color); } }
private IEnumerable <PhraseIdWithRankModel> GetTopTen(string keyword) { var outp = new List <PhraseIdWithRankModel>(10); var i = 0; foreach (var phrase in Phrases.Select(x => new { x.Id, x.Value })) { var rank = StringMetrics.Levenstein(keyword, phrase.Value).Value; if (i < 10) { outp.Add(new PhraseIdWithRankModel { PhraseId = phrase.Id, Rank = rank }); i++; continue; } if (i == 10) { outp.Sort(new PhraseWithRankModelComparer()); } if (!(rank > outp[9].Rank)) { continue; } outp.RemoveAt(9); outp.Add(new PhraseIdWithRankModel { PhraseId = phrase.Id, Rank = rank }); outp.Sort(new PhraseWithRankModelComparer()); } return(outp); }
protected override object?Visit(Expression.DotPath dot) { base.Visit(dot); var leftType = System.TypeOf(dot.Left); if (leftType.Equals(Type.Type_)) { // Static member access, we need the type value itself var leftValue = System.EvaluateType(dot.Left); var token = ((Syntax.ParseTree.Expression.DotPath?)dot.ParseTreeNode)?.Right; var _ = token == null ? leftValue.DefinedScope.Reference(dot.Right, System) : leftValue.DefinedScope.Reference(token, System); } else if (leftType is Type.Struct structType) { // Field access if (!(structType.Fields.ContainsKey(dot.Right))) { var rightIdent = (dot.ParseTreeNode as Syntax.ParseTree.Expression.DotPath)?.Right; var err = rightIdent == null ? new UndefinedSymbolError(dot.Right) : new UndefinedSymbolError(rightIdent); err.Context = "field access"; err.SimilarExistingNames = structType.Fields.Keys .Where(f => StringMetrics.OptimalStringAlignmentDistance(f, dot.Right) <= 2); System.Report(err); } } else { // TODO throw new NotImplementedException(); } return(null); }
private Dictionary <string, Shelf> CreateShelvesMap(List <Book> books) { var shelf_mapping = new Dictionary <string, Shelf>(); var goodreads_shelves = books.Select(b => b.Metadata["ExclusiveShelf"]) .Distinct() .ToList(); foreach (var shelf in goodreads_shelves) { var edit_distances = state_manager.CurrentCollection.Shelves .Select(s => new { Shelf = s, EditDistance = StringMetrics.EditDistance(s.Name, shelf) }) .OrderBy(x => x.EditDistance); var closest_match = edit_distances.First(); // Check if we found a match if (closest_match.EditDistance <= state_manager.Settings.MaxEditDistanceForShelfMatching) { shelf_mapping.Add(shelf, closest_match.Shelf); } else { // If not match found, then create one if (state_manager.Settings.CreateUnmatchedShelves) { var new_shelf = state_manager.CurrentCollection.AddShelf(shelf); shelf_mapping.Add(shelf, new_shelf); } else // Or use the default one { shelf_mapping.Add(shelf, state_manager.CurrentCollection.GetDefaultshelf()); } } } return(shelf_mapping); }
protected override object?Visit(Expression.StructValue sval) { base.Visit(sval); // Check if it's even a struct type var instanceType = System.EvaluateType(sval.StructType); if (!(instanceType is Type.Struct structType)) { // TODO throw new NotImplementedException("Can't instantiate non-struct!"); } // Check if all fields are instantiated exactly once and with their proper type var remainingFields = structType.Fields.ToDictionary(f => f.Key, f => f.Value); var alreadyInitialized = new Dictionary <string, IParseTreeElement?>(); foreach (var field in sval.Fields) { if (!remainingFields.Remove(field.Name, out var declaredField)) { // Either already initialized, or unknown field if (structType.Fields.ContainsKey(field.Name)) { System.Report(new DoubleInitializationError(structType, field.Name) { TypeInitializer = sval.StructType?.ParseTreeNode, FirstInitialized = alreadyInitialized[field.Name], SecondInitialized = field.ParseTreeNode, }); } else { System.Report(new UnknownInitializedFieldError(structType, field.Name) { TypeInitializer = sval.StructType?.ParseTreeNode, UnknownInitialized = field.ParseTreeNode, SimilarExistingNames = remainingFields.Keys .Where(n => StringMetrics.OptimalStringAlignmentDistance(n, field.Name) <= 2), }); } } else { // Types need to match var assignedType = System.TypeOf(field.Value); if (!assignedType.Equals(declaredField.Type.Value)) { System.Report(new TypeMismatchError(declaredField.Type.Value, assignedType) { Context = "struct value initialization", Defined = declaredField.Definition?.ParseTreeNode, Wrong = field.ParseTreeNode, }); } alreadyInitialized.Add(field.Name, field.ParseTreeNode); } } if (remainingFields.Count > 0) { System.Report(new MissingInitializationError(structType, remainingFields.Keys) { TypeInitializer = sval.StructType?.ParseTreeNode, }); } return(null); }
public static List <string> GetRanksForString(string key, int take = 5, int skip = 0) { var outp = new HashSet <KeywordPhraseReference>(); using (var ctx = new TypeFineContext()) { var keyword = ctx.Keywords.SingleOrDefault(x => x.Value == key); if (keyword != null) { if (keyword.Phrase != null) { return new List <string> { keyword.Phrase.Value } } ; var loveStories = ctx.KeywordPhraseReferences.Include("Phrase").Where(x => x.Keyword.Value == key); if (!loveStories.Any()) { var phrases = ctx.Phrases.ToList(); var lastRank = 0d; foreach (var phrase in phrases) { var rank = StringMetrics.Levenstein(key, phrase.Value).Value; if (outp.Count() < take) { outp.Add(new KeywordPhraseReference { Convergence = rank, UserWeight = 0, Keyword = keyword, Phrase = phrase }); } else { if (!(rank > lastRank)) { continue; } outp.Add(new KeywordPhraseReference { Convergence = rank, UserWeight = 0, Keyword = keyword, Phrase = phrase }); } var sort2 = outp.OrderByDescending(x => x.Convergence).Skip(skip).Take(take); outp = new HashSet <KeywordPhraseReference>(sort2); lastRank = outp.Last().Convergence; } ctx.KeywordPhraseReferences.AddRange(outp); ctx.SaveChanges(); return(outp.Select(x => x.Phrase.Value).ToList()); } var result = loveStories.ToList().OrderBy(x => x.CompositeConvergance); return(result.Select(x => x.Phrase.Value).ToList()); } throw new Exception("Missing keyword"); } } }