parser

package
v0.14.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package parser provides URL parsing and fuzzy finding functionality for dumber.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DetectedShortcut

type DetectedShortcut struct {
	// Key is the shortcut key (e.g., "g", "gh")
	Key string `json:"key"`
	// Query is the search query part
	Query string `json:"query"`
	// URL is the constructed URL
	URL string `json:"url"`
	// Description is the shortcut description
	Description string `json:"description"`
}

DetectedShortcut represents a detected search shortcut.

type FuzzyConfig

type FuzzyConfig struct {
	// MinSimilarityThreshold is the minimum similarity score to consider a match (0.0-1.0)
	MinSimilarityThreshold float64 `json:"min_similarity_threshold"`
	// MaxResults is the maximum number of fuzzy results to return
	MaxResults int `json:"max_results"`
	// URLWeight is the weight for URL similarity in scoring
	URLWeight float64 `json:"url_weight"`
	// TitleWeight is the weight for title similarity in scoring
	TitleWeight float64 `json:"title_weight"`
	// RecencyWeight is the weight for recency in scoring
	RecencyWeight float64 `json:"recency_weight"`
	// VisitWeight is the weight for visit count in scoring
	VisitWeight float64 `json:"visit_weight"`
	// RecencyDecayDays is the number of days for recency score decay
	RecencyDecayDays int `json:"recency_decay_days"`
}

FuzzyConfig holds configuration for fuzzy matching algorithms.

func DefaultFuzzyConfig

func DefaultFuzzyConfig() *FuzzyConfig

DefaultFuzzyConfig returns default fuzzy matching configuration.

func (*FuzzyConfig) IsValid

func (fc *FuzzyConfig) IsValid() bool

IsValid returns true if the FuzzyConfig has valid values.

type FuzzyMatch

type FuzzyMatch struct {
	// HistoryEntry is the matched history entry
	HistoryEntry *db.History `json:"history_entry"`
	// Score is the similarity score (0.0-1.0, higher is better)
	Score float64 `json:"score"`
	// URLScore is the URL similarity score
	URLScore float64 `json:"url_score"`
	// TitleScore is the title similarity score
	TitleScore float64 `json:"title_score"`
	// RecencyScore is the recency score based on last visit
	RecencyScore float64 `json:"recency_score"`
	// VisitScore is the score based on visit count
	VisitScore float64 `json:"visit_score"`
	// MatchedField indicates which field matched (url, title, or both)
	MatchedField string `json:"matched_field"`
}

FuzzyMatch represents a fuzzy search match from history.

type FuzzyMatcher

type FuzzyMatcher struct {
	// contains filtered or unexported fields
}

FuzzyMatcher implements efficient fuzzy string matching algorithms.

func NewFuzzyMatcher

func NewFuzzyMatcher(config *FuzzyConfig) *FuzzyMatcher

NewFuzzyMatcher creates a new FuzzyMatcher with the given configuration.

func (*FuzzyMatcher) LevenshteinDistance

func (fm *FuzzyMatcher) LevenshteinDistance(s1, s2 string) int

LevenshteinDistance calculates the Levenshtein distance between two strings.

func (*FuzzyMatcher) LevenshteinSimilarity

func (fm *FuzzyMatcher) LevenshteinSimilarity(s1, s2 string) float64

LevenshteinSimilarity converts Levenshtein distance to similarity score (0.0-1.0).

func (*FuzzyMatcher) RankMatches

func (fm *FuzzyMatcher) RankMatches(matches []FuzzyMatch, query string) []FuzzyMatch

RankMatches re-ranks fuzzy matches based on additional criteria.

func (*FuzzyMatcher) SearchHistory

func (fm *FuzzyMatcher) SearchHistory(query string, history []*db.History) []FuzzyMatch

SearchHistory performs fuzzy search on history entries and returns ranked matches.

func (*FuzzyMatcher) TokenizedMatch

func (fm *FuzzyMatcher) TokenizedMatch(query, text string) float64

TokenizedMatch performs token-based fuzzy matching for better phrase matching.

type HistoryProvider

type HistoryProvider interface {
	// GetRecentHistory returns recent history entries limited by count
	GetRecentHistory(limit int) ([]*db.History, error)
	// GetAllHistory returns all history entries for fuzzy search
	GetAllHistory() ([]*db.History, error)
	// SearchHistory performs a basic text search in history
	SearchHistory(query string, limit int) ([]*db.History, error)
	// GetHistoryByURL retrieves history entry by exact URL match
	GetHistoryByURL(url string) (*db.History, error)
}

HistoryProvider defines the interface for history data access.

type HistorySearchResult

type HistorySearchResult struct {
	Query         string        `json:"query"`
	TotalEntries  int           `json:"total_entries"`
	MatchCount    int           `json:"match_count"`
	ExactMatches  []FuzzyMatch  `json:"exact_matches,omitempty"`
	FuzzyMatches  []FuzzyMatch  `json:"fuzzy_matches,omitempty"`
	RankedMatches []FuzzyMatch  `json:"ranked_matches"`
	SearchTime    time.Duration `json:"search_time"`
}

HistorySearchResult represents the result of a history search operation.

type HistorySearcher

type HistorySearcher struct {
	// contains filtered or unexported fields
}

HistorySearcher provides history search integration with fuzzy matching.

func NewHistorySearcher

func NewHistorySearcher(provider HistoryProvider, fuzzyConfig *FuzzyConfig) *HistorySearcher

NewHistorySearcher creates a new HistorySearcher.

func (*HistorySearcher) GetBestMatch

func (hs *HistorySearcher) GetBestMatch(query string) (*FuzzyMatch, error)

GetBestMatch returns the single best match for a query.

func (*HistorySearcher) GetMatchesByScore

func (hs *HistorySearcher) GetMatchesByScore(query string, minScore float64) ([]FuzzyMatch, error)

GetMatchesByScore returns matches above a specific score threshold.

func (*HistorySearcher) GetRecentWithFuzzy

func (hs *HistorySearcher) GetRecentWithFuzzy(limit int, filterQuery string) ([]FuzzyMatch, error)

GetRecentWithFuzzy returns recent entries with optional fuzzy filtering.

func (*HistorySearcher) GetTopVisited

func (hs *HistorySearcher) GetTopVisited(limit int, filterQuery string) ([]FuzzyMatch, error)

GetTopVisited returns the most visited sites with optional fuzzy filtering.

func (*HistorySearcher) SearchByDomain

func (hs *HistorySearcher) SearchByDomain(domain string, options ...SearchOption) (*HistorySearchResult, error)

SearchByDomain searches history entries by domain with fuzzy matching.

func (*HistorySearcher) SearchByTimeRange

func (hs *HistorySearcher) SearchByTimeRange(query string, startTime, endTime time.Time) (*HistorySearchResult, error)

SearchByTimeRange searches history entries within a time range with fuzzy matching.

func (*HistorySearcher) SearchWithFuzzy

func (hs *HistorySearcher) SearchWithFuzzy(query string, options ...SearchOption) (*HistorySearchResult, error)

SearchWithFuzzy performs fuzzy search on history entries.

func (*HistorySearcher) UpdateFuzzyConfig

func (hs *HistorySearcher) UpdateFuzzyConfig(config *FuzzyConfig)

UpdateFuzzyConfig updates the fuzzy matching configuration.

type InputType

type InputType int

InputType represents the type of user input detected.

const (
	// InputTypeDirectURL represents a direct URL input (e.g., "reddit.com", "https://example.com")
	InputTypeDirectURL InputType = iota
	// InputTypeSearchShortcut represents a search shortcut (e.g., "g: golang tutorial")
	InputTypeSearchShortcut
	// InputTypeHistorySearch represents a history search query
	InputTypeHistorySearch
	// InputTypeFallbackSearch represents a fallback web search
	InputTypeFallbackSearch
)

func (InputType) String

func (t InputType) String() string

String returns a string representation of InputType.

type ParseResult

type ParseResult struct {
	// Type indicates the detected input type
	Type InputType `json:"type"`
	// URL is the final URL to navigate to
	URL string `json:"url"`
	// Query is the original user query
	Query string `json:"query"`
	// Confidence is the confidence score of the parsing (0.0-1.0)
	Confidence float64 `json:"confidence"`
	// FuzzyMatches contains fuzzy search results if applicable
	FuzzyMatches []FuzzyMatch `json:"fuzzy_matches,omitempty"`
	// Shortcut contains the detected shortcut information
	Shortcut *DetectedShortcut `json:"shortcut,omitempty"`
	// ProcessingTime is the time taken to process the input
	ProcessingTime time.Duration `json:"processing_time"`
}

ParseResult represents the result of parsing user input.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser handles URL parsing and fuzzy finding operations.

func NewParser

func NewParser(cfg *config.Config, historyProvider HistoryProvider, opts ...ParserOption) *Parser

NewParser creates a new Parser instance with the given configuration.

func (*Parser) CalculateSimilarity

func (p *Parser) CalculateSimilarity(s1, s2 string) float64

CalculateSimilarity calculates similarity between two strings.

func (*Parser) FuzzySearchHistory

func (p *Parser) FuzzySearchHistory(query string, threshold float64) ([]FuzzyMatch, error)

FuzzySearchHistory performs fuzzy search on history and returns ranked matches.

func (*Parser) GetFuzzyConfig

func (p *Parser) GetFuzzyConfig() *FuzzyConfig

GetFuzzyConfig returns the current fuzzy matching configuration.

func (*Parser) GetSupportedShortcuts

func (p *Parser) GetSupportedShortcuts() map[string]config.SearchShortcut

GetSupportedShortcuts returns a list of configured search shortcuts.

func (*Parser) IsValidURL

func (p *Parser) IsValidURL(input string) bool

IsValidURL checks if the input represents a valid URL.

func (*Parser) ParseInput

func (p *Parser) ParseInput(input string) (*ParseResult, error)

ParseInput parses user input and returns a ParseResult with URL and metadata. Now uses the proven CLI parsing logic for consistent behavior.

func (*Parser) PreviewParse

func (p *Parser) PreviewParse(input string) (*ParseResult, error)

PreviewParse provides a preview of what ParseInput would return without side effects.

func (*Parser) ProcessShortcut

func (p *Parser) ProcessShortcut(shortcut, query string, shortcuts map[string]config.SearchShortcut) (string, error)

ProcessShortcut processes a shortcut and returns the resulting URL.

func (*Parser) RankMatches

func (p *Parser) RankMatches(matches []FuzzyMatch, query string) []FuzzyMatch

RankMatches re-ranks fuzzy matches based on additional criteria.

func (*Parser) SuggestCompletions

func (p *Parser) SuggestCompletions(input string, limit int) ([]string, error)

SuggestCompletions provides URL/search completions based on input.

func (*Parser) UpdateFuzzyConfig

func (p *Parser) UpdateFuzzyConfig(newConfig *FuzzyConfig) error

UpdateFuzzyConfig updates the fuzzy matching configuration.

func (*Parser) ValidateConfig

func (p *Parser) ValidateConfig() error

ValidateConfig validates the parser configuration.

type ParserOption

type ParserOption func(*Parser)

ParserOption is a functional option for configuring the Parser.

func WithFuzzyConfig

func WithFuzzyConfig(fuzzyConfig *FuzzyConfig) ParserOption

WithFuzzyConfig sets custom fuzzy matching configuration.

type SearchConfig

type SearchConfig struct {
	Limit                  int     `json:"limit"`
	MinSimilarityThreshold float64 `json:"min_similarity_threshold"`
	IncludeExactMatches    bool    `json:"include_exact_matches"`
	IncludeFuzzyMatches    bool    `json:"include_fuzzy_matches"`
	SortByRelevance        bool    `json:"sort_by_relevance"`
}

SearchConfig holds configuration for history search operations.

type SearchOption

type SearchOption func(*SearchConfig)

SearchOption is a functional option for configuring search operations.

func WithExactMatches

func WithExactMatches(include bool) SearchOption

WithExactMatches enables/disables exact match inclusion.

func WithFuzzyMatches

func WithFuzzyMatches(include bool) SearchOption

WithFuzzyMatches enables/disables fuzzy match inclusion.

func WithLimit

func WithLimit(limit int) SearchOption

WithLimit sets the maximum number of results to return.

func WithRelevanceSort

func WithRelevanceSort(enable bool) SearchOption

WithRelevanceSort enables/disables relevance-based sorting.

func WithSimilarityThreshold

func WithSimilarityThreshold(threshold float64) SearchOption

WithSimilarityThreshold sets the minimum similarity threshold for matches.

type URLValidator

type URLValidator struct{}

URLValidator provides URL validation utilities.

func NewURLValidator

func NewURLValidator() *URLValidator

NewURLValidator creates a new URLValidator.

func (*URLValidator) ExtractDomain

func (v *URLValidator) ExtractDomain(rawURL string) string

ExtractDomain extracts the domain from a URL.

func (*URLValidator) GetURLType

func (v *URLValidator) GetURLType(input string) InputType

GetURLType determines the type of URL (direct, search, etc.).

func (*URLValidator) HasFileExtension

func (v *URLValidator) HasFileExtension(rawURL string) bool

HasFileExtension checks if the URL path has a file extension.

func (*URLValidator) IsDirectURL

func (v *URLValidator) IsDirectURL(input string) bool

IsDirectURL checks if the input should be treated as a direct URL.

func (*URLValidator) IsLocalhost

func (v *URLValidator) IsLocalhost(rawURL string) bool

IsLocalhost checks if the URL is pointing to localhost.

func (*URLValidator) IsSearchShortcut

func (v *URLValidator) IsSearchShortcut(input string) (bool, string, string)

IsSearchShortcut checks if the input matches a search shortcut pattern.

func (*URLValidator) IsValidURL

func (v *URLValidator) IsValidURL(input string) bool

IsValidURL checks if the input represents a valid URL.

func (*URLValidator) NormalizeURL

func (v *URLValidator) NormalizeURL(input string) string

NormalizeURL normalizes a URL by adding scheme if missing.

func (*URLValidator) SanitizeInput

func (v *URLValidator) SanitizeInput(input string) string

SanitizeInput sanitizes user input for URL parsing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL