Skip to content

Class HTM::TimeframeExtractor

Inherits Object
Defined in lib/htm/timeframe_extractor.rb

Timeframe Extractor - Extracts temporal expressions from queries

This service parses natural language time expressions from recall queries and returns both the timeframe and the cleaned query text.

Supports: * Standard time expressions via Chronic gem ("yesterday", "last week", etc.) * "few" keyword mapped to FEW constant (e.g., "few days ago" → "3 days ago") * "recent/recently" without units defaults to FEW days

@example Basic usage

result = TimeframeExtractor.extract("what did we discuss last week about PostgreSQL")
result[:query]     # => "what did we discuss about PostgreSQL"
result[:timeframe] # => #<Range: 2025-11-21..2025-11-28>

@example With "few" keyword

result = TimeframeExtractor.extract("show me notes from a few days ago")
result[:timeframe] # => Time object for 3 days ago

@example With "recently"

result = TimeframeExtractor.extract("what did we recently discuss")
result[:timeframe] # => Range from 3 days ago to now

Constants

DEFAULT_RECENT_UNIT

Default unit for "recently" when no time unit is specified

FEW

The numeric value for "few" and "recently" without units

TEMPORAL_PATTERNS

Patterns for temporal expressions (order matters - more specific first) Each pattern should match ORIGINAL text (including "few", "a few")

TIME_UNITS

Time unit patterns for matching

UNIT_SECONDS

Seconds per singular time unit (used by parse_last_x and parse_recent)

WORD_NUMBERS

Word-to-number mapping for written numbers

Public Class Methods

extract(query)

Extract timeframe from a query string - @param query [String] The query to parse - @return [Result] Struct with :query (cleaned), :timeframe, :original_expression

temporal?(query)

Check if query contains a temporal expression - @param query [String] The query to check - @return [Boolean]