commentsFromTurtle static method

Map<String, String> commentsFromTurtle(
  1. String ttlContent
)

Parses comments from TTL content using proper RDF parsing.

Implementation

static Map<String, String> commentsFromTurtle(String ttlContent) {
  try {
    // First try JSON backup for backward compatibility.

    final jsonMatch = RegExp(r'# JSON_DATA: (.+)').firstMatch(ttlContent);
    if (jsonMatch != null) {
      final jsonData = jsonMatch.group(1)!;
      final decoded = jsonDecode(jsonData) as Map<String, dynamic>;
      return decoded.cast<String, String>();
    }

    // Parse using proper RDF.

    final triples = turtleToTripleMap(ttlContent);
    final comments = <String, String>{};

    // Find comment resources.

    for (final subject in triples.keys) {
      final predicates = triples[subject]!;

      // Check if this is a comment resource.

      final typeValues =
          predicates['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] ?? [];
      final isComment = typeValues.any(
        (type) => type.toString().contains('Comment') || type == '#Comment',
      );

      if (isComment) {
        final movieIdValues = predicates['#movieId'] ?? [];
        final textValues = predicates['#text'] ?? [];

        if (movieIdValues.isNotEmpty && textValues.isNotEmpty) {
          final movieId = movieIdValues.first.toString();
          final comment = textValues.first.toString();
          comments[movieId] = comment;
        }
      }
    }

    return comments;
  } catch (e) {
    return {};
  }
}