Skip to content

Commit

Permalink
Add option to disable l to I correction
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <[email protected]>
  • Loading branch information
ecdye committed Oct 24, 2024
1 parent 9dbd08f commit a5892ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 10 additions & 4 deletions Sources/macSubtitleOCR/Subtitles/SubtitleProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,23 @@ struct SubtitleProcessor {
private let language: String
private let fastMode: Bool
private let disableLanguageCorrection: Bool
private let disableICorrection: Bool
private let forceOldAPI: Bool
private let outputDirectory: String
private let maxConcurrentTasks: Int
private let logger = Logger(subsystem: "github.ecdye.macSubtitleOCR", category: "SubtitleProcessor")

init(subtitles: [Subtitle], trackNumber: Int, invert: Bool, saveImages: Bool, language: String, fastMode: Bool,
disableLanguageCorrection: Bool,
forceOldAPI: Bool, outputDirectory: String, maxConcurrentTasks: Int) {
disableLanguageCorrection: Bool, disableICorrection: Bool, forceOldAPI: Bool, outputDirectory: String,
maxConcurrentTasks: Int) {
self.subtitles = subtitles
self.trackNumber = trackNumber
self.invert = invert
self.saveImages = saveImages
self.language = language
self.fastMode = fastMode
self.disableLanguageCorrection = disableLanguageCorrection
self.disableICorrection = disableICorrection
self.forceOldAPI = forceOldAPI
self.outputDirectory = outputDirectory
self.maxConcurrentTasks = maxConcurrentTasks
Expand Down Expand Up @@ -103,8 +105,12 @@ struct SubtitleProcessor {
}

let (subtitleText, subtitleLines) = await recognizeText(from: subImage)
let pattern = #"\bl\b"# // Replace l with I when it's a single character
subtitle.text = subtitleText.replacingOccurrences(of: pattern, with: "I", options: .regularExpression)
if language.contains("en"), !disableICorrection {
let pattern = #"\bl\b"# // Replace l with I when it's a single character
subtitle.text = subtitleText.replacingOccurrences(of: pattern, with: "I", options: .regularExpression)

Check warning on line 110 in Sources/macSubtitleOCR/Subtitles/SubtitleProcessor.swift

View workflow job for this annotation

GitHub Actions / Lint

Wrap lines that exceed the specified maximum width. (wrap)
} else {
subtitle.text = subtitleText
}
subtitle.imageData = nil // Clear the image data to save memory

let jsonOut = SubtitleJSONResult(index: subIndex, lines: subtitleLines, text: subtitleText)
Expand Down
7 changes: 5 additions & 2 deletions Sources/macSubtitleOCR/macSubtitleOCR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ struct ExperimentalOptions: ParsableArguments {
var disableLanguageCorrection = false
}

// The main struct representing the macSubtitleOCR command-line tool.
@main
struct macSubtitleOCR: AsyncParsableCommand {
// MARK: - Properties

static let configuration = CommandConfiguration(
commandName: "macSubtitleOCR",
abstract: "macSubtitleOCR - Convert bitmap subtitles into SubRip format using the macOS OCR engine")
abstract: "macSubtitleOCR - Convert bitmap subtitles into SubRip format using the macOS Vision framework")

@Argument(help: "Input subtitle file (supported formats: .sup, .sub, .idx, .mkv)")
var input: String
Expand Down Expand Up @@ -63,6 +62,9 @@ struct macSubtitleOCR: AsyncParsableCommand {
@Flag(help: "Use FFmpeg decoder")
var ffmpegDecoder = false

@Flag(help: "Disable correction of 'l' to 'I' in OCR results")
var disableICorrection = false

@OptionGroup(title: "Experimental Options", visibility: .hidden)
var experimentalOptions: ExperimentalOptions

Expand Down Expand Up @@ -164,6 +166,7 @@ struct macSubtitleOCR: AsyncParsableCommand {
language: languages,
fastMode: experimentalOptions.fastMode,
disableLanguageCorrection: experimentalOptions.disableLanguageCorrection,
disableICorrection: disableICorrection,
forceOldAPI: experimentalOptions.forceOldAPI,
outputDirectory: outputDirectory,
maxConcurrentTasks: maxThreads)
Expand Down

0 comments on commit a5892ec

Please sign in to comment.