Example #1
0
def simulate_main(triple):
    fast_search = 'true'
    obj_a = Argument(triple[1].lower().strip())
    obj_b = Argument(triple[2].lower().strip())
    aspects = [Aspect(triple[3].lower(), 5)]
    model = 'bow'
    # model = 'infersent'
    load_config()
    if aspects:
        json_compl_triples = request_es_triple(obj_a, obj_b, aspects)
    json_compl = request_es_ML(fast_search, obj_a, obj_b)

    if aspects:
        all_sentences = extract_sentences(json_compl_triples)
        all_sentences.extend(extract_sentences(json_compl))
    else:
        all_sentences = extract_sentences(json_compl)

    remove_questions(all_sentences)

    prepared_sentences = prepare_sentence_DF(all_sentences, obj_a, obj_b)

    classification_results = classify_sentences(prepared_sentences, model)

    final_dict = evaluate(all_sentences, prepared_sentences,
                          classification_results, obj_a, obj_b, aspects)

    a_aspect_score = 0
    if triple[3] in final_dict['object1']['points']:
        a_aspect_score = final_dict['object1']['points'][triple[3]]
    b_aspect_score = 0
    if triple[3] in final_dict['object2']['points']:
        b_aspect_score = final_dict['object2']['points'][triple[3]]

    return [a_aspect_score, b_aspect_score]
Example #2
0
    def test_build_object_urlpart2(self):
        obj_a = Argument('')
        obj_b = Argument('gorilla')
        with self.assertRaises(ValueError) as context:
            build_object_urlpart(obj_a, obj_b)

        self.assertTrue('Please enter both objects!' in str(context.exception))
Example #3
0
 def test_what_is_better11(self):
     obj_a = Argument('coca1-cola light')
     obj_b = Argument('pepsi light')
     self.assertEqual(
         what_is_better('Coca1-Cola™ light tastes better than Pepsi™ light',
                        obj_a, obj_b), {
                            'marker_cnt': 1,
                            'winner': obj_a
                        })
Example #4
0
 def test_clear_sentences9(self):
     obj_a = Argument('coca-cola light')
     obj_b = Argument('pepsi light')
     s = [
         Sentence('Coca Cola™ light tastes better than Pepsi™ light', 20,
                  '', '')
     ]
     s = clear_sentences(s, obj_a, obj_b)
     self.assertTrue(s)
Example #5
0
 def test_build_object_urlpart1(self):
     obj_a = Argument('ape')
     obj_b = Argument('gorilla')
     url = build_object_urlpart(obj_a, obj_b)
     self.assertTrue(
         url ==
         'http://ltdemos.informatik.uni-hamburg.de/depcc-index/depcc/_search?q=text:"ape"%20AND%20"gorilla"'
         or url ==
         'http://ltdemos.informatik.uni-hamburg.de/depcc-index/commoncrawl2/_search?q=text:"ape"%20AND%20"gorilla"'
     )
Example #6
0
    def test_sentence_preparation_ML(self):
        sentenceA = [
            Sentence(
                'Coca-Cola tastes better than pepsi, because of its ingredients',
                20, '', ''),
            Sentence(
                'Pepsi is worse than Coca-cola, because of the better sweeteners',
                20, '', '')
        ]

        df = prepare_sentence_DF(sentenceA, Argument('coca-cola'),
                                 Argument('pepsi'))
        self.assertNotEqual(df.loc[0]['object_a'], df.loc[1]['object_a'])
Example #7
0
 def test_extract_main_links(self):
     sentencesA = [
         Sentence('ObjA is better than ObjB because of more time', 10, '',
                  '')
     ]
     sentencesB = [
         Sentence('ObjA is worse than ObjB because of less power', 10, '',
                  '')
     ]
     obj_a = Argument('ObjA')
     obj_b = Argument('ObjB')
     self.assertEqual(
         extract_main_links(sentencesA, sentencesB, obj_a, obj_b), {
             'A': ['time'],
             'B': ['power']
         })
Example #8
0
def buildURLs(pairs):
    '''
    Builds a list of urls using the Backend functions 'build_object_urlpart' and 'add_marker_urlpart'

    @param pairs: all unic pairs

    @return list of len(pairs) urls
    '''
    urls = []
    for pair in pairs:
        objectA = Argument(pair[0])
        objectB = Argument(pair[1])
        url = es_requester.build_object_urlpart(objectA, objectB)
        url = es_requester.add_marker_urlpart(url, 'false')
        urls.append(url)

    return urls
Example #9
0
 def test_extract_main_links3(self):
     sentencesA = [
         Sentence(
             'Coca-Cola™ tastes better than pepsi, because of its ingredients',
             20, '', '')
     ]
     sentencesB = [
         Sentence(
             'Pepsi is worse than Coca-cola™, because of the better sweeteners',
             20, '', '')
     ]
     obj_a = Argument('coca-cola')
     obj_b = Argument('pepsi light')
     self.assertEqual(
         extract_main_links(sentencesA, sentencesB, obj_a, obj_b), {
             'A': ['ingredients'],
             'B': ['sweeteners']
         })
Example #10
0
    def calculateWinner(self, preprocessed, pairsSentences):
        '''
        Goes through the assigned preprocessed comparisons, reads the aspects 
        and calls a Backend function (find_winner) to compute the scores of the given pair 
        with respect to the aspects

        @param preprocessed: the preprocessed comparisons

        @param pairsSentences: a dictionary where the sentences of a pair can be
        accessed by the pair as key

        @returns a list containing the computed scores  ['id', 'object_a', 'object_b',
                  'gold_label', 'score_a', 'score_b']
        '''
        resultList = []
        for comparison in preprocessed:
            result = comparison[:4]
            objectA = comparison[1]
            objectB = comparison[2]
            pair = objectA + "," + objectB
            reversedPair = objectB + "," + objectA

            tempAspects = [x for x in comparison[5].split(', ')]
            aspects = [Aspect(aspect, 5) for aspect in tempAspects]

            sentences = pairsSentences[
                pair] if pair in pairsSentences else pairsSentences[
                    reversedPair] if reversedPair in pairsSentences else {}
            finalDict = find_winner(sentences, Argument(objectA),
                                    Argument(objectB), aspects)

            scoreA = finalDict['score object 1']
            scoreB = finalDict['score object 2']

            print(result[0])

            result.extend([scoreA, scoreB])
            resultList.append(result)
        return resultList
Example #11
0
    def requestSentences(self, pairs, threadName):
        '''
        Builds the urls and calls the function to request.
        Uses the functions of the Backend to extract and clear the sentences:
        'extract_sentences' and 'clear_sentences'

        @param pairs: all unic pairs extracted from file

        @param threadName: name of the thread

        @returns list of results, an entry per pair which consists of the pair itself,
        the requested sentences and the corresponding scores of the sentences
        '''
        resultList = []
        urls = buildURLs(pairs)

        for i in range(0, len(urls), 1):
            objectA = pairs[i][0]
            objectB = pairs[i][1]
            jsonResult = self.sendRequest(urls[i])
            allSentences = es_requester.extract_sentences(jsonResult)
            allSentences = clear_sentences(
                allSentences, Argument(objectA), Argument(objectB))

            sentences = []
            scores = []

            for key in allSentences:
                sentences.append(key)
                scores.append(allSentences[key])

            resultList.append([objectA, objectB])
            resultList[len(resultList)-1].extend(sentences)
            resultList[len(resultList)-1].extend(scores)

        return resultList
Example #12
0
class Test(unittest.TestCase):
    '''
    Testing different cases of possible object positioning as well as the presense 
    of the better/worse markers and negation (not excluded case)of the what_is_better
    method
    '''

    objA = Argument('dog')
    objB = Argument('cat')

    def test_what_is_better1(self):
        self.assertEqual(
            what_is_better('Dog is better than cat', self.objA, self.objB), {
                'winner': self.objA,
                'marker_cnt': 1
            })

    def test_what_is_better2(self):
        self.assertEqual(
            what_is_better('not Dog is better than cat ', self.objA,
                           self.objB), {
                               'winner': self.objA,
                               'marker_cnt': 1
                           })

    def test_what_is_better3(self):
        self.assertEqual(
            what_is_better('Dog is not better than cat ', self.objA,
                           self.objB), {
                               'winner': self.objB,
                               'marker_cnt': 1
                           })

    def test_what_is_better5(self):
        self.assertEqual(what_is_better('Dog is a cat', self.objA, self.objB),
                         {
                             'winner': self.objB,
                             'marker_cnt': 0
                         })

    def test_what_is_better6(self):
        self.assertEqual(
            what_is_better('Dog is worse than cat', self.objA, self.objB), {
                'winner': self.objB,
                'marker_cnt': 1
            })

    def test_what_is_better7(self):
        self.assertTrue(
            what_is_better('Dog is worse than cat', self.objB, self.objA))

    def test_what_is_better8(self):
        self.assertEqual(
            what_is_better('Dog is a cat solid', self.objB, self.objA), {
                'marker_cnt': 0,
                'winner': self.objB
            })

    def test_what_is_better9(self):
        self.assertEqual(
            what_is_better(
                'This tortoise sculpture is made of hammer formed copper sheet metal and filled solid with concrete.',
                self.objB, self.objA), {
                    'marker_cnt': 0,
                    'winner': self.objA
                })

    def test_what_is_better10(self):
        obj_a = Argument('coca-cola light')
        obj_b = Argument('pepsi light')
        self.assertEqual(
            what_is_better('Coca Cola light tastes better than pepsi light',
                           obj_a, obj_b), {
                               'marker_cnt': 1,
                               'winner': obj_a
                           })

    def test_what_is_better11(self):
        obj_a = Argument('coca1-cola light')
        obj_b = Argument('pepsi light')
        self.assertEqual(
            what_is_better('Coca1-Cola™ light tastes better than Pepsi™ light',
                           obj_a, obj_b), {
                               'marker_cnt': 1,
                               'winner': obj_a
                           })

    '''
    Testing if the removal of the following sentences work:
    1.containing '?'
    2.containing two markers of different types between the objects
    3.containing no markers between the object
    '''

    def test_clear_sentences1(self):
        s = [Sentence('Dog is worse than cat?', 20, '', '')]
        s = clear_sentences(s, self.objA, self.objB)
        self.assertFalse(s)

    def test_clear_sentences2(self):
        s = [
            Sentence('Dog is worse than cat', 20, '', ''),
            Sentence('Dog didn\'t look better than cat', 20, '', '')
        ]
        s = clear_sentences(s, self.objA, self.objB)
        self.assertTrue('Dog didn\'t look better than cat' in
                        [sentence.text for sentence in s])

    def test_clear_sentences3(self):
        s = [
            Sentence(
                'snowboarding is harder to learn but easier to master than skiing',
                20, '', '')
        ]
        s = clear_sentences(s, self.objA, self.objB)
        self.assertFalse(s)

    def test_clear_sentences4(self):
        s = [
            Sentence('A better cat is still no dog', 20, '', ''),
            Sentence('Cat are worse than dog', 20, '', '')
        ]
        s = clear_sentences(s, self.objA, self.objB)
        self.assertFalse('A better cat is still no dog' in
                         [sentence.text for sentence in s])
        self.assertTrue(s)

    def test_clear_sentences5(self):
        s = [
            Sentence('Cat and dog work well together', 10, '', ''),
            Sentence('Cat are worse than dog', 20, '', '')
        ]
        s = clear_sentences(s, self.objA, self.objB)
        self.assertFalse('Cats and dogs work well together' in
                         [sentence.text for sentence in s])
        self.assertTrue(s)

    def test_clear_sentences6(self):
        s = [
            Sentence('Cat is worse than dog', 10, '', ''),
            Sentence(
                'Cat are more beautiful, but are harder to raise than dog', 20,
                '', '')
        ]
        s = clear_sentences(s, self.objA, self.objB)
        self.assertFalse('Cat are wiser, but are harder to raise than dog' in
                         [sentence.text for sentence in s])
        self.assertTrue(s)

    def test_clear_sentences7(self):
        obj_a = Argument('cola light')
        obj_b = Argument('pepsi light')
        s = [Sentence('Cola light tastes better than pepsi light', 20, '', '')]
        s = clear_sentences(s, obj_a, obj_b)
        self.assertTrue(s)

    def test_clear_sentences8(self):
        obj_a = Argument('coca-cola light')
        obj_b = Argument('pepsi light')
        s = [
            Sentence('Coca Cola light tastes better than pepsi light', 20, '',
                     '')
        ]
        s = clear_sentences(s, obj_a, obj_b)
        self.assertTrue(s)

    def test_clear_sentences9(self):
        obj_a = Argument('coca-cola light')
        obj_b = Argument('pepsi light')
        s = [
            Sentence('Coca Cola™ light tastes better than Pepsi™ light', 20,
                     '', '')
        ]
        s = clear_sentences(s, obj_a, obj_b)
        self.assertTrue(s)

    '''
    Testing if all parts of the find_winner method work properly
    '''

    def test_find_winner1(self):
        s = [
            Sentence('Dog is better than cat', 10, 'http://test.com', 5),
            Sentence('Cat is beautiful better than dog', 10,
                     'http://test2.com', 7),
            Sentence('Dogs are way better than cat', 10, 'http://test3.com', 6)
        ]
        result = find_winner(s, self.objA, self.objB, [])
        obj_1 = result['object1']
        obj_2 = result['object2']

        self.assertEqual(obj_1['name'], self.objA.name)
        self.assertEqual(obj_2['name'], self.objB.name)
        self.assertEqual(obj_1['totalPoints'], 2.0)
        self.assertEqual(obj_2['totalPoints'], 1.0)
        self.assertEqual(obj_1['points'], {'none': 2.0})
        self.assertEqual(obj_2['points'], {'none': 1.0})
        sentences1 = [sentence['text'] for sentence in obj_1['sentences']]
        sentences2 = [sentence['text'] for sentence in obj_2['sentences']]
        self.assertEqual(
            sorted(sentences1),
            sorted(['Dog is better than cat', 'Dogs are way better than cat']))
        self.assertEqual(sorted(sentences2),
                         sorted(['Cat is beautiful better than dog']))
        self.assertEqual(result['winner'], self.objA.name)
        self.assertEqual(result['sentenceCount'], 3.0)

    def test_build_object_urlpart1(self):
        obj_a = Argument('ape')
        obj_b = Argument('gorilla')
        url = build_object_urlpart(obj_a, obj_b)
        self.assertTrue(
            url ==
            'http://ltdemos.informatik.uni-hamburg.de/depcc-index/depcc/_search?q=text:"ape"%20AND%20"gorilla"'
            or url ==
            'http://ltdemos.informatik.uni-hamburg.de/depcc-index/commoncrawl2/_search?q=text:"ape"%20AND%20"gorilla"'
        )

    def test_build_object_urlpart2(self):
        obj_a = Argument('')
        obj_b = Argument('gorilla')
        with self.assertRaises(ValueError) as context:
            build_object_urlpart(obj_a, obj_b)

        self.assertTrue('Please enter both objects!' in str(context.exception))

    def test_extract_main_links(self):
        sentencesA = [
            Sentence('ObjA is better than ObjB because of more time', 10, '',
                     '')
        ]
        sentencesB = [
            Sentence('ObjA is worse than ObjB because of less power', 10, '',
                     '')
        ]
        obj_a = Argument('ObjA')
        obj_b = Argument('ObjB')
        self.assertEqual(
            extract_main_links(sentencesA, sentencesB, obj_a, obj_b), {
                'A': ['time'],
                'B': ['power']
            })

    def test_extract_main_links2(self):
        sentencesA = [
            Sentence(
                'Coca Cola tastes better than pepsi, because of its ingredients',
                20, '', '')
        ]
        sentencesB = [
            Sentence(
                'Pepsi is worse than Coca-cola, because of the better sweeteners',
                20, '', '')
        ]
        obj_a = Argument('coca-cola')
        obj_b = Argument('pepsi light')
        self.assertEqual(
            extract_main_links(sentencesA, sentencesB, obj_a, obj_b), {
                'A': ['ingredients'],
                'B': ['sweeteners']
            })

    def test_extract_main_links3(self):
        sentencesA = [
            Sentence(
                'Coca-Cola™ tastes better than pepsi, because of its ingredients',
                20, '', '')
        ]
        sentencesB = [
            Sentence(
                'Pepsi is worse than Coca-cola™, because of the better sweeteners',
                20, '', '')
        ]
        obj_a = Argument('coca-cola')
        obj_b = Argument('pepsi light')
        self.assertEqual(
            extract_main_links(sentencesA, sentencesB, obj_a, obj_b), {
                'A': ['ingredients'],
                'B': ['sweeteners']
            })

    '''
    Test the ML parts
    '''

    def test_sentence_preparation_ML(self):
        sentenceA = [
            Sentence(
                'Coca-Cola tastes better than pepsi, because of its ingredients',
                20, '', ''),
            Sentence(
                'Pepsi is worse than Coca-cola, because of the better sweeteners',
                20, '', '')
        ]

        df = prepare_sentence_DF(sentenceA, Argument('coca-cola'),
                                 Argument('pepsi'))
        self.assertNotEqual(df.loc[0]['object_a'], df.loc[1]['object_a'])