-
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.
- Loading branch information
1 parent
7e97450
commit 77cfbc5
Showing
8 changed files
with
130 additions
and
0 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,14 @@ | ||
public abstract class KeywordAnalyzer implements TextAnalyzer { | ||
|
||
protected abstract String[] getKeywords(); | ||
protected abstract Label getLabel(); | ||
|
||
public Label processText(String text) { | ||
for (String k: this.getKeywords()) { | ||
if (text.contains(k)) { | ||
return this.getLabel(); | ||
} | ||
} | ||
return Label.OK; | ||
} | ||
} |
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,3 @@ | ||
public enum Label { | ||
SPAM, NEGATIVE_TEXT, TOO_LONG, OK | ||
} |
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,10 @@ | ||
public class NegativeTextAnalyzer extends KeywordAnalyzer { | ||
private String[] keywords = {":(", "=(", ":|"}; | ||
|
||
protected Label getLabel() { | ||
return Label.NEGATIVE_TEXT; | ||
} | ||
protected String[] getKeywords() { | ||
return this.keywords; | ||
} | ||
} |
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,16 @@ | ||
public class SpamAnalyzer extends KeywordAnalyzer { | ||
private String[] keywords; | ||
|
||
public SpamAnalyzer (String[] keywords) { | ||
String[] skey = new String[keywords.length]; | ||
System.arraycopy(keywords, 0, skey, 0, skey.length); | ||
this.keywords = skey; | ||
} | ||
|
||
protected Label getLabel() { | ||
return Label.SPAM; | ||
} | ||
protected String[] getKeywords() { | ||
return this.keywords; | ||
} | ||
} |
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,11 @@ | ||
public class TestObject { | ||
public Label checkLabels(TextAnalyzer[] analyzers, String text) { | ||
for (TextAnalyzer a: analyzers) { | ||
Label L = a.processText(text); | ||
if (L != Label.OK) { | ||
return L; | ||
} | ||
} | ||
return Label.OK; | ||
} | ||
} |
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,3 @@ | ||
public interface TextAnalyzer { | ||
Label processText(String text); | ||
} |
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,9 @@ | ||
public class TooLongTextAnalyzer implements TextAnalyzer { | ||
private int maxLength = 0; | ||
public TooLongTextAnalyzer(int maxLength) { | ||
this.maxLength = maxLength; | ||
} | ||
public Label processText(String text) { | ||
return (text.length() > this.maxLength ? Label.TOO_LONG : Label.OK); | ||
} | ||
} |
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,64 @@ | ||
public class task3_5_3 { | ||
public static void main(String[] args) { | ||
// инициализация анализаторов для проверки в порядке данного набора анализаторов | ||
String[] spamKeywords = {"spam", "bad"}; | ||
int commentMaxLength = 40; | ||
TextAnalyzer[] textAnalyzers1 = { | ||
new SpamAnalyzer(spamKeywords), | ||
new NegativeTextAnalyzer(), | ||
new TooLongTextAnalyzer(commentMaxLength) | ||
}; | ||
TextAnalyzer[] textAnalyzers2 = { | ||
new SpamAnalyzer(spamKeywords), | ||
new TooLongTextAnalyzer(commentMaxLength), | ||
new NegativeTextAnalyzer() | ||
}; | ||
TextAnalyzer[] textAnalyzers3 = { | ||
new TooLongTextAnalyzer(commentMaxLength), | ||
new SpamAnalyzer(spamKeywords), | ||
new NegativeTextAnalyzer() | ||
}; | ||
TextAnalyzer[] textAnalyzers4 = { | ||
new TooLongTextAnalyzer(commentMaxLength), | ||
new NegativeTextAnalyzer(), | ||
new SpamAnalyzer(spamKeywords) | ||
}; | ||
TextAnalyzer[] textAnalyzers5 = { | ||
new NegativeTextAnalyzer(), | ||
new SpamAnalyzer(spamKeywords), | ||
new TooLongTextAnalyzer(commentMaxLength) | ||
}; | ||
TextAnalyzer[] textAnalyzers6 = { | ||
new NegativeTextAnalyzer(), | ||
new TooLongTextAnalyzer(commentMaxLength), | ||
new SpamAnalyzer(spamKeywords) | ||
}; | ||
// тестовые комментарии | ||
String[] tests = { | ||
"This comment is so good.", // OK | ||
"This comment is so Loooooooooooooooooooooooooooong.", // TOO_LONG | ||
"Very negative comment !!!!=(!!!!;", // NEGATIVE_TEXT | ||
"Very BAAAAAAAAAAAAAAAAAAAAAAAAD comment with :|;", // NEGATIVE_TEXT or TOO_LONG | ||
"This comment is so bad....", // SPAM | ||
"The comment is a spam, maybeeeeeeeeeeeeeeeeeeeeee!", // SPAM or TOO_LONG | ||
"Negative bad :( spam.", // SPAM or NEGATIVE_TEXT | ||
"Very bad, very neg =(, very .................." // SPAM or NEGATIVE_TEXT or TOO_LONG | ||
}; | ||
TextAnalyzer[][] textAnalyzers = {textAnalyzers1, textAnalyzers2, textAnalyzers3, | ||
textAnalyzers4, textAnalyzers5, textAnalyzers6}; | ||
TestObject testObject = new TestObject(); | ||
int numberOfAnalyzer; // номер анализатора, указанный в идентификаторе textAnalyzers{№} | ||
int numberOfTest = 0; // номер теста, который соответствует индексу тестовых комментариев | ||
for (String test : tests) { | ||
numberOfAnalyzer = 1; | ||
System.out.print("test #" + numberOfTest + ": "); | ||
System.out.println(test); | ||
for (TextAnalyzer[] analyzers : textAnalyzers) { | ||
System.out.print(numberOfAnalyzer + ": "); | ||
System.out.println(testObject.checkLabels(analyzers, test)); | ||
numberOfAnalyzer++; | ||
} | ||
numberOfTest++; | ||
} | ||
} | ||
} |