2
0

Compare commits

..

3 Commits

2 changed files with 45 additions and 34 deletions

View File

@ -61,21 +61,28 @@ public class Mangasee : MangaConnector
} }
} }
private readonly string[] _filterWords = {"a", "the", "of", "as", "to", "no", "for", "on", "with", "be", "and", "in", "wa", "at", "be", "ni"};
private string ToFilteredString(string input) => string.Join(' ', input.ToLower().Split(' ').Where(word => _filterWords.Contains(word)));
private SearchResult[] FilteredResults(string publicationTitle, SearchResult[] unfilteredSearchResults) private SearchResult[] FilteredResults(string publicationTitle, SearchResult[] unfilteredSearchResults)
{ {
Dictionary<SearchResult, int> similarity = new(); Dictionary<SearchResult, int> similarity = new();
foreach (SearchResult sr in unfilteredSearchResults) foreach (SearchResult sr in unfilteredSearchResults)
{ {
List<int> scores = new(); List<int> scores = new();
foreach (string se in sr.a) string filteredPublicationString = ToFilteredString(publicationTitle);
scores.Add(NeedlemanWunschStringUtil.CalculateSimilarity(se.ToLower(), publicationTitle.ToLower())); string filteredSString = ToFilteredString(sr.s);
scores.Add(NeedlemanWunschStringUtil.CalculateSimilarity(sr.s.ToLower(), publicationTitle.ToLower())); scores.Add(NeedlemanWunschStringUtil.CalculateSimilarity(filteredSString, filteredPublicationString));
foreach (string srA in sr.a)
{
string filteredAString = ToFilteredString(srA);
scores.Add(NeedlemanWunschStringUtil.CalculateSimilarity(filteredAString, filteredPublicationString));
}
similarity.Add(sr, scores.Sum() / scores.Count); similarity.Add(sr, scores.Sum() / scores.Count);
} }
SearchResult[] similarity90 = similarity.Where(s => s.Value < 10).Select(s => s.Key).ToArray(); List<SearchResult> ret = similarity.OrderBy(s => s.Value).Take(10).Select(s => s.Key).ToList();
ret.AddRange(similarity.Where(s => s.Value < 5).Select(s => s.Key));
return similarity90; return ret.ToArray();
} }
public override Manga? GetMangaFromId(string publicationId) public override Manga? GetMangaFromId(string publicationId)

View File

@ -712,6 +712,10 @@ public class Server : GlobalBase
private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null) private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null)
{ {
if (response.OutputStream.CanWrite == false)
{
Log($"No response sent to request: Stream closed before response could be sent.");
}
//Log($"Response: {statusCode} {content}"); //Log($"Response: {statusCode} {content}");
response.StatusCode = (int)statusCode; response.StatusCode = (int)statusCode;
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With"); response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
@ -719,20 +723,14 @@ public class Server : GlobalBase
response.AddHeader("Access-Control-Max-Age", "1728000"); response.AddHeader("Access-Control-Max-Age", "1728000");
response.AppendHeader("Access-Control-Allow-Origin", "*"); response.AppendHeader("Access-Control-Allow-Origin", "*");
try
{
if (content is not Stream) if (content is not Stream)
{ {
response.ContentType = "application/json"; response.ContentType = "application/json";
try
{
response.OutputStream.Write(content is not null response.OutputStream.Write(content is not null
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)) ? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
: Array.Empty<byte>()); : Array.Empty<byte>());
response.OutputStream.Close();
}
catch (HttpListenerException e)
{
Log(e.ToString());
}
} }
else if (content is FileStream stream) else if (content is FileStream stream)
{ {
@ -753,9 +751,15 @@ public class Server : GlobalBase
response.ContentType = "text/plain"; response.ContentType = "text/plain";
break; break;
} }
stream.CopyTo(response.OutputStream); stream.CopyTo(response.OutputStream);
response.OutputStream.Close();
stream.Close(); stream.Close();
} }
response.OutputStream.Close();
}
catch (HttpListenerException e)
{
Log(e.ToString());
}
} }
} }