1
1
class SpamDetectionService
2
- ClassificationResult = Data . define ( :is_spam , :target )
3
-
4
2
def initialize ( tg_message_struct )
5
3
@tg_message_struct = tg_message_struct
6
4
@group_id = tg_message_struct . chat . id
@@ -14,6 +12,12 @@ def initialize(tg_message_struct)
14
12
def process
15
13
return non_spam_result unless valid_message?
16
14
15
+ rule_result = RuleBasedClassifier . new ( @message_text ) . classify
16
+ if rule_result . is_spam
17
+ create_trained_message ( @message_text , rule_result . target )
18
+ return rule_result
19
+ end
20
+
17
21
targets_to_check = [
18
22
{ name : "message_content" , value : @message_text } ,
19
23
{ name : "user_name" , value : @username }
@@ -56,7 +60,7 @@ def handle_existing_message(existing_message)
56
60
when "spam"
57
61
@is_confident = true
58
62
Rails . logger . info "Same message exists and already marked as spam: #{ existing_message . message } , training target: #{ existing_message . training_target } "
59
- ClassificationResult . new ( is_spam : true , target : existing_message . training_target )
63
+ Shared :: ClassificationResult . new ( is_spam : true , target : existing_message . training_target )
60
64
when "ham"
61
65
Rails . logger . info "Same message exists and already marked as ham: #{ existing_message . message } , training target: #{ existing_message . training_target } "
62
66
non_spam_result
@@ -66,12 +70,13 @@ def handle_existing_message(existing_message)
66
70
end
67
71
end
68
72
73
+
69
74
def classify_with_bayesian ( target_name , target_value )
70
75
classifier = build_classifier ( target_name )
71
76
is_spam , spam_score , ham_score = classifier . classify ( target_value )
72
77
73
78
Rails . logger . info "Classified '#{ target_value } ' against '#{ target_name } ': is_spam=#{ is_spam } , spam_score=#{ spam_score } , ham_score=#{ ham_score } "
74
- ClassificationResult . new ( is_spam : is_spam , target : target_name )
79
+ Shared :: ClassificationResult . new ( is_spam : is_spam , target : target_name )
75
80
end
76
81
77
82
def build_classifier ( target_name )
@@ -105,6 +110,6 @@ def valid_message?
105
110
end
106
111
107
112
def non_spam_result
108
- ClassificationResult . new ( is_spam : false , target : nil )
113
+ Shared :: ClassificationResult . new ( is_spam : false , target : nil )
109
114
end
110
115
end
0 commit comments