Blame view

dictionary/management/commands/get_examples.py 3.65 KB
Bartłomiej Nitoń authored
1
2
3
#-*- coding:utf-8 -*-

import codecs
Bartłomiej Nitoń authored
4
import datetime
Bartłomiej Nitoń authored
5
6
7
8
import os

from django.core.management.base import BaseCommand
Bartłomiej Nitoń authored
9
from dictionary.models import Lemma, get_ready_statuses
Bartłomiej Nitoń authored
10
11
12
13
from settings import PROJECT_PATH

BASE_PATH = os.path.join(PROJECT_PATH, 'data')
Bartłomiej Nitoń authored
14
15
16
17
18
19
20
21
22
LABELS = (u'hasło', 
          u'status hasła',
          u'identyfikator schematu',
          u'schemat', 
          u'opinia o schemacie',
          u'przykład',
          u'opinia o przykładzie',
          u'zródło przykładu',
          u'wybór typów fraz')
Bartłomiej Nitoń authored
23
24
25
26
27

class Command(BaseCommand):
    help = 'Get pinned examples from Slowal.'

    def handle(self, **options):
Bartłomiej Nitoń authored
28
29
30
31
32
33
        get_examples()

def get_examples():
    ready_statuses = get_ready_statuses()
    write_detailed_examples(ready_statuses)
    # write_examples(ready_statuses)
Bartłomiej Nitoń authored
34
Bartłomiej Nitoń authored
35
def write_detailed_examples(statuses):
Bartłomiej Nitoń authored
36
    try:
Bartłomiej Nitoń authored
37
38
39
40
41
42
        lemmas = Lemma.objects.filter(old=False)
        lemmas = lemmas.filter(status__in=statuses)
        now = datetime.datetime.now().strftime('%Y%m%d')
        examples_file = codecs.open(os.path.join(BASE_PATH, 'detailed_examples_%s.csv' % now), 'wt', 'utf-8') 
        examples_file.write(u'%s\n' % u'\t'.join(LABELS)) 
        for lemma in lemmas.order_by('entry_obj__name'):
Bartłomiej Nitoń authored
43
            print lemma
Bartłomiej Nitoń authored
44
            lemma_entry = lemma.entry_obj.name
Bartłomiej Nitoń authored
45
46
            lemma_status = lemma.status.status
            for frame in lemma.frames.order_by('text_rep').all():
Bartłomiej Nitoń authored
47
48
                if not lemma.phraseology_ready() and frame.phraseologic:
                    continue
Bartłomiej Nitoń authored
49
50
                frame_opinion = lemma.frame_opinions.filter(frame=frame).all()[0].value
                for example in lemma.nkjp_examples.filter(frame=frame):
Bartłomiej Nitoń authored
51
                    sentence = example.sentence.replace('\n', ' ').replace('\r', '').replace('\t', ' ') 
Bartłomiej Nitoń authored
52
                    arguments_selection = u'%s' % u' + '.join([u'%s' % selection.__unicode__() for selection in example.arguments.all()])
Bartłomiej Nitoń authored
53
54
55
56
57
58
59
60
61
                    examples_file.write(u'%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n' % (lemma_entry,
                                                                                   lemma_status,
                                                                                   frame.id,
                                                                                   frame.get_position_spaced_text_rep(),
                                                                                   frame_opinion,
                                                                                   sentence,
                                                                                   example.opinion.opinion,
                                                                                   example.source.source,
                                                                                   arguments_selection))
Bartłomiej Nitoń authored
62
63
64
    finally:
        examples_file.close()
Bartłomiej Nitoń authored
65
66
def write_examples(statuses):
    try:       
Bartłomiej Nitoń authored
67
        examples_file = codecs.open(os.path.join(BASE_PATH, 
Bartłomiej Nitoń authored
68
69
70
71
72
73
74
75
76
77
                                                 'examples_gotowe_plus.txt'), 'wt', 'utf-8')  
        for lemma in Lemma.objects.filter(old=False).filter(status__in=statuses).order_by('entry').all():
            print lemma
            examples_file.write(lemma.entry+'\n')
            for frame in lemma.frames.order_by('text_rep').all():
                if lemma.frame_opinions.get(frame=frame).value.value != u'zła':
                    examples_file.write('\t%s\n' % frame.text_rep)
                    for example in lemma.nkjp_examples.filter(frame=frame):
                        examples_file.write('\t\t--> %s\n' % example.sentence)
            examples_file.write('\n\n')
Bartłomiej Nitoń authored
78
    finally:
Bartłomiej Nitoń authored
79
        examples_file.close()