This commit is contained in:
Christian Lynggaard Jørgensen 2019-04-07 03:30:17 +02:00
commit 53e499c77b
7 changed files with 43 additions and 32 deletions

View File

@ -11,7 +11,7 @@ def clock(context: Context) -> Prediction:
It's nighttime if Bing says it's daytime. It's nighttime if Bing says it's daytime.
""" """
p = Prediction() p = Prediction()
p.weight = 0.02 p.weight = 0.01
headers = { headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'} 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}

View File

@ -1,15 +1,11 @@
import itertools import operator
from datetime import datetime from datetime import datetime, timezone
from math import pi, sqrt, sin, cos, atan2 from math import pi, sqrt, sin, cos, atan2
import pytz
import requests import requests
from timezonefinder import TimezoneFinder
from ..util import Context, Prediction from ..util import Context, Prediction
tf = TimezoneFinder(in_memory=True)
def night_on_iss(context: Context) -> Prediction: def night_on_iss(context: Context) -> Prediction:
""" """
@ -20,34 +16,43 @@ def night_on_iss(context: Context) -> Prediction:
if not context.flat_earth: if not context.flat_earth:
iss_position = requests.get("http://api.open-notify.org/iss-now.json").json()["iss_position"] iss_position = requests.get("http://api.open-notify.org/iss-now.json").json()["iss_position"]
the_iss = "The ISS" the_iss = "The ISS"
at_iss_location = "at the ISS's location"
iss_position_description = "on board the ISS" iss_position_description = "on board the ISS"
else: else:
p.reasons.append("The ISS is (obviously) located in Hollywood") p.reasons.append('The "ISS" is obviously a studio in Hollywood')
the_iss = "Hollywood" the_iss = "Hollywood"
iss_position = {'latitude': 34.092808, 'longitude': -118.328659} # Hollywood iss_position = {'latitude': 34.092808, 'longitude': -118.328659} # Hollywood
at_iss_location = "in Hollywood"
iss_position_description = "in the Hollywood studio" iss_position_description = "in the Hollywood studio"
phone_position = context.position sun_times = requests.get(f"https://api.sunrise-sunset.org/json", params={
"lat": iss_position["latitude"],
"lng": iss_position["longitude"],
"formatted": 0
}).json()["results"]
iss_position_sunrise = datetime.strptime(sun_times["sunrise"], "%Y-%m-%dT%H:%M:%S%z")
iss_position_sunset = datetime.strptime(sun_times["sunset"], "%Y-%m-%dT%H:%M:%S%z")
p.reasons.append(f"The sun rises and sets at UTC {iss_position_sunrise.strftime('%H:%M')} and {iss_position_sunset.strftime('%H:%M')}, respectively, {at_iss_location}.")
utcnow = datetime.now(timezone.utc)
p.reasons.append(f"It is currently {utcnow.strftime('%H:%M')} UTC")
iss_night = utcnow < iss_position_sunrise or iss_position_sunset < utcnow
iss_time_description = "nighttime" if iss_night else "daytime"
p.reasons.append(f"Therefore, it is {iss_time_description} {iss_position_description}.")
# Calculate ratio: a number between 0 and 1 saying how close we are to the ISS # Calculate ratio: a number between 0 and 1 saying how close we are to the ISS
phone_position = context.position
distance = haversine(iss_position, phone_position) distance = haversine(iss_position, phone_position)
max_distance = 40075 / 2 # the furthest you can be from any position is half of the earth's circumference max_distance = 40075 / 2 # the furthest you can be from any position is half of the earth's circumference
ratio = distance / max_distance ratio = distance / max_distance
# We're in the same "timezone" as the ISS if we're on the same half of the earth # We're in the same "timezone" as the ISS if we're on the same half of the earth
on_iss_time = ratio < 0.5 on_iss_time = ratio < 0.5
side = "same" if on_iss_time else "other" side = "same" if on_iss_time else "other"
p.reasons.append(f"{the_iss} is {int(distance)} km away, so we are on the {side} side of the earth.") p.reasons.append(f"{the_iss} is {int(distance)} km away, so we are on the {side} side of the earth.")
for i in itertools.count(1):
iss_tz = tf.closest_timezone_at(lng=float(iss_position["longitude"]), lat=float(iss_position["latitude"]),
delta_degree=i)
if iss_tz is not None:
break
iss_time = datetime.now(pytz.timezone(iss_tz))
iss_night = iss_time.hour < 6 or iss_time.hour >= 22
# iss_night on_iss_time night # iss_night on_iss_time night
# 0 0 1 # 0 0 1
@ -56,11 +61,10 @@ def night_on_iss(context: Context) -> Prediction:
# 1 1 1 # 1 1 1
night = iss_night == on_iss_time night = iss_night == on_iss_time
iss_time_description = "nighttime" if iss_night else "daytime"
time_description = "nighttime" if night else "daytime" time_description = "nighttime" if night else "daytime"
p.probability = float(night) op = operator.add if night else operator.sub
p.reasons.append(f"It is {iss_time_description} {iss_position_description}.") p.probability = op(0.5, ratio * 0.5)
p.reasons.append(f"Therefore, it must be {time_description} where we are.") p.reasons.append(f"So it must be {time_description} where we are.")
return p return p

View File

@ -34,7 +34,7 @@ def is_restaurant_open(name, open, close) -> Prediction:
else: else:
p.reasons.append(f"Our favorite pizza place, {name}, is closed.") p.reasons.append(f"Our favorite pizza place, {name}, is closed.")
p.reasons.append(f"We can conclude from this, that there is {1 - (1/11)}% chance of it currently being night outside!") p.reasons.append(f"We can conclude from this, that there is {1 - (1/11)}% chance of it currently being night outside!")
p.probability = 1 - (1 / 11) p.probability = round(1 - (1 / 11), 2)
return p return p

View File

@ -35,6 +35,14 @@ def australiaStrat(context : Context) -> Prediction:
hour = t.hour hour = t.hour
p = Prediction() p = Prediction()
if context.in_australia:
if hour > 22 or hour < 6:
p.probability = 1.0
p.reasons.append('It\'s night-time in Australia, and that\'s where we\'re at.')
else:
p.probability = 0.0
p.reasons.append('It\'s day-time in Australia, and that\'s where we\'re at.')
else:
if hour > 22 or hour < 6: if hour > 22 or hour < 6:
p.probability = 0.0 p.probability = 0.0
p.reasons.append('It\'s night-time in Australia, so it must be day-time here.') p.reasons.append('It\'s night-time in Australia, so it must be day-time here.')

View File

@ -55,7 +55,7 @@ def perform_svm_pred(context: Context) -> Prediction:
X = [min(x, 1) for x in X] X = [min(x, 1) for x in X]
p.reasons.append("We only have two data points") p.reasons.append("We only have two data points")
p.reasons.append("Our only two data points have 11 dimensions") p.reasons.append("Our only two data points have 11 dimensions")
p.reasons.append("We are using a SVM") p.reasons.append("We are using a SVM. Apparently that's a poor idea.")
p.probability = float(predict(X)) p.probability = float(predict(X))
return p return p

View File

@ -11,7 +11,7 @@ from ..util import Context, Prediction
def is_tide(context: Context) -> Prediction: def is_tide(context: Context) -> Prediction:
""" """
Determine whether or not it is night in Aarhus based no the current water level and which month we are in, based Determine whether or not it is night in Aarhus based on the current water level and which month we are in, based
on number of cars driving across The Storbæltsbro. on number of cars driving across The Storbæltsbro.
""" """

View File

@ -5,7 +5,6 @@ pytz
beautifulsoup4 beautifulsoup4
pandas pandas
opencv-python opencv-python
timezonefinder
scikit-learn scikit-learn
html5lib html5lib
xlrd xlrd