|
| 1 | +import { cuss } from 'cuss' |
| 2 | +import { cuss as cussPt } from 'cuss/pt' |
| 3 | +import { cuss as cussFr } from 'cuss/fr' |
| 4 | +import { cuss as cussEs } from 'cuss/es' |
| 5 | +import { Language } from '@horizon-rs/language-guesser' |
| 6 | + |
| 7 | +const language = new Language() |
| 8 | + |
| 9 | +// Exported for the debugging CLI script |
| 10 | +export const SIGNAL_RATINGS = [ |
| 11 | + { |
| 12 | + reduction: 1.0, |
| 13 | + name: 'email-only', |
| 14 | + validator: (comment) => isEmailOnly(comment), |
| 15 | + }, |
| 16 | + { |
| 17 | + reduction: 0.2, |
| 18 | + name: 'contains-email', |
| 19 | + validator: (comment) => isContainingEmail(comment), |
| 20 | + }, |
| 21 | + { |
| 22 | + reduction: 0.1, |
| 23 | + name: 'url-only', |
| 24 | + validator: (comment) => isURL(comment), |
| 25 | + }, |
| 26 | + { |
| 27 | + reduction: 0.1, |
| 28 | + name: 'numbers-only', |
| 29 | + validator: (comment) => isNumbersOnly(comment), |
| 30 | + }, |
| 31 | + { |
| 32 | + reduction: 0.1, |
| 33 | + name: 'all-uppercase', |
| 34 | + validator: (comment) => isAllUppercase(comment), |
| 35 | + }, |
| 36 | + { |
| 37 | + reduction: 0.1, |
| 38 | + name: 'too-short', |
| 39 | + validator: (comment) => isTooShort(comment), |
| 40 | + }, |
| 41 | + { |
| 42 | + reduction: 0.2, |
| 43 | + name: 'not-language', |
| 44 | + validator: (comment, language) => isNotLanguage(comment, language), |
| 45 | + }, |
| 46 | + { |
| 47 | + reduction: 0.3, |
| 48 | + name: 'cuss-words-likely', |
| 49 | + validator: (comment, language) => isLikelyCussWords(comment, language), |
| 50 | + }, |
| 51 | + { |
| 52 | + reduction: 0.1, |
| 53 | + name: 'cuss-words-maybe', |
| 54 | + validator: (comment, language) => isMaybeCussWords(comment, language), |
| 55 | + }, |
| 56 | +] |
| 57 | + |
| 58 | +export async function analyzeComment(text, language = 'en') { |
| 59 | + const signals = [] |
| 60 | + let rating = 1.0 |
| 61 | + for (const { reduction, name, validator } of SIGNAL_RATINGS) { |
| 62 | + if (validator(text, language)) { |
| 63 | + signals.push(name) |
| 64 | + rating -= reduction |
| 65 | + } |
| 66 | + if (rating <= 0) break |
| 67 | + } |
| 68 | + |
| 69 | + return { signals, rating } |
| 70 | +} |
| 71 | + |
| 72 | +function isEmailOnly(text) { |
| 73 | + if (text.includes('@') && !/\s/.test(text.trim()) && !text.includes('://')) { |
| 74 | + const atSigns = text.split('@').length |
| 75 | + if (atSigns === 2) { |
| 76 | + return true |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function isContainingEmail(text) { |
| 82 | + if (text.includes('@') && !isEmailOnly(text)) { |
| 83 | + // Don't use splitWords() here because `foo@example.com` will be |
| 84 | + // split up into ['foo', 'example.com']. |
| 85 | + return text.split(/\s+/g).some((word) => isEmailOnly(word)) |
| 86 | + } |
| 87 | + return false |
| 88 | +} |
| 89 | + |
| 90 | +function isURL(text) { |
| 91 | + if (!text.trim().includes(' ')) { |
| 92 | + if (URL.canParse(text.trim())) return true |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function isNumbersOnly(text) { |
| 97 | + return /^\d+$/.test(text.replace(/\s/g, '')) |
| 98 | +} |
| 99 | + |
| 100 | +function isAllUppercase(text) { |
| 101 | + return /[A-Z]/.test(text) && text === text.toUpperCase() |
| 102 | +} |
| 103 | + |
| 104 | +function isTooShort(text) { |
| 105 | + const split = text.trim().split(/\s+/) |
| 106 | + if (split.length <= 1) { |
| 107 | + // return !isNumbersOnly(text) && !isURL(text) && !isEmailOnly(text) && !isAllUppercase(text) |
| 108 | + return true |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function isNotLanguage(text, language_) { |
| 113 | + const bestGuess = language.guessBest(text.trim()) |
| 114 | + if (!bestGuess) return true // Can happen if the text is just whitespace |
| 115 | + // @horizon-rs/language-guesser is based on tri-grams and can lead |
| 116 | + // to false positives. For example, it thinks that 'Thamk you ❤️🙏' is |
| 117 | + // Haitian! And that 'I wanne robux 1000' is Polish! |
| 118 | + // But that's because they are short and there's not enough clues to |
| 119 | + // guess what language it is. You and I might know those are actually |
| 120 | + // attempts to be English, despite the spelling. |
| 121 | + // But are they useful comments? Given that this is just a signal, |
| 122 | + // and not a hard blocker, it's more of a clue than a fact. |
| 123 | + return bestGuess.alpha2 !== language_ |
| 124 | +} |
| 125 | + |
| 126 | +function getCussWords(lang) { |
| 127 | + switch (lang) { |
| 128 | + case 'pt': |
| 129 | + return cussPt |
| 130 | + case 'fr': |
| 131 | + return cussFr |
| 132 | + case 'es': |
| 133 | + return cussEs |
| 134 | + default: |
| 135 | + return cuss |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +function isLikelyCussWords(text, language_, rating = 2) { |
| 140 | + const cussWords = getCussWords(language_) |
| 141 | + for (const word of splitWords(text, language_ || 'en')) { |
| 142 | + if (cussWords[word] && cussWords[word] === rating) { |
| 143 | + return true |
| 144 | + } |
| 145 | + } |
| 146 | + return false |
| 147 | +} |
| 148 | + |
| 149 | +function isMaybeCussWords(text, language_) { |
| 150 | + return isLikelyCussWords(text, language_, 1) |
| 151 | +} |
| 152 | + |
| 153 | +const segmenter = new Intl.Segmenter([], { granularity: 'word' }) |
| 154 | + |
| 155 | +function splitWords(text) { |
| 156 | + const segmentedText = segmenter.segment(text) |
| 157 | + return [...segmentedText].filter((s) => s.isWordLike).map((s) => s.segment) |
| 158 | +} |
0 commit comments