Skip to content

Commit

Permalink
Merge pull request #4 from findsimilar/core_features
Browse files Browse the repository at this point in the history
find similar feature
  • Loading branch information
quillcraftsman authored Nov 4, 2023
2 parents 947db34 + 71a3964 commit 48f3469
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 31 deletions.
6 changes: 5 additions & 1 deletion analysis/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""
Admin page for analysis
"""
# from django.contrib import admin
from django.contrib import admin
from django_find_similar.models import TextToken
from django_find_similar.models.text import Token

# Register your models here.
admin.site.register(TextToken)
admin.site.register(Token)
9 changes: 9 additions & 0 deletions analysis/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ class LoadTrainingDataForm(forms.Form):
sheet_name = forms.IntegerField(required=False, initial=0, widget=forms.NumberInput(attrs={
'class': 'form-control'
}))


class FindSimilarForm(forms.Form):
"""
Form with one text
"""
text = forms.CharField(max_length=128, widget=forms.TextInput(attrs={
'class': 'form-control'
}))
6 changes: 4 additions & 2 deletions analysis/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
Analisys models
"""
from io import StringIO

import pandas as pd
from django.db import models

Expand All @@ -12,7 +14,7 @@ class TrainingData(models.Model):
update = models.DateTimeField(auto_now=True)

def get_dataframe(self) -> pd.DataFrame:
return pd.read_json(self.data, dtype=str)
return pd.read_json(StringIO(self.data), dtype=str)

@property
def columns_count(self):
Expand All @@ -29,4 +31,4 @@ def to_list(dataframe: pd.DataFrame) -> list:
for column in columns:
data_list = dataframe[column].values.tolist()
result += data_list
return result
return result
33 changes: 33 additions & 0 deletions analysis/templates/analysis/find_similar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends "base.html" %}
{% block main %}
<h2>{{object.name}}</h2>
<p></p>
<form method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-primary">Find Similar</button>
</form>
{% endblock %}
{% block results %}
{% with object.get_dataframe as data %}
<table class="table">
<tr>
{% for column in data.columns %}
<th>
{{column}}
</th>
{% endfor %}
</tr>
{% for index, row in data.iterrows %}
<tr>
{% for cell in row %}
<td>
{{cell}}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endwith %}

{% endblock %}
57 changes: 57 additions & 0 deletions analysis/templates/analysis/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{% extends "base.html" %}
{% block main %}
{% with object.text as text %}
<table class="table border">
<tr>
<th>Name</th>
<th>Value</th>
</tr>
<tr>
<td>
<b>Text</b>
</td>
<td>
{{text.text}}
</td>
</tr>
<tr>
<td>
<b>Language</b>
</td>
<td>
{{text.language}}
</td>
</tr>
<tr>
<td>
<b>Remove Stop Words</b>
</td>
<td>
{{text.remove_stopwords}}
</td>
</tr>
</table>
{% endwith %}
{% endblock %}
{% block results %}

{% with object.checkresultitem_set.all as items %}
<table class="table table-striped">
<tr>
<th>order</th>
<th>text</th>
<th>cos</th>
<th>%</th>
</tr>
{% for item in items %}
<tr>
<td>{{item.order}}</td>
<td>{{item.text.text}}</td>
<td>{{item.cos}}</td>
<td>{{item.cos_percent}}%</td>
</tr>
{% endfor %}
</table>

{% endwith %}
{% endblock %}
36 changes: 36 additions & 0 deletions analysis/templates/analysis/result_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends "base.html" %}
{% block main %}
<h2>Results List</h2>
{% endblock %}
{% block results %}
<table class="table">
<tr>
<th>Text</th>
<th>Language</th>
<th>Remove Stop Words</th>
<th>Created</th>
<th>Detail</th>
</tr>
{% for result in object_list %}
{% with result.text as text %}
<tr>
<td>
<a href="{% url 'analysis:result' pk=result.pk %}">
{{text.text}}
</a>
</td>
<td>{{text.language}}</td>
<td>{{text.remove_stopwords}}</td>
<td>
{{result.create}}
</td>
<td>
<a class="btn btn-info" href="{% url 'analysis:result' pk=result.pk %}">
Detail
</a>
</td>
</tr>
{% endwith %}
{% endfor %}
</table>
{% endblock %}
2 changes: 1 addition & 1 deletion analysis/templates/analysis/training_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ <h2>{{object.name}}</h2>
<p></p>
<a class="btn btn-primary" href="#">Total rating</a>
<a class="btn btn-info" href="#">One column rating</a>
<a class="btn btn-info" href="#">Find similar</a>
<a class="btn btn-info" href="{% url 'analysis:find_similar' pk=object.pk %}">Find similar</a>
<a class="btn btn-second" href="#">Tokenize</a>
<a class="btn btn-danger" href="{% url 'analysis:delete_training_data' pk=object.pk %}">Delete</a>
{% endblock %}
Expand Down
14 changes: 14 additions & 0 deletions analysis/tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
from django.test import SimpleTestCase, TestCase
from django.urls import reverse
from django_find_similar.models import CheckResult
from mixer.backend.django import mixer

from analysis.tests.data import get_2x2_training_data

Expand Down Expand Up @@ -38,6 +40,10 @@ def test_reverse(self):
'url': 'training_data_list',
'reverse': 'training-data-list/',
},
{
'url': 'result_list',
'reverse': 'result-list/',
},
]
for url in urls:
app_url = f'{app_name}:{url["url"]}'
Expand All @@ -58,6 +64,7 @@ def test_reverse(self):
"""

training_data = get_2x2_training_data()
check_result = mixer.blend(CheckResult)

app_name = 'analysis'
urls = [
Expand All @@ -82,6 +89,13 @@ def test_reverse(self):
},
'reverse': f'find-similar/{training_data.pk}/',
},
{
'url': 'result',
'kwargs': {
'pk': check_result.pk
},
'reverse': f'result/{check_result.pk}/',
},
]
for url in urls:
app_url = f'{app_name}:{url["url"]}'
Expand Down
24 changes: 24 additions & 0 deletions analysis/tests/tests_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
OneTextForm,
TwoTextForm,
LoadTrainingDataForm,
FindSimilarForm,
)


Expand Down Expand Up @@ -78,4 +79,27 @@ def test_fields(self):
)

current_form = LoadTrainingDataForm()
self.assertTrueForm(current_form, true_form)


class TestFindSimilarForm(SimpleTestCase):
"""
One text form test
"""

def test_fields(self):
"""
Test available fields
"""

true_form = TrueForm(
fields=Fields(
count=1,
types={
'text': forms.CharField
}
)
)

current_form = FindSimilarForm()
self.assertTrueForm(current_form, true_form)
76 changes: 74 additions & 2 deletions analysis/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
from django_find_similar.models import CheckResult
from dry_tests import (
TestCase,
SimpleTestCase,
Expand All @@ -12,6 +13,8 @@
Context,
POST,
)
from django_find_similar.forms import FindSimilarForm
from mixer.backend.django import mixer
from analysis.forms import OneTextForm, TwoTextForm, LoadTrainingDataForm
from analysis.models import TrainingData
from analysis.tests.data import get_2x2_filepath, get_2x2_training_data
Expand Down Expand Up @@ -485,12 +488,81 @@ def test_get(self):
true_response = TrueResponse(
status_code=200,
context=Context(
keys=['object'],
keys=['object', 'form'],
items={
'object': self.training_data,
},
types={
'form': FindSimilarForm
}
)
)
current_response = request.get_response(self.client)
self.assertTrueResponse(current_response, true_response)

def test_post(self):

data = {
'text': '1',
'language': 'english',
'remove_stopwords': True,
}

request = Request(
url=self.url,
method=POST,
data=data
)

true_response = TrueResponse(
status_code=302,
redirect_url=f'/analysis/result-list/'
)

current_response = request.get_response(self.client)
self.assertTrueResponse(current_response, true_response)


class ResultListTestCase(TestCase):

def setUp(self):
self.url = reverse('analysis:result_list')

def test_get(self):
request = Request(
url=self.url
)

self.assertTrueResponse(current_response, true_response)
true_response = TrueResponse(
status_code=200,
context=Context(
keys=['object_list'],
),
)

current_response = request.get_response(self.client)
self.assertTrueResponse(current_response, true_response)


class TestResultDetailView(TestCase):

def setUp(self):
self.check_result = mixer.blend(CheckResult)
self.url = reverse('analysis:result', kwargs={'pk': self.check_result.pk})

def test_get(self):
request = Request(
url=self.url,
)

true_response = TrueResponse(
status_code=200,
context=Context(
items={
'object': self.check_result,
}
)
)

current_response = request.get_response(self.client)
self.assertTrueResponse(current_response, true_response)
4 changes: 3 additions & 1 deletion analysis/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
path('example-frequency/', views.ExampleFrequencyAnalysis.as_view(), name="example_frequency"),
path('load-training-data/', views.LoadTrainingDataView.as_view(), name="load_training_data"),
path('training-data/<int:pk>/', views.TrainingDataDetailView.as_view(), name="training_data"),
path('find-similar/<int:pk>/', views.TrainingDataDetailView.as_view(), name="find_similar"),
path('find-similar/<int:pk>/', views.FindSimilarFormView.as_view(), name="find_similar"),
path('delete-training-data/<int:pk>/', views.TrainingDataDeleteView.as_view(), name="delete_training_data"),
path('training-data-list/', views.TrainingDataListView.as_view(), name="training_data_list"),
path('result-list/', views.ResultListView.as_view(), name="result_list"),
path('result/<int:pk>/', views.ResultDetailView.as_view(), name="result"),
]
Loading

0 comments on commit 48f3469

Please sign in to comment.