Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func separate(strin: String) -> FoodItem{
- //initialize a foodItem
- let newItem: FoodItem = FoodItem.init(nam: nameText.text!)
- //array of nutrient strings to look for
- let nutrientNames: [String] = ["Calories", "Fat", "Cholesterol", "Sodium", "Carbohydrate", "Fibre", "Sugars", "Protein"]
- var n = 0 //counter to work through nutrient names
- //break up translated string by line
- let lineArray = strin.components(separatedBy: "\n")
- for i in 0...lineArray.count - 1
- {
- //break up line by token
- let tokenArray = lineArray[i].components(separatedBy: " ")
- for j in 0...tokenArray.count - 1
- {
- if(n < 8){ // look for nutrients while not all are yet found
- var scannedString: String
- scannedString = tokenArray[j]
- if ( matchMajority(nutrient: nutrientNames[n], scannedN: scannedString ) )
- {
- //set nutrient name to
- let name = nutrientNames[n]
- //check subsequent tokens on line for a corresponding integer amount
- let amount = getAmount(tokenArray: tokenArray, j: j)
- //add nutrient to foodItem
- let newNutrient: Nutrient = Nutrient.init(nam: name, amoun: amount, servings: 1)
- _ = newItem.add(nutrient: newNutrient)
- n += 1 // look for next nutrient
- }
- }
- }
- }
- //return foodItem
- return newItem
- }
- 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