internal Dictionary<string, string> GetTokenMetadata(int tokenIndex, int spanIndex) { if (TokenMetadata is null) { TokenMetadata = new Dictionary<long, Dictionary<string, string>>(); } long ix = GetTokenIndex(spanIndex, tokenIndex); Dictionary<string, string> dict; if (TokenMetadata.TryGetValue(ix, out dict)) { return dict; } else { dict = new Dictionary<string, string>(); TokenMetadata.Add(ix, dict); return dict; } //TODO: REMOVE JsonIgnore from Token and from SingleToken when this is implemented }
public void WriteAsJson(IJsonWriter jw) { jw.WriteStartObject(); jw.WritePropertyName(nameof(Language)); jw.WriteStringValue(Languages.EnumToCode(Language)); jw.WritePropertyName(nameof(Length)); jw.WriteNumberValue(Length); jw.WritePropertyName(nameof(Value)); jw.WriteStringValue(Value); if (UID.IsNotNull()) { jw.WritePropertyName(nameof(UID)); jw.WriteStringValue(UID); } if (Metadata is object && Metadata.Any()) { jw.WritePropertyName(nameof(Metadata)); jw.WriteStartObject(); foreach (var kv in Metadata) { jw.WritePropertyName(kv.Key); jw.WriteStringValue(kv.Value); } jw.WriteEndObject(); } if (Labels is object && Labels.Length > 0) { jw.WritePropertyName(nameof(Labels)); jw.WriteStartArray(); foreach (var l in Labels) { jw.WriteStringValue(l); } jw.WriteEndArray(); } jw.WritePropertyName(nameof(TokensData)); jw.WriteStartArray(); for (int i = 0; i < TokensData.Length; i++) { var spanData = TokensData[i]; jw.WriteStartArray(); for (int j = 0; j < spanData.Length; j++) { var tk = spanData[j]; long ix = GetTokenIndex(i, j); jw.WriteStartObject(); jw.WritePropertyName(nameof(TokenData.Bounds)); jw.WriteStartArray(); jw.WriteNumberValue(tk.LowerBound); jw.WriteNumberValue(tk.UpperBound); jw.WriteEndArray(); if (tk.Tag != PartOfSpeech.NONE) { jw.WritePropertyName(nameof(TokenData.Tag)); jw.WriteStringValue(tk.Tag.ToString()); } if (tk.Head >= 0) { jw.WritePropertyName(nameof(TokenData.Head)); jw.WriteNumberValue(tk.Head); } if (tk.Frequency != 0) { jw.WritePropertyName(nameof(TokenData.Frequency)); jw.WriteNumberValue(tk.Frequency); } if (!string.IsNullOrEmpty(tk.Replacement)) { jw.WritePropertyName(nameof(TokenData.Replacement)); jw.WriteStringValue(tk.Replacement); } if (TokenMetadata is object) { if (TokenMetadata.TryGetValue(ix, out var tokenMetadata)) { if (!(tokenMetadata is null) && tokenMetadata.Any()) { jw.WritePropertyName(nameof(Metadata)); jw.WriteStartObject(); foreach (var kv in tokenMetadata) { jw.WritePropertyName(kv.Key); jw.WriteStringValue(kv.Value); } jw.WriteEndObject(); } } } if (EntityData is object) { if (EntityData.TryGetValue(ix, out var entities)) { if (!(entities is null) && entities.Any()) { jw.WritePropertyName(nameof(EntityType)); jw.WriteStartArray(); for (int k = 0; k < entities.Length; k++) { jw.WriteStartObject(); jw.WritePropertyName(nameof(EntityType.Type)); jw.WriteStringValue(entities[k].Type); jw.WritePropertyName(nameof(EntityType.Tag)); jw.WriteStringValue(entities[k].Tag.ToString()); if (entities[k].TargetUID.IsNotNull()) { jw.WritePropertyName(nameof(EntityType.TargetUID)); jw.WriteStringValue(entities[k].TargetUID); } if (!(entities[k].Metadata is null) && entities[k].Metadata.Any()) { jw.WritePropertyName(nameof(EntityType.Metadata)); jw.WriteStartObject(); foreach (var kv in entities[k].Metadata) { jw.WritePropertyName(kv.Key); jw.WriteStringValue(kv.Value); } jw.WriteEndObject(); } jw.WriteEndObject(); } jw.WriteEndArray(); } } } jw.WriteEndObject(); } jw.WriteEndArray(); } jw.WriteEndArray(); jw.WriteEndObject(); }