Skip to content

Commit

Permalink
Updated algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
bilimig committed Jun 23, 2024
1 parent a49b576 commit 4d0c404
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 50 deletions.
16 changes: 8 additions & 8 deletions Database/init-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ CREATE TABLE IF NOT EXISTS users.interest (
"name" text NOT NULL CONSTRAINT users_interest_name_maxlength CHECK (LENGTH("name") <= 32) UNIQUE
);

CREATE TABLE IF NOT EXISTS common.related (
"tag_name" text,
"interest_name" text,
"value" decimal,
UNIQUE ("tag_name", "interest_name")
CREATE TABLE IF NOT EXISTS common.tag_interest_simularity (
"tag" text,
"interest" text,
"simularity" decimal,
UNIQUE ("tag", "interest")
);

CREATE TABLE IF NOT EXISTS common.translated (
"name_pl" text,
"name_ang" text
CREATE TABLE IF NOT EXISTS common.translation (
"pl" text,
"ang" text
);

ALTER TABLE events.event ADD FOREIGN KEY ("address_id") REFERENCES common.address ("id");
Expand Down
2 changes: 1 addition & 1 deletion Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<IActionResult> GetRecomendetEvents(string username)
var currentUser = _userService.GetUserByUsername(username);
var interests = currentUser.Interests;

var events = await _recomendationService.GetEventsByInterest(interests);
var events = await _recomendationService.GetEventsByInterest(interests, username);

Check warning on line 44 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Possible null reference argument for parameter 'interestsDto' in 'Task<List<EventDto>> RecomendationService.GetEventsByInterest(List<UserInterestDto> interestsDto, string username)'.

Check warning on line 44 in Server/ReasnAPI/ReasnAPI/Controllers/UsersController.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Possible null reference argument for parameter 'interestsDto' in 'Task<List<EventDto>> RecomendationService.GetEventsByInterest(List<UserInterestDto> interestsDto, string username)'.

return Ok(events);
}
Expand Down
64 changes: 38 additions & 26 deletions Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,11 @@

namespace ReasnAPI.Services
{
public class RecomendationService
public class RecomendationService(ReasnContext context,EventService eventService, IConfiguration configuration)
{
private readonly HttpClient httpClient;
private readonly ReasnContext context;
private readonly string flaskApiUrl;
private readonly string _connectionString;
private readonly string? _connectionString = configuration.GetConnectionString("DefaultValue");

public RecomendationService(HttpClient httpClient, ReasnContext context, IConfiguration configuration)
{
this.httpClient = httpClient;
this.context = context;
_connectionString = configuration.GetConnectionString("DefaultValue");
}

public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> interestsDto)
public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> interestsDto, string username)
{
var interests = interestsDto.Select(i => i.Interest.Name).ToList();
var interestsLevels = interestsDto.Select(i => i.Level).ToList();
Expand All @@ -36,11 +26,6 @@ public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> inte
{
var similarTags = await GetSimilarTagsFromDb(interests);

if (similarTags == null || similarTags.Count == 0)
{
return new List<EventDto>();
}

var tagNames = similarTags.Select(t => t.Tag_Name).ToList();
var interestNames = similarTags.Select(t => t.Interest_Name).ToList();
var values = similarTags.Select(t => t.Value).ToList();
Expand All @@ -50,6 +35,17 @@ public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> inte
values[i] *= interestsLevels[interestNames.IndexOf(similarTags[i].Interest_Name)];
}

var userEvents = eventService.GetUserEvents(username);
var userEventTags = userEvents.SelectMany(e => e.Tags).Select(t => t.Name).Distinct().ToList();

Check warning on line 39 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Possible null reference return.

Check warning on line 39 in Server/ReasnAPI/ReasnAPI/Services/RecomendationService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Possible null reference return.

for (int i = 0; i < values.Count; i++)
{
if (userEventTags.Contains(tagNames[i]))
{
values[i] *= 1.25;
}
}

var events = await context.Events
.Include(e => e.Tags)
.Include(e => e.Parameters)
Expand All @@ -64,8 +60,24 @@ public async Task<List<EventDto>> GetEventsByInterest(List<UserInterestDto> inte
.Select(e => e.Event)
.ToListAsync();


var eventDtos = events.Select(e => e.ToDto()).ToList();

if (eventDtos.Count < 10)
{
int additionalEventsNeeded = 10 - eventDtos.Count;
var randomEvents = await context.Events
.Include(e => e.Tags)
.Include(e => e.Parameters)
.Where(e => !events.Contains(e))
.OrderBy(r => r.StartAt)
.Take(additionalEventsNeeded)
.ToListAsync();

eventDtos.AddRange(randomEvents.Select(e => e.ToDto()));

}

return eventDtos;
}
catch (Exception ex)
Expand All @@ -87,9 +99,9 @@ public async Task<List<TagInfo>> GetSimilarTagsFromDb(List<string> interests)


var query = @"
SELECT DISTINCT tag_name, interest_name, value
FROM common.related
WHERE interest_name = any(@interests) AND value > 0.3
SELECT DISTINCT tag, interest, simularity
FROM common.tag_interest_simularity
WHERE interest = any(@interests) AND simularity > 0.3
";

cmd.CommandText = query;
Expand All @@ -98,14 +110,14 @@ FROM common.related
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var tag_name = reader.GetString(reader.GetOrdinal("tag_name"));
var interest_name = reader.GetString(reader.GetOrdinal("interest_name"));
var value = reader.GetDecimal(reader.GetOrdinal("value"));
var tagName = reader.GetString(reader.GetOrdinal("tag"));
var interestName = reader.GetString(reader.GetOrdinal("interest"));
var value = reader.GetDecimal(reader.GetOrdinal("simularity"));

similarTags.Add(new TagInfo
{
Tag_Name = tag_name,
Interest_Name = interest_name,
Tag_Name = tagName,
Interest_Name = interestName,
Value = (double)value
});
}
Expand Down
20 changes: 10 additions & 10 deletions realeated.py → calculate_tag_interest_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def connect_to_db():
def get_translated_tags_and_interests(conn):
with conn.cursor() as cur:
query_translated_tags = """
SELECT t.name, tr.name_ang
SELECT t.name, tr.ang
FROM events.tag t
JOIN common.translated tr ON t.name = tr.name_pl
JOIN common.translation tr ON t.name = tr.pl
"""
query_translated_interests = """
SELECT i.name, tr.name_ang
SELECT i.name, tr.ang
FROM users.interest i
JOIN common.translated tr ON i.name = tr.name_pl
JOIN common.translation tr ON i.name = tr.pl
"""
cur.execute(query_translated_tags)
tags = cur.fetchall()
Expand All @@ -38,16 +38,16 @@ def get_missing_tags_and_interests(conn):
SELECT name
FROM events.tag
WHERE name NOT IN (
SELECT tag_name
FROM common.related
SELECT tag
FROM common.tag_interest_simularity
);
"""
query_missing_interests = """
SELECT name
FROM users.interest
WHERE name NOT IN (
SELECT interest_name
FROM common.related
SELECT interest
FROM common.tag_interest_simularity
);
"""
cur.execute(query_missing_tags)
Expand All @@ -71,9 +71,9 @@ def calculate_semantic_similarity_sbert(interests, tags):
def save_similarities_to_db(conn, tags, interests, similarities):
with conn.cursor() as cur:
insert_query = """
INSERT INTO common.related (tag_name, interest_name, value)
INSERT INTO common.tag_interest_simularity (tag, interest, simularity)
VALUES (%s, %s, %s)
ON CONFLICT (tag_name, interest_name) DO NOTHING
ON CONFLICT (tag, interest) DO NOTHING
"""
for i, tag in enumerate(tags):
for j, interest in enumerate(interests):
Expand Down
38 changes: 38 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
beautifulsoup4==4.12.3
certifi==2024.6.2
charset-normalizer==3.3.2
colorama==0.4.6
deep-translator==1.11.4
filelock==3.15.4
fsspec==2024.6.0
huggingface-hub==0.23.4
idna==3.7
intel-openmp==2021.4.0
Jinja2==3.1.4
joblib==1.4.2
MarkupSafe==2.1.5
mkl==2021.4.0
mpmath==1.3.0
networkx==3.3
numpy==2.0.0
packaging==24.1
pillow==10.3.0
psycopg2==2.9.9
psycopg2-binary==2.9.9
PyYAML==6.0.1
regex==2024.5.15
requests==2.32.3
safetensors==0.4.3
scikit-learn==1.5.0
scipy==1.13.1
sentence-transformers==3.0.1
soupsieve==2.5
sympy==1.12.1
tbb==2021.13.0
threadpoolctl==3.5.0
tokenizers==0.19.1
torch==2.3.1
tqdm==4.66.4
transformers==4.41.2
typing_extensions==4.12.2
urllib3==2.2.2
10 changes: 5 additions & 5 deletions translated.py → translate_tags_and_interests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ def get_untranslated_tags_and_interests(conn):
SELECT DISTINCT name AS name
FROM events.tag
WHERE name NOT IN (
SELECT name_pl
FROM common.translated
SELECT pl
FROM common.translation
);
"""
query_interests = """
SELECT DISTINCT name AS name
FROM users.interest
WHERE name NOT IN (
SELECT name_pl
FROM common.translated
SELECT pl
FROM common.translation
);
"""
cur.execute(query_tags)
Expand All @@ -46,7 +46,7 @@ def translate_to_english(texts):
def save_translations(conn, original_texts, translated_texts):
with conn.cursor() as cur:
insert_query = """
INSERT INTO common.translated (name_pl, name_ang)
INSERT INTO common.translation (pl, ang)
VALUES (%s, %s)
"""
for original, translated in zip(original_texts, translated_texts):
Expand Down

0 comments on commit 4d0c404

Please sign in to comment.