add_DDG_entry_responses_info.py
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding:utf-8 -*-
import random
import time
from django.core.management.base import BaseCommand
from verification.duckduckgo import DuckDuckGo
from webapp.models import Meaning, Source
SOURCE = Source.objects.get(key='PSC')
class Command(BaseCommand):
help = 'Add DDG entry information to expressions.'
def handle(self, *args, **options):
# check_meanings_with_multiple_sources()
catchword_verification()
add_DDG_entry_responses_info_meanings()
def check_meanings_with_multiple_sources():
for meaning in Meaning.objects.order_by('id'):
sources = []
for expr in meaning.expressions.all():
if expr.link.source.key not in sources:
sources.append(expr.link.source.key)
if len(sources) > 1:
print u'!!!!!!!!!! [%s] !!!!!!!!!!' % ', '.join(sources)
print meaning
def catchword_verification():
print (SOURCE)
for meaning in Meaning.objects.filter(expressions__link__source=SOURCE).distinct():
if meaning.expressions.filter(is_catchword=True).count() > 1:
print meaning
def add_DDG_entry_responses_info_meanings():
print (SOURCE)
meanings = Meaning.objects.filter(expressions__link__source=SOURCE).filter(expressions__DDG_entry_responses=1000).distinct()
check_again_meanings = []
duckduckgo = DuckDuckGo(25)
for meaning in meanings.order_by('id'):
print (meaning.id)
for catchword in meaning.expressions.filter(is_catchword=True):
for definition in meaning.expressions.exclude(pk=catchword.pk):
time.sleep(random.uniform(2.0, 4.0))
try:
responses = duckduckgo.entry_responses_count(catchword, definition)
definition.DDG_entry_responses = responses
definition.save()
catchword.DDG_entry_responses = responses
catchword.save()
print (definition.DDG_entry_responses, catchword.orth_text, definition.orth_text)
except RuntimeError:
check_again_meanings.append(meaning)
if len(check_again_meanings) > 0:
print ('Check again meanigs %d: run script again!' % len(check_again_meanings))