Repository navigation
ios: show the meter's brand beside the number candidates #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
02d74f6
ios: show the meter's brand beside the number candidates
SamGu-NRX 916838f
Merge remote-tracking branch 'origin/t3/ios-mvf' into t3/meter-brand
SamGu-NRX 659e5ae
ios: 44 pt reject target, and the brand lives only beside its number
SamGu-NRX 0a3a05e
Merge remote-tracking branch 'origin/t3/ios-mvf' into t3/meter-brand
SamGu-NRX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
131 changes: 131 additions & 0 deletions
131
ios/HouseScanKit/Sources/HouseScanKit/Meter/MeterBrand.swift
This file contains hidden or 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,131 @@ | ||
| import Foundation | ||
|
|
||
| // Names the meter's maker from the lines Vision read on the close-up. A port of the autodetect | ||
| // lab's brands.py and its pick rule (experiments/autodetect/meter_brand/ on t3/autodetect-lab at | ||
| // 538f60e): on #16's photos it named the maker correctly on 17 of 19 where the name is printed in | ||
| // plain letters and 22 of 42 where it is part of a logo, named none on the 9 without a name, and | ||
| // once named the wrong one, on a plate printed with two makers' names. | ||
|
|
||
| public enum MeterBrand { | ||
| /// A maker, as shown to the homeowner, and its spellings as printed on meters. Each printed | ||
| /// name is its own brand, even where one company later bought another (Elster and Honeywell, | ||
| /// Actaris and Itron), because the phone reports what is printed. A spelling of 4 characters | ||
| /// or fewer must be a whole token; one of 6 letters or more may be one edit away. | ||
| public static let makers: [(name: String, spellings: [String])] = [ | ||
| ("Itron", ["ITRON"]), | ||
| ("Actaris", ["ACTARIS"]), | ||
| ("Schlumberger", ["SCHLUMBERGER"]), | ||
| ("Landis+Gyr", ["LANDIS+GYR", "LANDIS GYR", "LANDIS & GYR", "LANDIS&GYR", "L+G", "LANDIS"]), | ||
| ("GE", ["GE", "GENERAL ELECTRIC"]), | ||
| ("Aclara", ["ACLARA"]), | ||
| ("Elster", ["ELSTER"]), | ||
| ("Honeywell", ["HONEYWELL"]), | ||
| ("ABB", ["ABB"]), | ||
| ("Sensus", ["SENSUS"]), | ||
| ("Sangamo", ["SANGAMO"]), | ||
| ("Westinghouse", ["WESTINGHOUSE"]), | ||
| ("Duncan", ["DUNCAN"]), | ||
| ("Siemens", ["SIEMENS"]), | ||
| ("AEG", ["AEG"]), | ||
| ("Iskra", ["ISKRA", "ISKRAEMECO"]), | ||
| ("EMH", ["EMH"]), | ||
| ("DZG", ["DZG"]), | ||
| ("Kamstrup", ["KAMSTRUP"]), | ||
| ("EDMI", ["EDMI"]), | ||
| ("ZIV", ["ZIV"]), | ||
| ("Sagemcom", ["SAGEMCOM", "SAGEM"]), | ||
| ("Hexing", ["HEXING"]), | ||
| ("Holley", ["HOLLEY"]), | ||
| ("Genus", ["GENUS"]), | ||
| ("Secure", ["SECURE METERS"]), | ||
| ("Toshiba", ["TOSHIBA"]), | ||
| ("Mitsubishi", ["MITSUBISHI"]), | ||
| ("Osaki", ["OSAKI"]), | ||
| ("Tatung", ["TATUNG"]), | ||
| ("Chung-Hsin", ["CHUNGHSIN", "CHUNG HSIN", "CHUNG-HSIN"]), | ||
| ("CGE", ["CGE"]), | ||
| ("Enermet", ["ENERMET"]), | ||
| ("Ampy", ["AMPY"]), | ||
| ("Logarex", ["LOGAREX"]), | ||
| ("Echelon", ["ECHELON"]), | ||
| ] | ||
|
|
||
| /// Every spelling with its maker, longest first, so `LANDIS & GYR` is tried before `LANDIS` | ||
| /// and `GENERAL ELECTRIC` before `GE`. Equal lengths keep list order, as Python's stable sort. | ||
| private static let spellings: [(spelling: String, maker: String)] = makers | ||
| .flatMap { maker in maker.spellings.map { (spelling: $0, maker: maker.name) } } | ||
| .enumerated() | ||
| .sorted { $0.element.spelling.count != $1.element.spelling.count ? $0.element.spelling.count > $1.element.spelling.count : $0.offset < $1.offset } | ||
| .map(\.element) | ||
|
|
||
| /// The maker to show for the close-up: the one named on the tallest line that names any, or | ||
| /// nil. Equal heights go to the maker whose name sorts last, as brands.py's `max` over | ||
| /// (height, name) does. | ||
| public static func read(_ lines: [MeterTextLine]) -> String? { | ||
| lines.compactMap { line in match(line.text).map { (height: line.box.height, maker: $0) } } | ||
| .max { ($0.height, $0.maker.uppercased()) < ($1.height, $1.maker.uppercased()) }? | ||
| .maker | ||
| } | ||
|
|
||
| /// The maker one recognized line names, or nil. | ||
| /// | ||
| /// Long spellings match as a substring of the line, or one edit from a token or a pair of | ||
| /// adjacent tokens (Vision drops or swaps a letter, or splits `LANDIS+GYR`). Short spellings | ||
| /// such as `GE` must be a whole token, because they occur inside unrelated words. The edit | ||
| /// needs 6 letters: at 5, `GENUS` is one edit from `GE US`. | ||
| public static func match(_ line: String) -> String? { | ||
| let norm = normalized(line) | ||
| guard !norm.isEmpty else { return nil } | ||
| let tokens = norm.split(separator: " ").map(String.init) | ||
| let pairs = zip(tokens, tokens.dropFirst()) | ||
| let windows = tokens + pairs.map { $0 + $1 } + pairs.map { $0 + " " + $1 } | ||
| let squeezed = norm.replacingOccurrences(of: " ", with: "") | ||
| for (spelling, maker) in spellings { | ||
| if spelling.count <= 4 { | ||
| if tokens.contains(spelling) { return maker } | ||
| continue | ||
| } | ||
| let bare = spelling.replacingOccurrences(of: " ", with: "") | ||
| if (" " + norm + " ").contains(" " + spelling + " ") || squeezed.contains(bare) { return maker } | ||
| if bare.count >= 6, | ||
| windows.contains(where: { withinOneEdit(bare, $0.replacingOccurrences(of: " ", with: "")) }) { | ||
| return maker | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| /// Accents folded to plain letters (`Itrón` reads as ITRON) and other non-ASCII dropped, upper | ||
| /// case, `&` and `+` kept because they are part of names, other characters to spaces, runs of | ||
| /// spaces collapsed. | ||
| static func normalized(_ text: String) -> String { | ||
| let ascii = String(String.UnicodeScalarView(text.decomposedStringWithCompatibilityMapping.unicodeScalars.filter(\.isASCII))) | ||
| let kept = ascii.uppercased().map { character -> Character in | ||
| character.isASCII && (character.isUppercase || character.isNumber || character == "+" || character == "&") ? character : " " | ||
| } | ||
| return String(kept).split(separator: " ").joined(separator: " ") | ||
| } | ||
|
|
||
| static func withinOneEdit(_ a: String, _ b: String) -> Bool { | ||
| if a == b { return true } | ||
| var short = Array(a) | ||
| var long = Array(b) | ||
| guard abs(short.count - long.count) <= 1 else { return false } | ||
| if short.count > long.count { swap(&short, &long) } | ||
| var i = 0 | ||
| var j = 0 | ||
| var edited = false | ||
| while i < short.count, j < long.count { | ||
| if short[i] == long[j] { | ||
| i += 1 | ||
| j += 1 | ||
| continue | ||
| } | ||
| if edited { return false } | ||
| edited = true | ||
| if short.count == long.count { i += 1 } | ||
| j += 1 | ||
| } | ||
| return true | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.