Documentation
¶
Overview ¶
Package proximity implements proximity-based PI detection enhancement. It analyzes surrounding text for PI-related keywords and patterns to increase confidence in PI detection when relevant context is found nearby.
Index ¶
- type ContextAnalyzer
- func (ca *ContextAnalyzer) AnalyzeSemanticContext(content string, startIndex, endIndex int) SemanticAnalysis
- func (ca *ContextAnalyzer) AnalyzeStructure(content string, startIndex, endIndex int) StructureAnalysis
- func (ca *ContextAnalyzer) CalculateContextWindow(content string, startIndex, endIndex, baseWindow int) (beforeWindow, afterWindow int)
- func (ca *ContextAnalyzer) CountKeywords(text string, keywords []string) int
- func (ca *ContextAnalyzer) ExtractSurroundingText(content string, startIndex, endIndex, windowSize int) (before, after string)
- func (ca *ContextAnalyzer) FindNearestKeyword(content string, keywords []string, startIndex, endIndex int) (keyword string, distance int, found bool)
- func (ca *ContextAnalyzer) GetWordProximity(content, targetWord string, startIndex, endIndex int) (distance int, found bool)
- type PIContextInfo
- type PIContextType
- type PatternMatcher
- func (pm *PatternMatcher) AnalyzeContextType(text string) PIContextType
- func (pm *PatternMatcher) ContainsTestDataKeywords(text string) bool
- func (pm *PatternMatcher) ExtractRelevantKeywords(text string) []string
- func (pm *PatternMatcher) FindPIContextLabels(text string) []string
- func (pm *PatternMatcher) GetPIContextConfidence(text string) float64
- func (pm *PatternMatcher) GetTestDataConfidence(text string) float64
- func (pm *PatternMatcher) IsConfigurationContext(text string) bool
- func (pm *PatternMatcher) IsDatabaseContext(text string) bool
- func (pm *PatternMatcher) IsDocumentationContext(text string) bool
- func (pm *PatternMatcher) IsFormFieldContext(text string) bool
- func (pm *PatternMatcher) IsLogContext(text string) bool
- func (pm *PatternMatcher) IsVariableContext(text string) bool
- type ProximityDetector
- func (pd *ProximityDetector) AnalyzeContext(content, match string, startIndex, endIndex int) ProximityResult
- func (pd *ProximityDetector) AnalyzeFile(filename string, content string, findings []detection.Finding) []detection.Finding
- func (pd *ProximityDetector) CalculateProximityScore(distance int, contextType PIContextType) float64
- func (pd *ProximityDetector) EnhanceFinding(finding *detection.Finding, content string)
- func (pd *ProximityDetector) IdentifyPIContext(content, match string, startIndex, endIndex int) PIContextInfo
- func (pd *ProximityDetector) IsTestData(filename, content, match string, startIndex, endIndex int) bool
- type ProximityResult
- type SemanticAnalysis
- type StructureAnalysis
- type StructureType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContextAnalyzer ¶
type ContextAnalyzer struct {
// contains filtered or unexported fields
}
ContextAnalyzer provides utilities for analyzing the context around potential PI matches
func NewContextAnalyzer ¶
func NewContextAnalyzer() *ContextAnalyzer
NewContextAnalyzer creates a new context analyzer
func (*ContextAnalyzer) AnalyzeSemanticContext ¶
func (ca *ContextAnalyzer) AnalyzeSemanticContext(content string, startIndex, endIndex int) SemanticAnalysis
AnalyzeSemanticContext performs semantic analysis of the context
func (*ContextAnalyzer) AnalyzeStructure ¶
func (ca *ContextAnalyzer) AnalyzeStructure(content string, startIndex, endIndex int) StructureAnalysis
AnalyzeStructure analyzes the structure type of content around a match
func (*ContextAnalyzer) CalculateContextWindow ¶
func (ca *ContextAnalyzer) CalculateContextWindow(content string, startIndex, endIndex, baseWindow int) (beforeWindow, afterWindow int)
CalculateContextWindow calculates the optimal context window size based on content
func (*ContextAnalyzer) CountKeywords ¶
func (ca *ContextAnalyzer) CountKeywords(text string, keywords []string) int
CountKeywords counts occurrences of keywords in the text as whole words
func (*ContextAnalyzer) ExtractSurroundingText ¶
func (ca *ContextAnalyzer) ExtractSurroundingText(content string, startIndex, endIndex, windowSize int) (before, after string)
ExtractSurroundingText extracts text before and after a match within a specified window
func (*ContextAnalyzer) FindNearestKeyword ¶
func (ca *ContextAnalyzer) FindNearestKeyword(content string, keywords []string, startIndex, endIndex int) (keyword string, distance int, found bool)
FindNearestKeyword finds the nearest keyword to a match position
func (*ContextAnalyzer) GetWordProximity ¶
func (ca *ContextAnalyzer) GetWordProximity(content, targetWord string, startIndex, endIndex int) (distance int, found bool)
GetWordProximity calculates the word distance between a target word and a match position
type PIContextInfo ¶
type PIContextInfo struct {
Type PIContextType `json:"type"`
Keywords []string `json:"keywords"`
Distance int `json:"distance"`
}
PIContextInfo contains information about detected PI context
type PIContextType ¶
type PIContextType string
PIContextType represents different types of PI context
const ( PIContextLabel PIContextType = "label" // "SSN:", "Tax File Number:" PIContextForm PIContextType = "form" // HTML forms, input fields PIContextDatabase PIContextType = "database" // SQL queries, database operations PIContextLog PIContextType = "log" // Log entries PIContextConfig PIContextType = "config" // Configuration files PIContextVariable PIContextType = "variable" // Variable assignments PIContextDocumentation PIContextType = "documentation" // Comments, docs PIContextProduction PIContextType = "production" // Regular production code PIContextTest PIContextType = "test" // Test/mock/sample data )
type PatternMatcher ¶
type PatternMatcher struct {
// contains filtered or unexported fields
}
PatternMatcher provides methods to identify various patterns that indicate PI context vs test data
func NewPatternMatcher ¶
func NewPatternMatcher() *PatternMatcher
NewPatternMatcher creates a new pattern matcher with compiled patterns
func (*PatternMatcher) AnalyzeContextType ¶
func (pm *PatternMatcher) AnalyzeContextType(text string) PIContextType
AnalyzeContextType determines the most likely context type
func (*PatternMatcher) ContainsTestDataKeywords ¶
func (pm *PatternMatcher) ContainsTestDataKeywords(text string) bool
ContainsTestDataKeywords checks if the text contains keywords that indicate test/mock data
func (*PatternMatcher) ExtractRelevantKeywords ¶
func (pm *PatternMatcher) ExtractRelevantKeywords(text string) []string
ExtractRelevantKeywords extracts keywords that are relevant for context analysis
func (*PatternMatcher) FindPIContextLabels ¶
func (pm *PatternMatcher) FindPIContextLabels(text string) []string
FindPIContextLabels finds PI context labels in the text
func (*PatternMatcher) GetPIContextConfidence ¶
func (pm *PatternMatcher) GetPIContextConfidence(text string) float64
GetPIContextConfidence returns a confidence score for PI context detection
func (*PatternMatcher) GetTestDataConfidence ¶
func (pm *PatternMatcher) GetTestDataConfidence(text string) float64
GetTestDataConfidence returns a confidence score for test data detection
func (*PatternMatcher) IsConfigurationContext ¶
func (pm *PatternMatcher) IsConfigurationContext(text string) bool
IsConfigurationContext checks if the text appears to be configuration related
func (*PatternMatcher) IsDatabaseContext ¶
func (pm *PatternMatcher) IsDatabaseContext(text string) bool
IsDatabaseContext checks if the text appears to be database query related
func (*PatternMatcher) IsDocumentationContext ¶
func (pm *PatternMatcher) IsDocumentationContext(text string) bool
IsDocumentationContext checks if the text appears to be documentation/comments
func (*PatternMatcher) IsFormFieldContext ¶
func (pm *PatternMatcher) IsFormFieldContext(text string) bool
IsFormFieldContext checks if the text appears to be form field related
func (*PatternMatcher) IsLogContext ¶
func (pm *PatternMatcher) IsLogContext(text string) bool
IsLogContext checks if the text appears to be log entry related
func (*PatternMatcher) IsVariableContext ¶
func (pm *PatternMatcher) IsVariableContext(text string) bool
IsVariableContext checks if the text appears to be variable assignment related
type ProximityDetector ¶
type ProximityDetector struct {
// contains filtered or unexported fields
}
ProximityDetector analyzes the context around potential PI to improve detection accuracy
Example ¶
ExampleProximityDetector demonstrates how to use the proximity detector
detector := NewProximityDetector()
// Example 1: Test data
content1 := "// Test TFN: 123 456 789 for unit testing"
result1 := detector.AnalyzeContext(content1, "123 456 789", 12, 23)
fmt.Printf("Test data - Score: %.2f, Is Test: %t, Reason: %s\n",
result1.Score, result1.IsTestData, result1.Reason)
// Example 2: Real PI with label
content2 := "Customer TFN: 123 456 789"
result2 := detector.AnalyzeContext(content2, "123 456 789", 14, 25)
fmt.Printf("Real PI - Score: %.2f, Is Test: %t, Reason: %s\n",
result2.Score, result2.IsTestData, result2.Reason)
// Example 3: Form field
content3 := `<input type="text" name="tfn" value="123 456 789">`
result3 := detector.AnalyzeContext(content3, "123 456 789", 37, 48)
fmt.Printf("Form field - Score: %.2f, Context: %s, Reason: %s\n",
result3.Score, result3.Context, result3.Reason)
Output: Test data - Score: 0.10, Is Test: true, Reason: test data indicator Real PI - Score: 0.90, Is Test: false, Reason: PI context label detected Form field - Score: 0.80, Context: form, Reason: form field context
func NewProximityDetector ¶
func NewProximityDetector() *ProximityDetector
NewProximityDetector creates a new proximity detector
func (*ProximityDetector) AnalyzeContext ¶
func (pd *ProximityDetector) AnalyzeContext(content, match string, startIndex, endIndex int) ProximityResult
AnalyzeContext performs comprehensive context analysis around a potential PI match
func (*ProximityDetector) AnalyzeFile ¶
func (pd *ProximityDetector) AnalyzeFile(filename string, content string, findings []detection.Finding) []detection.Finding
AnalyzeFile performs proximity analysis on an entire file's findings
func (*ProximityDetector) CalculateProximityScore ¶
func (pd *ProximityDetector) CalculateProximityScore(distance int, contextType PIContextType) float64
CalculateProximityScore calculates a proximity score based on distance and context type
func (*ProximityDetector) EnhanceFinding ¶
func (pd *ProximityDetector) EnhanceFinding(finding *detection.Finding, content string)
EnhanceFinding enhances a detection finding with proximity analysis
func (*ProximityDetector) IdentifyPIContext ¶
func (pd *ProximityDetector) IdentifyPIContext(content, match string, startIndex, endIndex int) PIContextInfo
IdentifyPIContext identifies the type of context where PI appears
func (*ProximityDetector) IsTestData ¶
func (pd *ProximityDetector) IsTestData(filename, content, match string, startIndex, endIndex int) bool
IsTestData determines if the match appears to be test/mock/sample data
type ProximityResult ¶
type ProximityResult struct {
Score float64 `json:"score"`
Reason string `json:"reason"`
Context PIContextType `json:"context"`
Keywords []string `json:"keywords"`
Structure StructureAnalysis `json:"structure"`
Semantic SemanticAnalysis `json:"semantic"`
IsTestData bool `json:"is_test_data"`
}
ProximityResult represents the result of proximity context analysis
type SemanticAnalysis ¶
type SemanticAnalysis struct {
Confidence float64 `json:"confidence"`
Indicators []string `json:"indicators"`
PITypes []string `json:"pi_types"`
}
SemanticAnalysis contains semantic context information
type StructureAnalysis ¶
type StructureAnalysis struct {
Type StructureType `json:"type"`
NestingLevel int `json:"nesting_level"`
ElementType string `json:"element_type,omitempty"`
}
StructureAnalysis contains information about content structure
type StructureType ¶
type StructureType string
StructureType represents different content structure types
const ( StructureJSON StructureType = "json" StructureXML StructureType = "xml" StructureHTML StructureType = "html" StructureSQL StructureType = "sql" StructureYAML StructureType = "yaml" StructureCode StructureType = "code" StructurePlainText StructureType = "plain_text" StructureURL StructureType = "url" )