Exemplo n.º 1
0
def run(
        protocol_file: str, dtd_file: str, output_file: str,
        parse_only: bool
) -> None:
    """Parse and process speeches of test protocol.

    Tries to parse and process the given protocol. The result is written to
    the given output file (JSON format).

    Args:
        protocol_file (str): protocol to parse
        dtd_file (str): document type definition
        output_file (str): output file
        parse_only (bool): skip processing

    """
    ts_start = time.time()
    speeches = parsing.get_speeches(protocol_file, dtd_file)
    print("Parsing speeches took {:3.2f} seconds".format(time.time()-ts_start))
    if parse_only:
        return
    ts_start = time.time()
    processing.analyze_speeches(speeches)
    print("Processing speeches took {:3.2f} seconds".format(
        time.time()-ts_start
    ))
    speeches_json = [speech.to_json() for speech in speeches]
    with open(output_file, "a") as out:
        json.dump(speeches_json, out, indent=4)
    print("Check result in {}".format(output_file))
Exemplo n.º 2
0
 def run(self):
     """Infinetly process protocols which have not been processed yet."""
     while True:
         logging.info("Updater requests semaphore.")
         self.sem.acquire()
         logging.info("Updater obtained semaphore.")
         protocol = self.db_client.protocol_get_next()
         if protocol is None:
             continue
         try:
             fpath = os.path.join(self.protocols_directory, protocol.fname)
             wget.download(protocol.url, fpath)
             speeches = parsing.get_speeches(fpath, self.dtd_file)
             processing.analyze_speeches(speeches)
             self.db_client.speech_insert_collection(speeches)
         except myexceptions.SpeechParsingException as parse_exception:
             logging.error("Failed parsing speeches in protocol %s",
                           protocol.url)
             print(str(parse_exception))
         except myexceptions.SpeechAnalysisException:
             logging.error("Failed analyzing speech in protocol %s",
                           protocol.url)
         finally:
             # Execute this even if a failure occured to pretend that the
             # broken protocol is updated multiple times
             self.db_client.protocol_is_done(protocol)
Exemplo n.º 3
0
    def test_analyze_sentiment_positive(self):
        """Test function with a positive speech.

        Sentiment analysis should return a positive value for polarity.
        """
        processing.analyze_speeches([self.positive_speech])
        self.assertGreater(self.positive_speech.analysis.polarity, 0.0)
Exemplo n.º 4
0
    def test_analyze_sentiment_negative(self):
        """Test function with a negative speech.

        Sentiment analysis should return a negative value for polarity.
        """
        processing.analyze_speeches([self.negative_speech])
        self.assertLess(self.negative_speech.analysis.polarity, 0.0)
Exemplo n.º 5
0
    def test_analyze_speeches_is_none(self):
        """Test function with None as argument.

        Function should raise an SpeechAnalysis Excpetion.
        """
        with self.assertRaises(myexceptions.SpeechAnalysisException):
            processing.analyze_speeches(None)
Exemplo n.º 6
0
    def test_analyze_empty_speech(self):
        """Test function with an completely empty speech.

        Sentiment analysis should be equal to the default values.
        """
        processing.analyze_speeches([self.empty_speech])
        self.assertIsNotNone(self.empty_speech.analysis)
        self.assertEqual(self.empty_speech.analysis.number_of_comments, 0)
        self.assertEqual(self.empty_speech.analysis.polarity, 0.0)
        self.assertEqual(self.empty_speech.analysis.subjectivity, 0.5)
Exemplo n.º 7
0
    def test_analyze_speeches_is_empty_list():
        """Test function with empty list as argument.

        Function should do nothing, especially do not raise an exception.
        """
        processing.analyze_speeches([])