| 1 | package llm |
| 2 | |
| 3 | // Tier 6 Utility Functions - Shared across new intelligent systems |
| 4 | // This prevents function redeclaration issues |
| 5 | |
| 6 | // min3 returns minimum of three integers |
| 7 | func min3Tier6(a, b, c int) int { |
| 8 | if a <= b && a <= c { |
| 9 | return a |
| 10 | } |
| 11 | if b <= c { |
| 12 | return b |
| 13 | } |
| 14 | return c |
| 15 | } |
| 16 | |
| 17 | // minTier6 returns minimum of two integers |
| 18 | func minTier6(a, b int) int { |
| 19 | if a < b { |
| 20 | return a |
| 21 | } |
| 22 | return b |
| 23 | } |
| 24 | |
| 25 | // maxTier6 returns maximum of two integers |
| 26 | func maxTier6(a, b int) int { |
| 27 | if a > b { |
| 28 | return a |
| 29 | } |
| 30 | return b |
| 31 | } |
| 32 | |
| 33 | // toLowerTier6 converts string to lowercase |
| 34 | func toLowerTier6(s string) string { |
| 35 | result := "" |
| 36 | for _, r := range s { |
| 37 | if r >= 'A' && r <= 'Z' { |
| 38 | result += string(r + 32) |
| 39 | } else { |
| 40 | result += string(r) |
| 41 | } |
| 42 | } |
| 43 | return result |
| 44 | } |
| 45 | |
| 46 | // containsWordTier6 checks if text contains word |
| 47 | func containsWordTier6(text, word string) bool { |
| 48 | i := 0 |
| 49 | wordLower := toLowerTier6(word) |
| 50 | textLower := toLowerTier6(text) |
| 51 | |
| 52 | for i+len(wordLower) <= len(textLower) { |
| 53 | if textLower[i:i+len(wordLower)] == wordLower { |
| 54 | // Check word boundaries |
| 55 | beforeOk := i == 0 || !isAlphaNumTier6(rune(textLower[i-1])) |
| 56 | afterOk := i+len(wordLower) == len(textLower) || !isAlphaNumTier6(rune(textLower[i+len(wordLower)])) |
| 57 | |
| 58 | if beforeOk && afterOk { |
| 59 | return true |
| 60 | } |
| 61 | } |
| 62 | i++ |
| 63 | } |
| 64 | |
| 65 | return false |
| 66 | } |
| 67 | |
| 68 | // isAlphaNumTier6 checks if rune is alphanumeric |
| 69 | func isAlphaNumTier6(r rune) bool { |
| 70 | return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') |
| 71 | } |
| 72 | |
| 73 | // findSubstringTier6 finds substring in string |
| 74 | func findSubstringTier6(s, substr string) int { |
| 75 | if len(substr) == 0 { |
| 76 | return 0 |
| 77 | } |
| 78 | if len(substr) > len(s) { |
| 79 | return -1 |
| 80 | } |
| 81 | |
| 82 | for i := 0; i <= len(s)-len(substr); i++ { |
| 83 | if s[i:i+len(substr)] == substr { |
| 84 | return i |
| 85 | } |
| 86 | } |
| 87 | return -1 |
| 88 | } |
| 89 | |
| 90 | // levenshteinDistanceTier6 calculates edit distance between two strings |
| 91 | func levenshteinDistanceTier6(s1, s2 string) int { |
| 92 | len1, len2 := len(s1), len(s2) |
| 93 | |
| 94 | // Create matrix |
| 95 | matrix := make([][]int, len1+1) |
| 96 | for i := range matrix { |
| 97 | matrix[i] = make([]int, len2+1) |
| 98 | } |
| 99 | |
| 100 | // Initialize first row and column |
| 101 | for i := 0; i <= len1; i++ { |
| 102 | matrix[i][0] = i |
| 103 | } |
| 104 | for j := 0; j <= len2; j++ { |
| 105 | matrix[0][j] = j |
| 106 | } |
| 107 | |
| 108 | // Fill matrix |
| 109 | for i := 1; i <= len1; i++ { |
| 110 | for j := 1; j <= len2; j++ { |
| 111 | cost := 0 |
| 112 | if s1[i-1] != s2[j-1] { |
| 113 | cost = 1 |
| 114 | } |
| 115 | |
| 116 | matrix[i][j] = min3Tier6( |
| 117 | matrix[i-1][j]+1, // deletion |
| 118 | matrix[i][j-1]+1, // insertion |
| 119 | matrix[i-1][j-1]+cost, // substitution |
| 120 | ) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return matrix[len1][len2] |
| 125 | } |
| 126 | |
| 127 | // hasTagTier6 checks if tags contain target |
| 128 | func hasTagTier6(tags []InsultTag, target InsultTag) bool { |
| 129 | for _, tag := range tags { |
| 130 | if tag == target { |
| 131 | return true |
| 132 | } |
| 133 | } |
| 134 | return false |
| 135 | } |
| 136 | |
| 137 | // errorCategoriesToTagsTier6 converts error categories to insult tags |
| 138 | func errorCategoriesToTagsTier6(categories []ErrorCategory) []InsultTag { |
| 139 | tags := make([]InsultTag, 0, len(categories)) |
| 140 | |
| 141 | for _, cat := range categories { |
| 142 | switch cat { |
| 143 | case ErrorPermission: |
| 144 | tags = append(tags, TagPermission) |
| 145 | case ErrorSyntax: |
| 146 | tags = append(tags, TagSyntax) |
| 147 | case ErrorNetwork: |
| 148 | tags = append(tags, TagNetwork) |
| 149 | case ErrorMergeConflict: |
| 150 | tags = append(tags, TagMergeConflict) |
| 151 | case ErrorTestFailure: |
| 152 | tags = append(tags, TagTest) |
| 153 | case ErrorBuildFailure: |
| 154 | tags = append(tags, TagBuild) |
| 155 | case ErrorTimeout: |
| 156 | tags = append(tags, TagTimeout) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return tags |
| 161 | } |
| 162 | |
| 163 | // replaceWordTier6 replaces word in text |
| 164 | func replaceWordTier6(text, old, new string) string { |
| 165 | result := "" |
| 166 | i := 0 |
| 167 | |
| 168 | for i < len(text) { |
| 169 | if i+len(old) <= len(text) && text[i:i+len(old)] == old { |
| 170 | // Check word boundaries |
| 171 | beforeOk := i == 0 || !isAlphaNumTier6(rune(text[i-1])) |
| 172 | afterOk := i+len(old) == len(text) || !isAlphaNumTier6(rune(text[i+len(old)])) |
| 173 | |
| 174 | if beforeOk && afterOk { |
| 175 | result += new |
| 176 | i += len(old) |
| 177 | continue |
| 178 | } |
| 179 | } |
| 180 | result += string(text[i]) |
| 181 | i++ |
| 182 | } |
| 183 | |
| 184 | return result |
| 185 | } |
| 186 |