Untitled

 avatar
faisalsalameh
dart
9 months ago
2.4 kB
11
Indexable

/// Represents a single parsed reference with its extracted details.
class ReferenceDetails {
  /// The original raw string of the reference.
  final String? rawReference;

  /// The extracted authors of the reference.
  final String? authors;

  /// The extracted title of the reference.
  final String? title;

  /// The extracted journal name of the reference.
  final String? journal;

  /// The extracted link (URL) of the reference.
  final String? link;

  /// Creates a [ReferenceDetails] instance.
  ReferenceDetails({
    required this.rawReference,
    required this.authors,
    required this.title,
    required this.journal,
    required this.link,
  });

  @override
  String toString() {
    return 'Raw: $rawReference\n'
        '  Authors: $authors\n'
        '  Title: $title\n'
        '  Journal: $journal\n'
        '  Link: $link';
  }
}

/// Extracts details (authors, title, journal, link) from a single reference string.
///
/// It uses regular expressions to parse specific patterns within the string.
ReferenceDetails extractReferenceDetails(String reference) {
  // Regex for extracting the link:
  // It looks for "— <URL>" at the end of the string.
  final linkRegExp = RegExp(r'—\s*<(https?:\/\/[^>]+)>$');
  final linkMatch = linkRegExp.firstMatch(reference);
  final link = linkMatch?.group(1) ?? 'N/A';

  // Regex for extracting the journal name:
  // It looks for text enclosed in asterisks, e.g., *Journal Name*.
  final journalRegExp = RegExp(r'\*(.*?)\*');
  final journalMatch = journalRegExp.firstMatch(reference);
  final journal = (journalMatch?.group(1) ?? 'N/A').trim();

  // Regex for extracting authors and title:
  // It looks for text after '[#] ' and before the first period ('.') for authors.
  // Then, it looks for the title after that period and before the journal (*Journal*)
  // or a DOI/link indicator.
  final fullAuthorAndTitleRegExp = RegExp(
    r'^\[\d+\]\s*(.*?)\.\s*(.*?)(?=\s*(?:\*|DOI:|—\s*<https?:\/\/[^>]+>|$))',
  );

  final fullAuthorAndTitleMatch = fullAuthorAndTitleRegExp.firstMatch(
    reference,
  );
  final authors = (fullAuthorAndTitleMatch?.group(1) ?? 'N/A').trim();
  final title = (fullAuthorAndTitleMatch?.group(2) ?? 'N/A').trim();

  return ReferenceDetails(
    rawReference: reference,
    authors: authors,
    title: title,
    journal: journal,
    link: link,
  );
}
Editor is loading...
Leave a Comment