#!/usr/bin/python

##########################################################################
#          (C) Vrije Universiteit, Amsterdam (the Netherlands)            #
#                                                                         #
# This file is part of AmCAT - The Amsterdam Content Analysis Toolkit     #
#                                                                         #
# AmCAT is free software: you can redistribute it and/or modify it under  #
# the terms of the GNU Affero General Public License as published by the  #
# Free Software Foundation, either version 3 of the License, or (at your  #
# option) any later version.                                              #
#                                                                         #
# AmCAT is distributed in the hope that it will be useful, but WITHOUT    #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public     #
# License for more details.                                               #
#                                                                         #
# You should have received a copy of the GNU Affero General Public        #
# License along with AmCAT.  If not, see <http://www.gnu.org/licenses/>.  #
###########################################################################


import logging; log = logging.getLogger(__name__)

from django import forms
from django.db import transaction

from amcat.scripts.script import Script
from amcat.models import ArticleSet, Article

from amcat.nlp import sbd

class SplitArticles(Script):
    """
    Perform a keyword query on an articleset.
    """
    class options_form(forms.Form):
        articlesets = forms.ModelMultipleChoiceField(queryset=ArticleSet.objects.all())

    @transaction.commit_on_success    
    def run(self, _input=None):
        sets = self.options['articlesets']
        to_split = list(Article.objects.filter(articlesets__in=sets, sentences=None))
        n = len(to_split)

        log.info("Will split {n} articles".format(**locals()))
        for i, article in enumerate(to_split):
            if not i % 100:
                log.info("Splitting article {i}/{n}".format(**locals()))

            sbd.get_or_create_sentences(article)

        log.info("Splitted {n} articles!".format(**locals()))
        
    
if __name__ == '__main__':
    from amcat.scripts.tools import cli
    cli.run_cli()
        

@@ -1576,16 +1576,26 @@
  Article
+, Sentence
 %0A%0Afrom a
@@ -1945,16 +1945,89 @@
 esets'%5D%0A
+        log.info(%22Listing articles from sets %7Bsets%7D%22.format(**locals()))%0A
         
@@ -2089,24 +2089,19 @@
 sets
-, sentences=None
+).only(%22id%22
 ))%0A 
@@ -2149,16 +2149,26 @@
 o(%22Will 
+check and 
 split %7Bn
@@ -2300,22 +2300,23 @@
 g.info(%22
-Splitt
+Process
 ing arti
@@ -2348,17 +2348,90 @@
 als()))%0A
-%0A
+            if not Sentence.objects.filter(article=article).exists():%0A    
         

make splitting filter on sentences later to prevent slow join