-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add points to creator of vote when that vote has been voted on * Check if poll has ended when voting * Add assignpoints to assign points when a poll has ended * rename x to suggestion * Add poll end timer * Update migrations * add HasPointsAssigned to Poll * Create timers for polls that haven't been assigned points yet. * update swashbuckle * check if tag already exists * rename SuggestionsIds to SuggestionIds * feat: SimilarityCheck.cs added to Tags * Update PollController.cs --------- Co-authored-by: KaanSecen <[email protected]> Co-authored-by: Kaan Secen <[email protected]>
- Loading branch information
1 parent
99acde9
commit 308b143
Showing
19 changed files
with
968 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Talpa_Api.Models; | ||
using Talpa_Api.Contexts; | ||
|
||
namespace Talpa_Api.Algorithms | ||
{ | ||
public abstract class SimilarityCheck | ||
{ | ||
public readonly struct ObjectWithSimilarity(int suggestionId, string title, double similarity) | ||
{ | ||
public int Id { get; init; } = suggestionId; | ||
public string Title { get; init; } = title; | ||
public double Similarity { get; init; } = similarity; | ||
} | ||
|
||
private static double CalculateSimilarityPercentage(string string1, string string2) | ||
{ | ||
var pairs1 = WordLetterPairs(string1.ToUpper()); | ||
var pairs2 = WordLetterPairs(string2.ToUpper()); | ||
|
||
var intersection = 0; | ||
var union = pairs1.Count + pairs2.Count; | ||
|
||
foreach (var pair1 in pairs1) | ||
{ | ||
for (var number = 0; number < pairs2.Count; number++) | ||
{ | ||
if (pair1 != pairs2[number]) continue; | ||
|
||
intersection++; | ||
pairs2.RemoveAt(number); | ||
break; | ||
} | ||
} | ||
|
||
// return the percentage of similarity | ||
return 2.0 * intersection * 100 / union; | ||
} | ||
|
||
private static List<string> WordLetterPairs(string str) | ||
{ | ||
var allPairs = new List<string>(); | ||
var words = str.Split(' '); | ||
|
||
foreach (var word in words) | ||
{ | ||
for (var number = 0; number < word.Length - 1; number++) | ||
{ | ||
allPairs.Add(word.Substring(number, 2)); | ||
} | ||
} | ||
|
||
return allPairs; | ||
} | ||
|
||
public static (List<ObjectWithSimilarity> objects, double max) GetObjectWithSimilarity(string title, dynamic dbSet) | ||
{ | ||
var objectWithSimilarity = new List<ObjectWithSimilarity>(); | ||
var maxSimilarity = 0.0; | ||
|
||
foreach (var obj in dbSet) | ||
{ | ||
var sim = CalculateSimilarityPercentage(obj.Title, title); | ||
|
||
if (sim > maxSimilarity) maxSimilarity = sim; | ||
|
||
if (sim > 50) objectWithSimilarity.Add(new ObjectWithSimilarity(obj.Id, obj.Title, sim)); | ||
} | ||
|
||
return (objectWithSimilarity, maxSimilarity); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.Extensions.Localization; | ||
using Talpa_Api.Contexts; | ||
using Talpa_Api.Controllers.Api; | ||
using Talpa_Api.Localization; | ||
using Talpa_Api.Models; | ||
using Timer = System.Timers.Timer; | ||
|
||
namespace Talpa_Api.Events; | ||
|
||
public static class PollEndTimer | ||
{ | ||
private static readonly List<Timer> Timers = []; | ||
|
||
public static void CreateTimer(Poll poll) | ||
{ | ||
switch (poll) | ||
{ | ||
case { HasEnded: true, HasPointsAssigned: true }: | ||
return; | ||
case { HasEnded: true, HasPointsAssigned: false }: | ||
PollController.AssignPoints(poll.Id); | ||
return; | ||
default: | ||
{ | ||
var timer = new Timer | ||
{ | ||
Interval = poll.EndDate.Subtract(DateTime.UtcNow).TotalMilliseconds, | ||
AutoReset = false, | ||
}; | ||
|
||
timer.Elapsed += (_, _) => PollController.AssignPoints(poll.Id); | ||
|
||
timer.Start(); | ||
|
||
Timers.Add(timer); | ||
break; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.