|
| 1 | +using System.Diagnostics.CodeAnalysis; |
| 2 | +using static ManagedCode.MarkdownLd.Kb.Pipeline.PipelineConstants; |
| 3 | + |
| 4 | +namespace ManagedCode.MarkdownLd.Kb.Pipeline; |
| 5 | + |
| 6 | +internal sealed partial class KnowledgeGraphRuleExtractor |
| 7 | +{ |
| 8 | + private string? ReadMapNodeId( |
| 9 | + IReadOnlyDictionary<string, object?> map, |
| 10 | + MarkdownDocument document, |
| 11 | + params string[] keys) |
| 12 | + { |
| 13 | + foreach (var key in keys) |
| 14 | + { |
| 15 | + if (TryReadString(map, key, out var value)) |
| 16 | + { |
| 17 | + return ResolveNodeId(document, value); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + private bool TryReadNodeReference( |
| 25 | + object? item, |
| 26 | + MarkdownDocument document, |
| 27 | + [NotNullWhen(true)] out GraphNodeReference? node) |
| 28 | + { |
| 29 | + if (item is IReadOnlyDictionary<string, object?> map) |
| 30 | + { |
| 31 | + return TryReadMapNodeReference(map, document, out node); |
| 32 | + } |
| 33 | + |
| 34 | + var text = item?.ToString()?.Trim(); |
| 35 | + if (string.IsNullOrWhiteSpace(text)) |
| 36 | + { |
| 37 | + node = null; |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + node = new GraphNodeReference( |
| 42 | + ResolveNodeId(document, text), |
| 43 | + text, |
| 44 | + null, |
| 45 | + [], |
| 46 | + FullConfidence, |
| 47 | + !IsExternalIdentifier(text)); |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + private bool TryReadMapNodeReference( |
| 52 | + IReadOnlyDictionary<string, object?> map, |
| 53 | + MarkdownDocument document, |
| 54 | + [NotNullWhen(true)] out GraphNodeReference? node) |
| 55 | + { |
| 56 | + var label = ReadFirstString(map, LabelKey, NameKey, ValueKey, TargetKey, ObjectKey); |
| 57 | + var idText = ReadFirstString(map, IdKey, TargetIdKey, TargetIdSnakeKey, ObjectIdKey, ObjectIdSnakeKey); |
| 58 | + if (string.IsNullOrWhiteSpace(label)) |
| 59 | + { |
| 60 | + label = idText; |
| 61 | + } |
| 62 | + |
| 63 | + if (string.IsNullOrWhiteSpace(label) && string.IsNullOrWhiteSpace(idText)) |
| 64 | + { |
| 65 | + node = null; |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + var type = ReadFirstString(map, TypeKey); |
| 70 | + var sameAs = ReadStringList(map, SameAsKey, SameAsSnakeKey).ToList(); |
| 71 | + node = new GraphNodeReference( |
| 72 | + string.IsNullOrWhiteSpace(idText) |
| 73 | + ? ResolveNodeId(document, label!) |
| 74 | + : ResolveNodeId(document, idText), |
| 75 | + label ?? idText!, |
| 76 | + type, |
| 77 | + sameAs, |
| 78 | + FullConfidence, |
| 79 | + ShouldAddEntity(label, idText, type, sameAs)); |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + private string ResolveNodeId(MarkdownDocument document, string? value) |
| 84 | + { |
| 85 | + var text = value?.Trim(); |
| 86 | + if (string.IsNullOrWhiteSpace(text) || |
| 87 | + text.Equals(ArticleMarker, StringComparison.OrdinalIgnoreCase) || |
| 88 | + text.Equals(ThisArticleMarker, StringComparison.OrdinalIgnoreCase) || |
| 89 | + text.Equals(DefaultDocument, StringComparison.OrdinalIgnoreCase)) |
| 90 | + { |
| 91 | + return document.DocumentUri.AbsoluteUri; |
| 92 | + } |
| 93 | + |
| 94 | + if (Uri.TryCreate(text, UriKind.Absolute, out var absolute)) |
| 95 | + { |
| 96 | + return absolute.AbsoluteUri; |
| 97 | + } |
| 98 | + |
| 99 | + if (text.StartsWith(UriSchemePrefix, StringComparison.OrdinalIgnoreCase)) |
| 100 | + { |
| 101 | + return text; |
| 102 | + } |
| 103 | + |
| 104 | + return KnowledgeNaming.CreateEntityId(_baseUri, text); |
| 105 | + } |
| 106 | + |
| 107 | + private static IEnumerable<GraphRuleFrontMatterItem> ReadFrontMatterItems( |
| 108 | + IReadOnlyDictionary<string, object?> frontMatter, |
| 109 | + params string[] keys) |
| 110 | + { |
| 111 | + foreach (var key in keys) |
| 112 | + { |
| 113 | + if (!frontMatter.TryGetValue(key, out var raw)) |
| 114 | + { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + var index = 0; |
| 119 | + if (raw is IEnumerable<object?> list && raw is not string) |
| 120 | + { |
| 121 | + foreach (var item in list) |
| 122 | + { |
| 123 | + yield return new GraphRuleFrontMatterItem(key, index, item); |
| 124 | + index++; |
| 125 | + } |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + yield return new GraphRuleFrontMatterItem(key, index, raw); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static string? ReadFirstString(IReadOnlyDictionary<string, object?> map, params string[] keys) |
| 135 | + { |
| 136 | + foreach (var key in keys) |
| 137 | + { |
| 138 | + if (TryReadString(map, key, out var value)) |
| 139 | + { |
| 140 | + return value; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + private static IEnumerable<string> ReadStringList( |
| 148 | + IReadOnlyDictionary<string, object?> map, |
| 149 | + params string[] keys) |
| 150 | + { |
| 151 | + foreach (var key in keys) |
| 152 | + { |
| 153 | + if (!map.TryGetValue(key, out var raw)) |
| 154 | + { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + if (raw is IEnumerable<object?> list && raw is not string) |
| 159 | + { |
| 160 | + foreach (var item in list) |
| 161 | + { |
| 162 | + var text = item?.ToString()?.Trim(); |
| 163 | + if (!string.IsNullOrWhiteSpace(text)) |
| 164 | + { |
| 165 | + yield return text; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + else if (!string.IsNullOrWhiteSpace(raw?.ToString())) |
| 170 | + { |
| 171 | + yield return raw.ToString()!.Trim(); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static bool TryReadString( |
| 177 | + IReadOnlyDictionary<string, object?> map, |
| 178 | + string key, |
| 179 | + [NotNullWhen(true)] out string? value) |
| 180 | + { |
| 181 | + if (map.TryGetValue(key, out var raw) && !string.IsNullOrWhiteSpace(raw?.ToString())) |
| 182 | + { |
| 183 | + value = raw.ToString()!.Trim(); |
| 184 | + return true; |
| 185 | + } |
| 186 | + |
| 187 | + value = null; |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + private static bool ShouldAddEntity( |
| 192 | + string? label, |
| 193 | + string? idText, |
| 194 | + string? type, |
| 195 | + IReadOnlyCollection<string> sameAs) |
| 196 | + { |
| 197 | + if (!string.IsNullOrWhiteSpace(type) || sameAs.Count > 0) |
| 198 | + { |
| 199 | + return true; |
| 200 | + } |
| 201 | + |
| 202 | + if (!string.IsNullOrWhiteSpace(idText)) |
| 203 | + { |
| 204 | + return !string.Equals(label, idText, StringComparison.OrdinalIgnoreCase); |
| 205 | + } |
| 206 | + |
| 207 | + return !IsExternalIdentifier(label); |
| 208 | + } |
| 209 | + |
| 210 | + private static bool IsExternalIdentifier(string? value) |
| 211 | + { |
| 212 | + return !string.IsNullOrWhiteSpace(value) && |
| 213 | + Uri.TryCreate(value.Trim(), UriKind.Absolute, out _); |
| 214 | + } |
| 215 | + |
| 216 | + private sealed record GraphRuleFrontMatterItem( |
| 217 | + string RuleName, |
| 218 | + int Index, |
| 219 | + object? Value); |
| 220 | + |
| 221 | + private sealed record GraphNodeReference( |
| 222 | + string Id, |
| 223 | + string Label, |
| 224 | + string? Type, |
| 225 | + List<string> SameAs, |
| 226 | + double Confidence, |
| 227 | + bool ShouldAddEntity); |
| 228 | +} |
0 commit comments