-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractor.cs
More file actions
124 lines (104 loc) · 5.18 KB
/
Copy pathExtractor.cs
File metadata and controls
124 lines (104 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace shookayNET
{
public static class Extractor
{
public static HashSet<string> GetWords(string wyrazenie)
{
Console.WriteLine($"dodaje {wyrazenie}");
if (string.IsNullOrWhiteSpace(wyrazenie))
{
return new HashSet<string>();
}
var przecinki = new List<char> { ' ', '-', '.', '/', '\n', '\r' }.ToArray();
var wszystkieslowa = wyrazenie.Split(przecinki);
return new HashSet<string>(wszystkieslowa.Select(w => w.ToLower().Trim()));
}
public static KeyValuePair<int, string> WordExtractor<T>(T item, string? idname = null) where T : class
{
ArgumentNullException.ThrowIfNull(item);
HashSet<string> resultStrings = new();
int id = 0;
bool idFound = false;
bool multipleIdProperties = false;
ExtractProperties(item, ref resultStrings, ref id, ref idFound, ref multipleIdProperties, idname, true);
if (multipleIdProperties)
{
throw new InvalidOperationException("More than one potential ID property found.");
}
if (!idFound)
{
throw new InvalidOperationException("ID property not found.");
}
return new KeyValuePair<int, string>(id, string.Join(" ", resultStrings));
}
private static void ExtractProperties<T>(T item, ref HashSet<string> resultStrings, ref int id, ref bool idFound, ref bool multipleIdProperties, string? idname, bool isRootObject) where T : class
{
Console.WriteLine($"Analyzing properties of object: {item.GetType().Name}");
foreach (PropertyInfo prop in item.GetType().GetProperties())
{
Console.WriteLine($"Checking property: {prop.Name}, Type: {prop.PropertyType.Name}");
// Ignoring virtual properties (except strings)
if (prop.GetGetMethod().IsVirtual && prop.PropertyType != typeof(string))
{
Console.WriteLine($"Ignoring virtual property: {prop.Name}");
continue;
}
if (prop.PropertyType == typeof(string))
{
string value = (string)prop.GetValue(item);
if (!string.IsNullOrWhiteSpace(value))
{
Console.WriteLine($"Adding string property: {prop.Name}, Value: {value}");
resultStrings.UnionWith(GetWords(value));
}
}
else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string))
{
Console.WriteLine($"Processing collection property: {prop.Name}");
var enumerable = (System.Collections.IEnumerable)prop.GetValue(item);
foreach (var element in enumerable)
{
if (element.GetType().IsClass)
{
Console.WriteLine($"Processing element of type: {element.GetType().Name} in collection {prop.Name}");
ExtractProperties(element, ref resultStrings, ref id, ref idFound, ref multipleIdProperties, idname, false);
}
}
}
else if (!prop.PropertyType.IsPrimitive && prop.PropertyType != typeof(DateTime) && prop.PropertyType != typeof(string) && prop.GetValue(item) != null)
{
Console.WriteLine($"Processing nested object property: {prop.Name}");
ExtractProperties(prop.GetValue(item), ref resultStrings, ref id, ref idFound, ref multipleIdProperties, idname, false);
}
if (!idFound && isRootObject && (prop.Name.Equals("Id", StringComparison.OrdinalIgnoreCase)
|| prop.Name.Equals(item.GetType().Name + "Id", StringComparison.OrdinalIgnoreCase)
|| prop.Name.Equals(idname, StringComparison.OrdinalIgnoreCase)))
{
if (prop.PropertyType != typeof(int))
{
throw new InvalidOperationException("ID property is not an integer.");
}
if (idFound && isRootObject)
{
multipleIdProperties = true;
Console.WriteLine($"Multiple ID properties found, which is invalid.");
return; // No need to continue
}
id = (int)prop.GetValue(item);
idFound = true;
Console.WriteLine($"ID property found: {prop.Name}, Value: {id}");
}
}
if (isRootObject && idname != null && !idFound)
{
throw new InvalidOperationException($"ID property with name '{idname}' not found.");
}
}
}
}