-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ocr filter that detects and fixes some common ocr errors, activat…
…e using the 'ocr' filter, or the new 'all' filter
- Loading branch information
1 parent
b96fd96
commit 578c6a2
Showing
4 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package subtitles | ||
|
||
import ( | ||
"strings" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
ocrErrors = map[string]string{ | ||
"s0 ": "so ", | ||
"g0 ": "go ", | ||
"0n ": "on ", | ||
"c0uld": "could", | ||
"s0mething": "something", | ||
"l've": "i've", | ||
} | ||
) | ||
|
||
// filterOCR corrects some OCR mistakes | ||
func (subtitle *Subtitle) filterOCR() *Subtitle { | ||
for _, cap := range subtitle.Captions { | ||
for i, org := range cap.Text { | ||
for bad, good := range ocrErrors { | ||
// lower case | ||
cap.Text[i] = strings.Replace(cap.Text[i], bad, good, -1) | ||
|
||
// upper case | ||
cap.Text[i] = strings.Replace(cap.Text[i], strings.ToUpper(bad), strings.ToUpper(good), -1) | ||
|
||
// ucfirst | ||
cap.Text[i] = strings.Replace(cap.Text[i], strings.Title(bad), strings.Title(good), -1) | ||
} | ||
|
||
if org != cap.Text[i] { | ||
log.Println("[ocr]", org, "->", cap.Text[i]) | ||
} | ||
} | ||
} | ||
return subtitle | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package subtitles | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFilterOCRLower(t *testing.T) { | ||
|
||
in := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 4, 630), | ||
makeTime(0, 0, 6, 18), | ||
[]string{"s0mething good"}, | ||
}}} | ||
|
||
expected := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 4, 630), | ||
makeTime(0, 0, 6, 18), | ||
[]string{"something good"}, | ||
}}} | ||
|
||
assert.Equal(t, &expected, in.filterOCR()) | ||
} | ||
|
||
func TestFilterOCRUpper(t *testing.T) { | ||
|
||
in := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 4, 630), | ||
makeTime(0, 0, 6, 18), | ||
[]string{"S0METHING GOOD"}, | ||
}}} | ||
|
||
expected := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 4, 630), | ||
makeTime(0, 0, 6, 18), | ||
[]string{"SOMETHING GOOD"}, | ||
}}} | ||
|
||
assert.Equal(t, &expected, in.filterOCR()) | ||
} | ||
|
||
func TestFilterOCRUcFirst(t *testing.T) { | ||
|
||
in := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 4, 630), | ||
makeTime(0, 0, 6, 18), | ||
[]string{"S0mething good"}, | ||
}}} | ||
|
||
expected := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 4, 630), | ||
makeTime(0, 0, 6, 18), | ||
[]string{"Something good"}, | ||
}}} | ||
|
||
assert.Equal(t, &expected, in.filterOCR()) | ||
} | ||
|
||
func TestFilterOCREnglish(t *testing.T) { | ||
|
||
in := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 4, 630), | ||
makeTime(0, 0, 6, 18), | ||
[]string{"l've got a feeling"}, | ||
}}} | ||
|
||
expected := Subtitle{[]Caption{{ | ||
1, | ||
makeTime(0, 0, 4, 630), | ||
makeTime(0, 0, 6, 18), | ||
[]string{"i've got a feeling"}, | ||
}}} | ||
|
||
assert.Equal(t, &expected, in.filterOCR()) | ||
} |