extract method

ExtractedFeatures extract(
  1. String input
)

Extracts features from a raw text input.

Implementation

ExtractedFeatures extract(String input) {
  final lower = input.toLowerCase().trim();
  final tokens = _tokenise(lower);

  final entities = <String>[];
  final actions = <String>[];
  final attributes = <String>[];
  final spatial = <String>[];
  final temporal = <String>[];
  final emotional = <String>[];
  final causal = <String>[];
  final quantifiers = <String>[];
  final negations = <String>[];

  for (final token in tokens) {
    if (_negationWords.contains(token)) {
      negations.add(token);
    } else if (_causalWords.contains(token)) {
      causal.add(token);
    } else if (_spatialWords.contains(token)) {
      spatial.add(token);
    } else if (_temporalWords.contains(token)) {
      temporal.add(token);
    } else if (_emotionalWords.contains(token)) {
      emotional.add(token);
    } else if (_quantifiers.contains(token)) {
      quantifiers.add(token);
    } else if (!_stopWords.contains(token)) {
      // Heuristic: short tokens with verb suffixes → action
      if (_looksLikeVerb(token)) {
        actions.add(token);
      } else if (_looksLikeAdjective(token)) {
        attributes.add(token);
      } else if (token.length > 2) {
        entities.add(token);
      }
    }
  }

  final features = ExtractedFeatures(
    entities: entities,
    actions: actions,
    attributes: attributes,
    spatialRelations: spatial,
    temporalMarkers: temporal,
    emotionalCues: emotional,
    causalCues: causal,
    quantifiers: quantifiers,
    negations: negations,
    questions: lower.endsWith('?') || lower.startsWith('what') ||
        lower.startsWith('why') || lower.startsWith('how') ||
        lower.startsWith('who') || lower.startsWith('where'),
  );

  _logger.debug(
      'Extracted from "${_trunc(input)}": '
      '${entities.length} entities, '
      '${actions.length} actions, '
      '${emotional.length} emotional cues');

  return features;
}