Пример #1
0
    def test_gets_correct_keywords_from_comparative_phrase(self):
        """
            Check if it works with 2 and more companies, try it with
            different functions (compare spot price, % change, news)
        """
        q1 = "get me the highest stock price out of amazon, google and hanson"
        q2 = "does mining have a worse % change than Technology"

        r1 = nl.getRequests(q1)
        r2 = nl.getRequests(q2)
        self.assertSetEqual(set(["GOOGL", "AMZ", "HNS"]),
                            set(r1[0]["companies"]))
        self.assertIn("price", r1[0]["quality"])
        self.assertSetEqual(set(["Mining", "Technology"]), set(r2[0]["areas"]))
        self.assertIn("percentDiff", r2[0]["quality"])
Пример #2
0
def ask_chatbot(request):
    """
        Given a query in the POST data, it will
        parse the message then return a valid response.
    """
    query = request.POST.get("query")
    trader = request.user.traderprofile
    data = {"name": "FLORIN", "messages": []}
    requests = nl.getRequests(query)
    if requests == [] or requests == None:
        data["messages"].append(nl.genericUnknownResponse())
    else:
        for request in requests:
            # for each company that was requested, incremenet the hit count
            for ticker in request["companies"]:
                try:
                    hc = CompanyHitCount.objects.get(company__ticker=ticker,
                                                     trader=trader)
                    hc.hit_count += 1
                    hc.save()
                except ObjectDoesNotExist:
                    CompanyHitCount.objects.create(
                        company=Company.objects.get(ticker=ticker),
                        trader=trader,
                        hit_count=1)
            # for each industry that was requested, incremenet the hit count
            for name in request["areas"]:
                try:
                    hc = IndustryHitCount.objects.get(industry__name=name,
                                                      trader=trader)
                    hc.hit_count += 1
                    hc.save()
                except ObjectDoesNotExist:
                    IndustryHitCount.objects.create(
                        industry=Industry.objects.get(name=name),
                        trader=trader,
                        hit_count=1)
            if request["quality"] == "joke":
                data["messages"].append(
                    nl.turnIntoResponse("Why did the chicken cross the road?"))
            try:
                data["messages"].append(respond_to_request(request))
            except Exception as e:
                data["messages"].append({
                    "name":
                    "FLORIN",
                    "type":
                    "text",
                    "body":
                    "Sorry, something went wrong with your " + " query.",
                    "caption":
                    str(e) if len(str(e)) < 30 else ''
                })
                print("--------------------- ERROR ---------------------")
                logging.exception("Error while parsing %s query" %
                                  request["quality"])
                print("------------------ REQUEST OBJ ------------------")
                print(request)
                print("-------------------------------------------------")
    return JsonResponse(data)
Пример #3
0
 def test_can_identify_news_for_multiple_companies(self):
     """
         Same as above but make sure it can do it for more than
         one company. (Maybe doesn't have to be as rigourus as above
         in terms of the number of different phrases)
     """
     query = "give me the news for google, amazon and mining"
     r = nl.getRequests(query)[0]
     self.assertIsNot(r, None)
     self.assertIn("news", r["quality"])
     self.assertSetEqual(set(["GOOGL", "AMZ", "Mining"]),
                         set().union(r["companies"], r["areas"]))
Пример #4
0
 def test_can_identify_company_alias(self):
     """
         Tests to see if a company can be obtained with an alias
     """
     ticker = "GOOGL"
     queries = [
         "what is the price of alphabet",
     ]
     responses = [nl.getRequests(x) for x in queries]
     self.assertIsNot(responses, None)
     for rq in responses:
         for r in rq:
             self.assertTrue(ticker in r["companies"])
Пример #5
0
 def test_can_identify_industry_in_query(self):
     """
         Just looking for a single industry here.
     """
     queries = [
         "blah blah blah mining blAH PRICE",
         "what is the stock history of technology like",
         "what is the percentage change for mining?"
     ]
     requests = list(map(lambda x: nl.getRequests(x), queries))
     self.assertIsNot(requests, None)
     for r in requests:
         self.assertIsNotNone(r[0]["areas"])
         self.assertEqual(len(r[0]["areas"]), 1)
Пример #6
0
 def test_can_identify_multiple_industries_in_query(self):
     """
         Have multiple industries in the query and make sure it
         gets them all.
     """
     queries = [
         "blah blah technology blah mining blAH PRICE",
         "what is the stock history of technology and mining like",
         "what is the percentage change for mining, technology and blah?"
     ]
     requests = list(map(lambda x: nl.getRequests(x), queries))
     for r in requests:
         self.assertIsNotNone(r[0]["areas"])
         self.assertEqual(len(r[0]["areas"]), 2)
Пример #7
0
 def test_can_identify_ticker(self):
     """
         Can it get a ticker from a sentence.
     """
     ticker = "AMZ"
     queries = [
         "what is the price of $", "get me % change of $ blah",
         "$ blah sjh dsjjjh news change"
     ]
     responses = [nl.getRequests(x.replace("$", ticker)) for x in queries]
     self.assertIsNot(responses, None)
     for rq in responses:
         for r in rq:
             self.assertTrue(ticker in r["companies"])
Пример #8
0
 def test_can_identify_multiple_tickers(self):
     """
         Can it get a tickers from a sentence containing many
         of them.
     """
     queries = [
         "what is the price of AMZ and GOOGL",
         "get me % change of HNS & AMZ blah",
         "Is GOOGL doing as well as AMZ and HNS on stock price this week?"
     ]
     responses = [nl.getRequests(x) for x in queries]
     self.assertIsNot(responses, None)
     for rq in responses:
         for r in rq:
             self.assertTrue(len(r["companies"]) > 1)
Пример #9
0
 def test_can_identify_company_name(self):
     """
         Input a sentence containing a company name, check if
         it can identify it.
     """
     ticker = "GOOGL"
     queries = [
         "what is the price of google", "get me % change of Google blah",
         "Google blah sjh dsjjjh news change"
     ]
     responses = [nl.getRequests(x) for x in queries]
     self.assertIsNot(responses, None)
     for rq in responses:
         for r in rq:
             self.assertTrue(ticker in r["companies"])
Пример #10
0
 def test_can_identify_multiple_company_aliases(self):
     """
         Tests to see if multiple companies can be obtained with their aliases
     """
     #TODO find out why second query fails
     queries = [
         "what is the price of amz and alphabet",
         "What is the price of PLC, google and amz",
         "Is google doing as well as amazon and hanson plc on stock price this week?"
     ]
     responses = [nl.getRequests(x) for x in queries]
     self.assertIsNot(responses, None)
     for rq in responses:
         for r in rq:
             self.assertTrue(len(r["companies"]) > 1)
Пример #11
0
 def test_can_identify_multiple_company_names(self):
     """
         Input a sentence containing multiple company name, check if
         it can identify it.
     """
     queries = [
         "what is the price of amazon and google",
         "get me % change of Hanson & Amazon blah",
         "Is Google doing as well as Amazon and Hanson on stock price this week?"
     ]
     responses = [nl.getRequests(x) for x in queries]
     self.assertIsNot(responses, None)
     for rq in responses:
         for r in rq:
             self.assertTrue(len(r["companies"]) > 1)
Пример #12
0
 def test_can_identify_news_query(self):
     """
         Can it identify the user asking for the news of a
         specific company. Try and test different things such as
         'recent news', 'news this week', 'news last month'
     """
     entity = ["GOOGL", "AMZ", "Technology"]
     queries = [
         "give me the recent news of google",
         "news for AMZ since last wednesday",
         "is there any news about technology this week"
     ]
     requests = list(map(lambda x: nl.getRequests(x), queries))
     for i in range(len(requests)):
         r = requests[i][0]
         e = entity[i]
         self.assertIn("news", r["quality"])
         self.assertIn(e, set().union(r["companies"], r["areas"]))
Пример #13
0
 def test_can_identify_stock_price_query(self):
     """
         Does it give the correct response for a properly
         formatted stock price query?
     """
     ticker = "HNS"
     industry = "Mining"
     queries = [
         "What is the spot price for hanson?",
         "Give me the current price for HNS",
         "mining price",
     ]
     requests = list(map(lambda x: nl.getRequests(x), queries))
     self.assertIsNot(requests, None)
     for r in requests:
         self.assertIsNot(r, None)
         self.assertTrue((ticker in r[0]["companies"])
                         or (industry in r[0]["areas"]))
         self.assertIn("price", r[0]["quality"])
Пример #14
0
 def test_can_identify_comparative_in_query(self):
     """
         Make sure to check for all the common compararives
         here such as 'more', 'greater', 'less than', 'better',
         'worse' etc.
     """
     comparatives = ["lower", "highest", "lowest"]
     queries = [
         "Is AMZ tock price lower than GOOGL",
         "Who has the best percentage change out of HMS and amazon",
         "Who is doing the worst out of google and amazon?"
     ]
     responses = [nl.getRequests(x) for x in queries]
     self.assertIsNot(responses, None)
     i = 0
     for rq in responses:
         self.assertIsNot(rq[0]["comparative"], None)
         self.assertEqual(comparatives[i], rq[0]["comparative"])
         i += 1
Пример #15
0
 def test_can_identify_time_phrase(self):
     """
         Try different formats such as
             - '26th January 2017'
             - '2018'
         etc
     """
     start_times = [
         datetime.datetime.strptime('26/01/2018', '%d/%m/%Y'),
         datetime.datetime.strptime('01/02/2018', '%d/%m/%Y')
     ]
     queries = [
         "what has googles stock price been like from the 26th January 2018?",
         "Show me hanson's stock price since feb 2018"
     ]
     requests = list(map(lambda x: nl.getRequests(x), queries))
     for i in range(len(requests)):
         time = start_times[i]
         r = requests[i][0]
         self.assertEqual(time.year, r["time"]["start"].year)
         self.assertEqual(time.month, r["time"]["start"].month)
         self.assertEqual(time.day, r["time"]["start"].day)
Пример #16
0
    def test_can_identify_relative_time_phrase(self):
        """
            Here test for things such as 'last week', 'today',
            'this month' etc.

        """
        start_times = [
            datetime.datetime.now() - datetime.timedelta(days=7),
            datetime.datetime.now() -
            datetime.timedelta(days=datetime.datetime.now().weekday()) +
            datetime.timedelta(days=4, weeks=-1)
        ]
        queries = [
            "what has googles stock price been like from last week?",
            "Show me hanson's stock price from last friday"
        ]
        requests = list(map(lambda x: nl.getRequests(x), queries))
        for i in range(len(requests)):
            time = start_times[i]
            r = requests[i][0]["time"]["start"]
            self.assertEqual(time.year, r.year)
            self.assertEqual(time.month, r.month)
            self.assertEqual(time.day, r.day)
Пример #17
0
 def test_correct_response_from_nonesense_phrase(self):
     """
         Does it speak rubbish? Try a few examples.
     """
     self.assertEqual([],
                      nl.getRequests("sjkhd kahjd kjsahd kajshd jkas dk"))