Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func getAmount(tokenArray: [String], j: Int) -> Double{
- var amount = ""
- for k in j+1...tokenArray.count - 1
- {
- //recognize integers with attached unit fix
- amount = tokenArray[k].replacingOccurrences(of: "g", with: "")
- amount = amount.replacingOccurrences(of: "m", with: "")
- let stringAmount = amount
- if(stringAmount == "09"){ // if this is true then Tesseract mistook 0g for "09", since if the amount was 9 there wouldn't be a preceding zero
- return 0
- }
- //check for valid number
- else if Double(amount) != nil
- {
- return Double(amount)!
- }
- }
- return 0
- }
- //finds out if 2 strings have a majority of matching characters
- //returns: Boolean
- func matchMajority(nutrient: String, scannedN: String) -> Bool{
- var numMatched = 0 //counter for characters matched
- let majority = (nutrient.characters.count) / 2 //50% of letters in String to be scanned
- var count: Int //the number of letters to scan through
- //find out which word is the smaller one
- if(nutrient.characters.count < scannedN.characters.count){
- count = nutrient.characters.count
- }
- else{
- count = scannedN.characters.count
- }
- var aa = Array(nutrient.uppercased().characters) //turn Strings into array of chars cuz Swift is annoying
- var ba = Array(scannedN.uppercased().characters) //also make sure both are upper cased
- if(count > 0){
- //search through each letter
- for index in 0...count - 1{
- var a: Character
- var b: Character
- a = aa[index]
- b = ba[index]
- if (a == b){
- numMatched += 1 //increment if letters match
- }
- }
- }
- if(numMatched >= majority){
- return true
- }
- else {
- return false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement