Last active
July 11, 2018 01:29
-
-
Save tbass134/26d880cdb2c7f11ed7a13aa6f610323f to your computer and use it in GitHub Desktop.
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
func toOneHot(_ string:String) -> [Double] { | |
var str = string | |
var items = [Double](repeating: 0.0, count: 7) | |
let weather_conds:[String] = ["Clear", "Clouds", "Fog", "Haze", "Rain", "Smoke", "Snow"] | |
if str.lowercased().range(of:"cloud") != nil || str.lowercased().range(of:"overcast") != nil{ | |
str = "Clouds" | |
} | |
if str.lowercased().range(of:"snow") != nil { | |
str = "Snow" | |
} | |
if str.lowercased().range(of:"rain") != nil || str.lowercased().range(of:"drizzle") != nil || str.lowercased().range(of:"mist") != nil{ | |
str = "Rain" | |
} | |
if str.lowercased().range(of:"none") != nil { | |
str = "Clear" | |
} | |
guard let index = weather_conds.index(of: str) else { | |
items[0] = 1 | |
return items | |
} | |
items[index] = 1 | |
return items | |
} |
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
func predict(_ location:CLLocationCoordinate2D) { | |
self.class_image.image = nil | |
self.predict_label.text = "" | |
guard let location = self.lastLocation else { | |
return | |
} | |
OpenWeatherAPI.sharedInstance.weatherDataFor(location: location, completion: { | |
(response: JSON?) in | |
guard let json = response else { | |
return | |
} | |
self.weatherView?.weatherData = json | |
self.weatherView?.view.isHidden = false | |
if #available(iOS 11.0, *) { | |
let model = coffee_prediction() | |
guard let mlMultiArray = try? MLMultiArray(shape:[12,1], dataType:MLMultiArrayDataType.double) else { | |
fatalError("Unexpected runtime error. MLMultiArray") | |
} | |
var values = [json["clouds"]["all"].doubleValue, | |
json["main"]["humidity"].doubleValue, | |
round(json["main"]["temp"].doubleValue), | |
round(json["visibility"].doubleValue / 1609.344), | |
round(json["wind"]["speed"].doubleValue), | |
] | |
values.append(contentsOf: self.toOneHot(json["weather"][0]["main"].stringValue)) | |
for (index, element) in values.enumerated() { | |
mlMultiArray[index] = NSNumber(floatLiteral: element ) | |
} | |
let input = coffee_predictionInput(input: mlMultiArray) | |
guard let prediction = try? model.prediction(input: input) else { | |
return | |
} | |
let result = prediction | |
print("classLabel \(result.classLabel)") | |
if result.classLabel == 1 { | |
self.class_image.image = UIImage.init(named: "coffee_hot") | |
self.predict_label.text = "Hot Coffee" | |
} else { | |
self.class_image.image = UIImage.init(named: "coffee_iced") | |
self.predict_label.text = "Iced Coffee" | |
} | |
let percent = Int(round(result.classProbability[result.classLabel]! * 100)) | |
self.predict_label.text = self.predict_label.text! + "\n(\(percent)% probability)" | |
print(result.classProbability) | |
// self.predict_label.text = self.predict_label.text | |
let loc = CLLocation(latitude: location.latitude, longitude: location.longitude) | |
print("loc",loc) | |
CLGeocoder().reverseGeocodeLocation(loc, completionHandler: {(placemarks, error) -> Void in | |
if error != nil { | |
print("Reverse geocoder failed with error" + (error?.localizedDescription)!) | |
return | |
} | |
guard let pm = placemarks?.first, let locality = pm.locality, let administrativeArea = pm.administrativeArea else { | |
return | |
} | |
self.weatherView?.locationLabel.text = locality + ", " + administrativeArea | |
}) | |
} else { | |
// Fallback on earlier versions | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment