{"id":37136592,"url":"https://github.com/zorihq/trie-url-classifier","last_synced_at":"2026-01-14T15:55:16.313Z","repository":{"id":324985487,"uuid":"1099279849","full_name":"ZoriHQ/trie-url-classifier","owner":"ZoriHQ","description":"A Go library that automatically learns URL patterns from batches of URLs and normalizes them by detecting high cardinality segments","archived":false,"fork":false,"pushed_at":"2025-11-18T23:01:57.000Z","size":18,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-19T01:05:45.171Z","etag":null,"topics":["analytics","cardinality","classification","golang","url-pattern"],"latest_commit_sha":null,"homepage":"https://zorihq.com","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZoriHQ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-18T19:56:01.000Z","updated_at":"2025-11-18T23:02:04.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/ZoriHQ/trie-url-classifier","commit_stats":null,"previous_names":["zorihq/trie-url-classifier"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ZoriHQ/trie-url-classifier","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZoriHQ%2Ftrie-url-classifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZoriHQ%2Ftrie-url-classifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZoriHQ%2Ftrie-url-classifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZoriHQ%2Ftrie-url-classifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZoriHQ","download_url":"https://codeload.github.com/ZoriHQ/trie-url-classifier/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZoriHQ%2Ftrie-url-classifier/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28425569,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T15:24:48.085Z","status":"ssl_error","status_checked_at":"2026-01-14T15:23:41.940Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","cardinality","classification","golang","url-pattern"],"created_at":"2026-01-14T15:55:15.682Z","updated_at":"2026-01-14T15:55:16.308Z","avatar_url":"https://github.com/ZoriHQ.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Trie-Based URL Pattern Classifier\n\nA Go library that automatically learns URL patterns from batches of URLs and normalizes them by detecting dynamic segments.\n\n## Why This Library?\n\nWe built this library to normalize URLs at [Zori](https://zorihq.com) when processing customer analytics events. High-cardinality URLs with dynamic IDs, UUIDs, and dates create storage and query performance issues that this library solves by:\n\n- **Automatic pattern detection** - No manual route definitions needed\n- **Batch-based learning** - Discovers patterns from actual URLs in each batch\n- **Cardinality reduction** - Converts `/users/12345/profile` to `/users/{id}/profile`\n- **Type-aware** - Distinguishes UUIDs, IuDs, dates, hashes, and other parameter types\n\nWorks for analytics, metrics storage, log normalization, and any system that needs to aggregate URL-based data.\n\n## Key Features\n\n- **Batch-Based Processing**: Create a new classifier instance per batch to avoid memory growth\n- **Smart Pattern Detection**: Detects high-cardinality segments automatically\n- **Parameter Type Classification**: Identifies UUIDs, IDs, dates, timestamps, hashes, tokens, and slugs\n- **No Upfront Knowledge**: Learns patterns from actual URLs in each batch\n- **Configurable**: Adjust cardinality thresholds and minimum sample requirements\n\n## Installation\n\n```bash\ngo get github.com/ZoriHQ/trie-url-classifier\n```\n\n## Quick Start\n\n```go\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n    // Process a batch of URLs\n    batch := []string{\n        \"/projects/d381b052-99eb-40f2-9ede-9bce790faae1/analytics\",\n        \"/projects/a1b2c3d4-e5f6-7890-abcd-ef1234567890/analytics\",\n        \"/projects/12345678-1234-1234-1234-123456789012/analytics\",\n        \"/users/123456/profile\",\n        \"/users/789012/profile\",\n        \"/users/345678/profile\",\n    }\n\n    // Create a NEW classifier for this batch\n    classifier := NewClassifier()\n\n    // Learn patterns from the batch\n    classifier.Learn(batch)\n\n    // Normalize URLs in the batch\n    for _, url := range batch {\n        pattern := classifier.Classify(url)\n        fmt.Printf(\"%s -\u003e %s\\n\", url, pattern)\n    }\n\n    // After processing, discard the classifier\n    // For the next batch, create a NEW instance\n}\n```\n\n## Usage Pattern\n\nProcess batches of URLs independently:\n\n```go\nfunc processBatch(urlBatch []string) {\n    classifier := NewClassifier()\n    classifier.Learn(urlBatch)\n\n    for _, url := range urlBatch {\n        normalizedURL := classifier.Classify(url)\n        // Use normalizedURL for storage, aggregation, etc.\n    }\n}\n```\n\n**Important**: Create a new classifier for each batch to avoid memory growth.\n\n## Configuration\n\nCustomize the classifier behavior:\n\n```go\nclassifier := NewClassifier(\n    WithCardinalityThreshold(0.75),  // 75% unique values = dynamic (default)\n    WithMinSamples(2),                // Minimum samples before detection (default: 2)\n)\n```\n\n### Configuration Options\n\n- **CardinalityThreshold** (default: 0.75): Ratio of unique values to total count. Higher = stricter detection\n- **MinSamples** (default: 2): Minimum samples needed at a position before considering it for parametrization\n\n## Parameter Type Detection\n\nThe classifier automatically detects and labels these parameter types:\n\n| Type | Pattern | Example |\n|------|---------|---------|\n| `{uuid}` | UUID v4 format | `d381b052-99eb-40f2-9ede-9bce790faae1` |\n| `{id}` | Numeric ID (6+ digits) or prefixed IDs | `123456`, `cus_abc123` |\n| `{hash}` | 24+ hex characters | `507f1f77bcf86cd799439011` |\n| `{date}` | ISO date (YYYY-MM-DD) | `2024-01-15` |\n| `{timestamp}` | Unix timestamp (10+ digits) | `1705334400` |\n| `{token}` | JWT tokens | `eyJhbGci...` |\n| `{slug}` | Hyphenated words with numbers | `my-post-12345` |\n| `{param}` | Generic parameter (fallback) | Any other dynamic value |\n\n## How It Works\n\n1. **Build Trie**: URLs are split by `/` and inserted into a trie structure\n2. **Track Cardinality**: Each node tracks unique values and total count\n3. **Detect Patterns**: High-cardinality positions (many unique values) = dynamic segments\n4. **Classify Types**: Dynamic values are classified using regex patterns\n5. **Normalize**: New URLs are matched against learned patterns\n\n### Example\n\nGiven training URLs:\n```\n/projects/uuid-1/analytics\n/projects/uuid-2/analytics\n/projects/uuid-3/analytics\n```\n\nThe trie detects:\n- \"projects\" appears 3 times with same value → static\n- Second segment has 3 unique values in 3 traversals → dynamic (100% cardinality)\n- \"analytics\" appears 3 times with same value → static\n\nResult: `/projects/{uuid}/analytics`\n\n## API Reference\n\n### `NewClassifier(opts ...Option) *Classifier`\n\nCreates a new URL pattern classifier with optional configuration.\n\n### `(*Classifier) Learn(urls []string)`\n\nLearns patterns from a batch of URLs. Can be called multiple times.\n\n### `(*Classifier) Classify(url string) string`\n\nNormalizes a URL based on learned patterns.\n\n### Configuration Options\n\n- `WithCardinalityThreshold(threshold float64) Option`\n- `WithMinSamples(min int) Option`\n\n## How Pattern Detection Works\n\nThe classifier detects dynamic segments through:\n\n1. **Cardinality analysis** - Segments with many unique values (≥75% by default) are marked as dynamic\n2. **Pattern matching** - Even with identical values, segments matching UUID/ID/date patterns are parameterized\n3. **Static preservation** - Common words like \"api\", \"users\", \"settings\" remain static\n\nThis handles both diverse batches and edge cases like repeated identical URLs.\n\n## Performance\n\n- **Space**: O(N × M) where N = number of unique URLs, M = average URL segments\n- **Time**:\n  - Learning: O(N × M) for N URLs\n  - Classification: O(M) for M segments\n\n## Contributing\n\nContributions welcome! Please open an issue or PR.\n\n## License\n\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzorihq%2Ftrie-url-classifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzorihq%2Ftrie-url-classifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzorihq%2Ftrie-url-classifier/lists"}