Skip to content

Commit 7f6b45a

Browse files
mnalissivaraam
andauthored
Update allowed recent years to include 2020s (#5761)
* document regex due to #47 * also count 2020s as "recent years" * clarify that not all years are ignored * clarify "year" is current year * original logic fix from #5761 (review) * better variale name for ".*0s.*" as that regex will match e.g. `1920s` and `80s` too, so the original `is20xxsYear` would be confusing name for it * consolidate duplicated code to spammyCategory * clarify regexes via variables * spammyCategory should always be skipped * return is simple now, so we can get rid of extra val oldDecade * fix curYearInString * some clarification comments * refactor: rename containsYear to isSpammyCategory This is done as the name containsYear is ambiguous. It not just checks for year to identify spammy categories. * refactor: rename containsYear to isSpammyCategory (take 2) A continuation of fe74c77 (refactor: rename containsYear to isSpammyCategory, 2024-07-17) --------- Co-authored-by: Kaartic Sivaraam <[email protected]>
1 parent 34addbe commit 7f6b45a

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

app/src/main/java/fr/free/nrw/commons/category/CategoriesModel.kt

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,42 @@ class CategoriesModel @Inject constructor(
2727
private var selectedExistingCategories: MutableList<String> = mutableListOf()
2828

2929
/**
30-
* Returns if the item contains an year
31-
* @param item
30+
* Returns true if an item is considered to be a spammy category which should be ignored
31+
*
32+
* @param item a category item that needs to be validated to know if it is spammy or not
3233
* @return
3334
*/
34-
fun containsYear(item: String): Boolean {
35+
fun isSpammyCategory(item: String): Boolean {
3536
//Check for current and previous year to exclude these categories from removal
3637
val now = Calendar.getInstance()
37-
val year = now[Calendar.YEAR]
38-
val yearInString = year.toString()
39-
val prevYear = year - 1
38+
val curYear = now[Calendar.YEAR]
39+
val curYearInString = curYear.toString()
40+
val prevYear = curYear - 1
4041
val prevYearInString = prevYear.toString()
4142
Timber.d("Previous year: %s", prevYearInString)
4243

43-
//Check if item contains a 4-digit word anywhere within the string (.* is wildcard)
44-
//And that item does not equal the current year or previous year
45-
//And if it is an irrelevant category such as Media_needing_categories_as_of_16_June_2017(Issue #750)
46-
//Check if the year in the form of XX(X)0s is relevant, i.e. in the 2000s or 2010s as stated in Issue #1029
47-
return item.matches(".*(19|20)\\d{2}.*".toRegex())
48-
&& !item.contains(yearInString)
49-
&& !item.contains(prevYearInString)
50-
|| item.matches("(.*)needing(.*)".toRegex())
51-
|| item.matches("(.*)taken on(.*)".toRegex())
52-
|| item.matches(".*0s.*".toRegex())
53-
&& !item.matches(".*(200|201)0s.*".toRegex())
44+
val mentionsDecade = item.matches(".*0s.*".toRegex())
45+
val recentDecade = item.matches(".*20[0-2]0s.*".toRegex())
46+
val spammyCategory = item.matches("(.*)needing(.*)".toRegex())
47+
|| item.matches("(.*)taken on(.*)".toRegex())
48+
49+
// always skip irrelevant categories such as Media_needing_categories_as_of_16_June_2017(Issue #750)
50+
if (spammyCategory) {
51+
return true
52+
}
53+
54+
if (mentionsDecade) {
55+
// Check if the year in the form of XX(X)0s is recent/relevant, i.e. in the 2000s or 2010s/2020s as stated in Issue #1029
56+
// Example: "2020s" is OK, but "1920s" is not (and should be skipped)
57+
return !recentDecade
58+
} else {
59+
// If it is not an year in decade form (e.g. 19xxs/20xxs), then check if item contains a 4-digit year
60+
// anywhere within the string (.* is wildcard) (Issue #47)
61+
// And that item does not equal the current year or previous year
62+
return item.matches(".*(19|20)\\d{2}.*".toRegex())
63+
&& !item.contains(curYearInString)
64+
&& !item.contains(prevYearInString)
65+
}
5466
}
5567

5668
/**

app/src/main/java/fr/free/nrw/commons/repository/UploadRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public void onCategoryClicked(CategoryItem categoryItem, final Media media) {
148148
* @param name
149149
* @return
150150
*/
151-
public boolean containsYear(String name) {
152-
return categoriesModel.containsYear(name);
151+
public boolean isSpammyCategory(String name) {
152+
return categoriesModel.isSpammyCategory(name);
153153
}
154154

155155
/**

app/src/main/java/fr/free/nrw/commons/upload/categories/CategoriesPresenter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class CategoriesPresenter @Inject constructor(
8989
if (media == null) {
9090
return repository.searchAll(term, getImageTitleList(), repository.selectedDepictions)
9191
.subscribeOn(ioScheduler)
92-
.map { it.filter { categoryItem -> !repository.containsYear(categoryItem.name)
92+
.map { it.filter { categoryItem -> !repository.isSpammyCategory(categoryItem.name)
9393
|| categoryItem.name==term } }
9494
} else {
9595
return Observable.zip(
@@ -103,7 +103,7 @@ class CategoriesPresenter @Inject constructor(
103103
it1 + it2
104104
}
105105
.subscribeOn(ioScheduler)
106-
.map { it.filter { categoryItem -> !repository.containsYear(categoryItem.name)
106+
.map { it.filter { categoryItem -> !repository.isSpammyCategory(categoryItem.name)
107107
|| categoryItem.name==term } }
108108
.map { it.filterNot { categoryItem -> categoryItem.thumbnail == "hidden" } }
109109
}

app/src/test/kotlin/fr/free/nrw/commons/upload/CategoriesPresenterTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import media
1414
import org.junit.Before
1515
import org.junit.Test
1616
import org.mockito.Mock
17-
import org.mockito.Mockito.verifyNoInteractions
1817
import org.mockito.MockitoAnnotations
1918
import java.lang.reflect.Method
2019

@@ -97,8 +96,8 @@ class CategoriesPresenterTest {
9796
)
9897
)
9998
)
100-
whenever(repository.containsYear("selected")).thenReturn(false)
101-
whenever(repository.containsYear("doesContainYear")).thenReturn(true)
99+
whenever(repository.isSpammyCategory("selected")).thenReturn(false)
100+
whenever(repository.isSpammyCategory("doesContainYear")).thenReturn(true)
102101
whenever(repository.selectedCategories).thenReturn(listOf(
103102
categoryItem("selected", "", "",true)))
104103
categoriesPresenter.searchForCategories("test")

app/src/test/kotlin/fr/free/nrw/commons/upload/UploadRepositoryUnitTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class UploadRepositoryUnitTest {
156156
@Test
157157
fun testContainsYear() {
158158
assertEquals(
159-
repository.containsYear(""), categoriesModel.containsYear("")
159+
repository.isSpammyCategory(""), categoriesModel.isSpammyCategory("")
160160
)
161161
}
162162

0 commit comments

Comments
 (0)