Skip to content

Commit

Permalink
Merge pull request #615 from ejplatform/improve-fake-gen
Browse files Browse the repository at this point in the history
[REVIEW] Melhora gerador de clusteres falsos
  • Loading branch information
fabiommendes authored Nov 6, 2018
2 parents f507828 + 47eb5b4 commit 1d01a74
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
65 changes: 59 additions & 6 deletions src/ej_clusters/factories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from ej_clusters.models import Cluster, Stereotype, StereotypeClusterMap, UserClusterMap
from django.contrib.auth import get_user_model
from django.db.models import Avg
import random

from ej_clusters.models import Cluster, Stereotype, StereotypeVote, UserClusterMap, StereotypeClusterMap
from ej_conversations.models import Choice, Vote

User = get_user_model()


def set_clusters_from_comments(conversation, comment_map, exclusive=True,
Expand All @@ -19,6 +26,7 @@ def set_clusters_from_comments(conversation, comment_map, exclusive=True,
})
"""
author = author or conversation.author
clusterization = conversation.get_clusterization()
created_comments = []
created_stereotypes = []

Expand All @@ -30,18 +38,16 @@ def set_clusters_from_comments(conversation, comment_map, exclusive=True,

# Create cluster and stereotype
cluster = Cluster.objects.create(
conversation=conversation,
clusterization=clusterization,
name=cluster_name,
)
stereotype, _ = Stereotype.objects.get_or_create(
name=cluster_name,
description=description,
)
StereotypeClusterMap.objects.create(
stereotype=stereotype,
cluster=cluster,
owner=author,
conversation=conversation,
)
cluster.stereotypes.add(stereotype)
created_stereotypes.append(stereotype)

# Save comments for stereotype
Expand All @@ -68,6 +74,53 @@ def set_clusters_from_comments(conversation, comment_map, exclusive=True,
return created_comments


def cluster_votes(conversation, users):
clusterization = conversation.get_clusterization()
comments = list(conversation.comments.all())
comments_map = {comment.id: comment for comment in comments}
clusters = {cluster: [] for cluster in clusterization.clusters.all()}
cluster_list = list(clusters)
n_clusters = len(cluster_list)

for i, user in enumerate(users):
cluster = cluster_list[i % n_clusters]
clusters[cluster].append(user)

votes = []
for cluster, users in clusters.items():
vote_profiles = (
StereotypeVote.objects
.filter(author__in=cluster.stereotypes.all())
.values('comment')
.annotate(average=Avg('choice'))
)
for data in vote_profiles:
comment_id = data['comment']
prob = 0.5 + data['average'] * 0.4

for user in users:
vote = random_vote(prob)

if vote is not None:
vote = comments_map[comment_id].vote(user, vote, commit=False)
votes.append(vote)

Vote.objects.bulk_create(votes)


def random_vote(prob):
r = random.random()
if r < 0.25:
vote = Choice.SKIP
elif r < 0.50:
vote = None
elif random.random() < prob:
vote = Choice.AGREE
else:
vote = Choice.DISAGREE
return vote


def set_clusters(conversation, stereotype_map=None, user_map=None, clean=False):
"""
Update cluster
Expand Down
15 changes: 8 additions & 7 deletions src/ej_clusters/management/commands/_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
"""

from django.contrib.auth import get_user_model
from faker import Factory

from ej_clusters.factories import set_clusters_from_comments
from ej_conversations import create_conversation
from ej_conversations.models import Conversation

fake = Factory.create()
User = get_user_model()

User = get_user_model()

def make_clusters(verbose=True, force=False):
if force:
Conversation.objects.filter(title='Economy').delete()

def make_clusters(verbose=True):
conversation = create_conversation(
'How should our society organize the production of goods and services?',
'Economy',
is_promoted=True,
author=User.objects.filter(is_staff=True).first(),
)
set_clusters_from_comments(conversation, {
Expand All @@ -33,5 +29,10 @@ def make_clusters(verbose=True, force=False):
'Government and the society as a whole must regulate business '
'decisions to favor the common good rather than private interests.',
'State leadership is necessary to drive a strong economy.',
],
'Facist': [
'Government should elliminate opposition in order to ensure '
'governability.',
'Military should occupy high ranks in government.',
]
})
12 changes: 10 additions & 2 deletions src/ej_clusters/management/commands/createfakeclusters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.core.management.base import BaseCommand

from django.contrib.auth import get_user_model
from ej_conversations.models import Conversation
from ._examples import make_clusters
from ...factories import cluster_votes
User = get_user_model()


class Command(BaseCommand):
Expand All @@ -19,4 +22,9 @@ def add_arguments(self, parser):
)

def handle(self, *args, silent=False, force=False, **options):
make_clusters(verbose=not silent, force=force)
if force:
Conversation.objects.filter(title='Economy').delete()
make_clusters(verbose=not silent)

conversation = Conversation.objects.get(title='Economy')
cluster_votes(conversation, User.objects.all())
4 changes: 2 additions & 2 deletions src/ej_conversations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
default_app_config = 'ej_conversations.apps.EjConversationsConfig'


def create_conversation(text, title, author, *, is_promoted=False, tags=(), commit=True):
def create_conversation(text, title, author, *, is_promoted=False, tags=(), commit=True, **kwargs):
"""
Creates a new conversation object and saves it in the database.
"""
from .models import Conversation

conversation = Conversation(text=text, title=title, author=author, is_promoted=is_promoted)
conversation = Conversation(text=text, title=title, author=author, is_promoted=is_promoted, **kwargs)
conversation.clean()
if commit:
conversation.save()
Expand Down

0 comments on commit 1d01a74

Please sign in to comment.