Skip to content

Commit

Permalink
b
Browse files Browse the repository at this point in the history
  • Loading branch information
krasotinpa committed Nov 13, 2018
1 parent 7e97450 commit 77cfbc5
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Java/task3_5_3/KeywordAnalyzer.java
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;
}
}
3 changes: 3 additions & 0 deletions Java/task3_5_3/Label.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum Label {
SPAM, NEGATIVE_TEXT, TOO_LONG, OK
}
10 changes: 10 additions & 0 deletions Java/task3_5_3/NegativeTextAnalyzer.java
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;
}
}
16 changes: 16 additions & 0 deletions Java/task3_5_3/SpamAnalyzer.java
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;
}
}
11 changes: 11 additions & 0 deletions Java/task3_5_3/TestObject.java
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;
}
}
3 changes: 3 additions & 0 deletions Java/task3_5_3/TextAnalyzer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface TextAnalyzer {
Label processText(String text);
}
9 changes: 9 additions & 0 deletions Java/task3_5_3/TooLongTextAnalyzer.java
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);
}
}
64 changes: 64 additions & 0 deletions Java/task3_5_3/task3_5_3.java
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++;
}
}
}

0 comments on commit 77cfbc5

Please sign in to comment.