Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.23 KB | None | 0 0
  1.     func getAmount(tokenArray: [String], j: Int) -> Double{
  2.         var amount = ""
  3.         for k in j+1...tokenArray.count - 1
  4.         {
  5.             //recognize integers with attached unit fix
  6.             amount = tokenArray[k].replacingOccurrences(of: "g", with: "")
  7.             amount = amount.replacingOccurrences(of: "m", with: "")
  8.             let stringAmount = amount
  9.             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
  10.                 return 0
  11.             }
  12.             //check for valid number
  13.             else if Double(amount) != nil
  14.             {
  15.                 return Double(amount)!
  16.             }
  17.         }
  18.         return 0
  19.     }
  20.    
  21.  
  22.    
  23.     //finds out if 2 strings have a majority of matching characters
  24.     //returns: Boolean
  25.     func matchMajority(nutrient: String, scannedN: String) -> Bool{
  26.         var numMatched = 0                                  //counter for characters matched
  27.         let majority = (nutrient.characters.count) / 2      //50% of letters in String to be scanned
  28.         var count: Int                                      //the number of letters to scan through
  29.        
  30.         //find out which word is the smaller one
  31.         if(nutrient.characters.count < scannedN.characters.count){
  32.             count = nutrient.characters.count
  33.         }
  34.         else{
  35.             count = scannedN.characters.count
  36.         }
  37.        
  38.         var aa = Array(nutrient.uppercased().characters)         //turn Strings into array of chars cuz Swift is annoying
  39.         var ba = Array(scannedN.uppercased().characters)         //also make sure both are upper cased
  40.        
  41.         if(count > 0){
  42.         //search through each letter
  43.         for index in 0...count - 1{
  44.             var a: Character
  45.             var b: Character
  46.          
  47.             a = aa[index]
  48.             b = ba[index]
  49.            
  50.             if (a == b){
  51.                 numMatched += 1                            //increment if letters match
  52.             }
  53.         }
  54.         }
  55.         if(numMatched >= majority){
  56.             return true
  57.         }
  58.         else {
  59.             return false
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement