Exemplo n.º 1
0
 def __init__(self):
     self.client = Client()
Exemplo n.º 2
0
class VUNLPParser(AnalysisScript):
    """Analysisscript subclass for parsers bound to a specific ('home') folder"""

    def __init__(self):
        self.client = Client()
    
    def submit_article(self, article):
        plugin = self.get_plugin()
        handle = self.client.upload(self.parse_command, self._get_text_to_submit(article))
        log.info("Submitted article {article.id}, handle {handle}".format(**locals()))
        if isinstance(article, AnalysedArticle):
            # (re)set done, error, info
            article.done = False
            article.error = False
            article.info = handle
            article.save()
        else:
            # Create AnalysedArticle object
            article = AnalysedArticle.objects.create(article=article, plugin=plugin,
                                                     done=False, error=False, info=handle)
        return article


    def _get_sentences(self, article):
        """
        Return the sentences in the article, creating them if needed.
        @param article: an amcat.Article or AnalysedArticle model instance.
        @return: a sequence of Sentence model instances
        """
        if isinstance(article, AnalysedArticle):
            article = article.article
        return sbd.get_or_create_sentences(article)
    
    def _get_text_to_submit(self, article):
        """
        Return the text to be submitted to the VUNLP web service
        @param article: an amcat.Article or AnalysedArticle model instance. 
        """
        return "\n".join(s.sentence for s in self._get_sentences(article))
        

    def _do_retrieve_article(self, analysed_article):
        if self.check_article(analysed_article):
            parse = Client().download(analysed_article.info)
            open("/tmp/aa_%i.xml" % analysed_article.id, "w").write(parse)
            if len(parse) == 0:
                raise Exception("Empty article: downloading %i / %s yielded %r"
                                % (analysed_article.id, analysed_article.info, parse))
            
            self.store_parse(analysed_article, parse)
            log.info("Stored article  {analysed_article.id}".format(**locals()))
            return True
            
    def _do_check_article(self, analysed_article):
        status = Client().check(analysed_article.info)
        log.info("Article  {analysed_article.id} has parse status {status}".format(**locals()))

        if status == "unknown":
            raise Exception("Article {analysed_article.id} has status 'unknown'"
                            .format(**locals()))
        
        return (status == "ready")