Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.03 KB | None | 0 0
  1.     func separate(strin: String) -> FoodItem{
  2.        
  3.         //initialize a foodItem
  4.         let newItem: FoodItem = FoodItem.init(nam: nameText.text!)
  5.        
  6.         //array of nutrient strings to look for
  7.         let nutrientNames: [String] = ["Calories", "Fat", "Cholesterol", "Sodium", "Carbohydrate", "Fibre", "Sugars", "Protein"]
  8.         var n = 0                     //counter to work through nutrient names
  9.        
  10.        
  11.         //break up translated string by line
  12.         let lineArray = strin.components(separatedBy: "\n")
  13.         for i in 0...lineArray.count - 1
  14.         {
  15.             //break up line by token
  16.             let tokenArray = lineArray[i].components(separatedBy: " ")
  17.             for j in 0...tokenArray.count - 1
  18.             {
  19.                 if(n < 8){                                // look for nutrients while not all are yet found
  20.                 var scannedString: String
  21.                 scannedString = tokenArray[j]
  22.                 if ( matchMajority(nutrient: nutrientNames[n], scannedN: scannedString ) )
  23.                 {
  24.                     //set nutrient name to
  25.                     let name = nutrientNames[n]
  26.                     //check subsequent tokens on line for a corresponding integer amount
  27.                     let amount = getAmount(tokenArray: tokenArray, j: j)
  28.                    
  29.                     //add nutrient to foodItem
  30.                     let newNutrient: Nutrient = Nutrient.init(nam: name, amoun: amount, servings: 1)
  31.                     _ = newItem.add(nutrient: newNutrient)
  32.                     n += 1                                          // look for next nutrient
  33.                 }
  34.                 }
  35.                
  36.             }
  37.         }
  38.        
  39.        
  40.         //return foodItem
  41.         return newItem
  42.     }
  43.    
  44.     func getAmount(tokenArray: [String], j: Int) -> Double{
  45.         var amount = ""
  46.         for k in j+1...tokenArray.count - 1
  47.         {
  48.             //recognize integers with attached unit fix
  49.             amount = tokenArray[k].replacingOccurrences(of: "g", with: "")
  50.             amount = amount.replacingOccurrences(of: "m", with: "")
  51.             let stringAmount = amount
  52.             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
  53.                 return 0
  54.             }
  55.             //check for valid number
  56.             else if Double(amount) != nil
  57.             {
  58.                 return Double(amount)!
  59.             }
  60.         }
  61.         return 0
  62.     }
  63.    
  64.  
  65.    
  66.     //finds out if 2 strings have a majority of matching characters
  67.     //returns: Boolean
  68.     func matchMajority(nutrient: String, scannedN: String) -> Bool{
  69.         var numMatched = 0                                  //counter for characters matched
  70.         let majority = (nutrient.characters.count) / 2      //50% of letters in String to be scanned
  71.         var count: Int                                      //the number of letters to scan through
  72.        
  73.         //find out which word is the smaller one
  74.         if(nutrient.characters.count < scannedN.characters.count){
  75.             count = nutrient.characters.count
  76.         }
  77.         else{
  78.             count = scannedN.characters.count
  79.         }
  80.        
  81.         var aa = Array(nutrient.uppercased().characters)         //turn Strings into array of chars cuz Swift is annoying
  82.         var ba = Array(scannedN.uppercased().characters)         //also make sure both are upper cased
  83.        
  84.         if(count > 0){
  85.         //search through each letter
  86.         for index in 0...count - 1{
  87.             var a: Character
  88.             var b: Character
  89.          
  90.             a = aa[index]
  91.             b = ba[index]
  92.            
  93.             if (a == b){
  94.                 numMatched += 1                            //increment if letters match
  95.             }
  96.         }
  97.         }
  98.         if(numMatched >= majority){
  99.             return true
  100.         }
  101.         else {
  102.             return false
  103.         }
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement