Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

言語をなんとかするやつ #1440

Merged
merged 7 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions atcoder-problems-backend/sql-client/src/language_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::models::{Submission, UserLanguageCount, UserLanguageCountRank, UserPr
use crate::{PgPool, MAX_INSERT_ROWS};
use anyhow::Result;
use async_trait::async_trait;
use regex::Regex;
use sqlx::postgres::PgRow;
use sqlx::Row;
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -192,12 +191,33 @@ impl LanguageCountClient for PgPool {
}
}

const MAPPING: [(&str, &str); 9] = [
("PyPy", "Python"),
("Python (Cython", "Cython"),
("Assembly x64", "Assembly x64"),
("Awk", "AWK"),
("IOI-Style", "C++"),
("LuaJIT", "Lua"),
("Seed7", "Seed7"),
("Perl6", "Raku"),
("Objective-C", "Objective-C"),
];

fn simplify_language(lang: &str) -> String {
let re = Regex::new(r"\d*\s*\(.*\)").unwrap();
if lang.starts_with("Perl6") {
"Raku".to_string()
} else {
re.replace(lang, "").to_string()
for (beginning, simplified) in MAPPING {
if lang.starts_with(beginning) {
return simplified.to_string();
}
}

let simplified = lang
.chars()
.take_while(|&c| !c.is_numeric() && c != '(' && c != '-')
.collect::<String>();

match simplified.len() {
0 => lang.to_string(),
_ => simplified.trim().to_string(),
}
}

Expand All @@ -207,12 +227,31 @@ mod tests {

#[test]
fn test_simplify_language() {
assert_eq!(simplify_language("language1"), "language1");
assert_eq!(simplify_language("language1"), "language");
assert_eq!(simplify_language("Perl (5)"), "Perl");
assert_eq!(simplify_language("Perl6"), "Raku");
assert_eq!(simplify_language("Fortran(GNU Fortran 9.2.1)"), "Fortran");
assert_eq!(simplify_language("Ada2012 (GNAT 9.2.1)"), "Ada");
assert_eq!(simplify_language("PyPy2 (7.3.0)"), "PyPy");
assert_eq!(simplify_language("Haxe (4.0.3); js"), "Haxe; js");
assert_eq!(simplify_language("Haxe (4.0.3); js"), "Haxe");
assert_eq!(simplify_language("C++11 (Clang++ 3.4)"), "C++");
assert_eq!(simplify_language("C++ 20 (gcc 12.2)"), "C++");
assert_eq!(simplify_language("C# 11.0 (.NET 7.0.7)"), "C#");
assert_eq!(simplify_language("C# 11.0 AOT (.NET 7.0.7)"), "C#");
assert_eq!(simplify_language("Visual Basic 16.9 (...)"), "Visual Basic");
assert_eq!(simplify_language("><> (fishr 0.1.0)"), "><>");
assert_eq!(simplify_language("プロデル (...)"), "プロデル");

// mapped individually
assert_eq!(simplify_language("Assembly x64"), "Assembly x64");
assert_eq!(simplify_language("Awk (GNU Awk 4.1.4)"), "AWK");
assert_eq!(simplify_language("IOI-Style C++ (GCC 5.4.1)"), "C++");
assert_eq!(simplify_language("LuaJIT (2.0.4)"), "Lua");
assert_eq!(simplify_language("Objective-C (Clang3.8.0)"), "Objective-C");
assert_eq!(simplify_language("PyPy2 (7.3.0)"), "Python");
assert_eq!(simplify_language("Python (Cython 0.29.34)"), "Cython");
assert_eq!(simplify_language("Cython (0.29.16)"), "Cython");
assert_eq!(simplify_language("Seed7 (Seed7 3.2.1)"), "Seed7");

assert_eq!(simplify_language("1234"), "1234");
}
}
42 changes: 21 additions & 21 deletions atcoder-problems-backend/sql-client/tests/test_language_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ async fn test_language_count() {
id: 1,
problem_id: "problem1".to_owned(),
user_id: "user1".to_owned(),
language: "language1".to_owned(),
language: "language x".to_owned(),
..Default::default()
},
Submission {
id: 2,
problem_id: "problem2".to_owned(),
user_id: "user1".to_owned(),
language: "language1".to_owned(),
language: "language x".to_owned(),
..Default::default()
},
Submission {
id: 3,
problem_id: "problem1".to_owned(),
user_id: "user1".to_owned(),
language: "language1".to_owned(),
language: "language x".to_owned(),
..Default::default()
},
Submission {
id: 4,
problem_id: "problem1".to_owned(),
user_id: "user1".to_owned(),
language: "language2".to_owned(),
language: "language y".to_owned(),
..Default::default()
},
Submission {
id: 5,
problem_id: "problem1".to_owned(),
user_id: "user2".to_owned(),
language: "language1".to_owned(),
language: "language x".to_owned(),
..Default::default()
},
Submission {
Expand All @@ -65,17 +65,17 @@ async fn test_language_count() {
vec![
UserLanguageCount {
user_id: "user1".to_owned(),
simplified_language: "language1".to_owned(),
simplified_language: "language x".to_owned(),
problem_count: 2
},
UserLanguageCount {
user_id: "user1".to_owned(),
simplified_language: "language2".to_owned(),
simplified_language: "language y".to_owned(),
problem_count: 1
},
UserLanguageCount {
user_id: "user2".to_owned(),
simplified_language: "language1".to_owned(),
simplified_language: "language x".to_owned(),
problem_count: 1
},
UserLanguageCount {
Expand Down Expand Up @@ -106,17 +106,17 @@ async fn test_language_count() {
vec![
UserLanguageCount {
user_id: "user1".to_owned(),
simplified_language: "language1".to_owned(),
simplified_language: "language x".to_owned(),
problem_count: 2
},
UserLanguageCount {
user_id: "user1".to_owned(),
simplified_language: "language2".to_owned(),
simplified_language: "language y".to_owned(),
problem_count: 1
},
UserLanguageCount {
user_id: "user2".to_owned(),
simplified_language: "language1".to_owned(),
simplified_language: "language x".to_owned(),
problem_count: 1
},
UserLanguageCount {
Expand All @@ -133,7 +133,7 @@ async fn test_language_count() {
);

let language_count_1st_to_2nd = pool
.load_language_count_in_range("language1", 0..2)
.load_language_count_in_range("language x", 0..2)
.await
.unwrap();
assert_eq!(
Expand All @@ -151,7 +151,7 @@ async fn test_language_count() {
);

let language_count_2nd_to_2nd = pool
.load_language_count_in_range("language1", 1..2)
.load_language_count_in_range("language x", 1..2)
.await
.unwrap();
assert_eq!(
Expand All @@ -163,7 +163,7 @@ async fn test_language_count() {
);

assert_eq!(
pool.load_language_count_in_range("language1", 0..10)
pool.load_language_count_in_range("language x", 0..10)
.await
.unwrap()
.len(),
Expand All @@ -176,12 +176,12 @@ async fn test_language_count() {
vec![
UserLanguageCount {
user_id: "user1".to_owned(),
simplified_language: "language1".to_owned(),
simplified_language: "language x".to_owned(),
problem_count: 2,
},
UserLanguageCount {
user_id: "user1".to_owned(),
simplified_language: "language2".to_owned(),
simplified_language: "language y".to_owned(),
problem_count: 1,
},
]
Expand All @@ -192,12 +192,12 @@ async fn test_language_count() {
vec![
UserLanguageCountRank {
user_id: "user1".to_owned(),
simplified_language: "language1".to_owned(),
simplified_language: "language x".to_owned(),
rank: 1,
},
UserLanguageCountRank {
user_id: "user1".to_owned(),
simplified_language: "language2".to_owned(),
simplified_language: "language y".to_owned(),
rank: 1,
},
]
Expand All @@ -208,7 +208,7 @@ async fn test_language_count() {
language_count,
vec![UserLanguageCount {
user_id: "user2".to_owned(),
simplified_language: "language1".to_owned(),
simplified_language: "language x".to_owned(),
problem_count: 1,
},]
);
Expand All @@ -217,7 +217,7 @@ async fn test_language_count() {
language_count_rank,
vec![UserLanguageCountRank {
user_id: "user2".to_owned(),
simplified_language: "language1".to_owned(),
simplified_language: "language x".to_owned(),
rank: 2,
},]
);
Expand Down Expand Up @@ -255,5 +255,5 @@ async fn test_language_count() {
]
);
let languages = pool.load_languages().await.unwrap();
assert_eq!(languages, ["language1", "language2", "Perl", "Raku"]);
assert_eq!(languages, ["language x", "language y", "Perl", "Raku"]);
}
4 changes: 2 additions & 2 deletions atcoder-problems-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"format": "yarn run sort-package-json && yarn run prettier && yarn run lint:fix",
"lint": "eslint --ignore-path .gitignore \"./src/**/*.{js,jsx,ts,tsx}\"",
"lint:fix": "yarn run lint --fix",
"md:serve": "mdbook serve ../guide/",
"md:clean": "mdbook clean ../guide/ -d=../atcoder-problems-frontend/build/book/",
"md:build": "mkdir -p build/book && yarn run md:clean && mdbook build ../guide/ -d=../atcoder-problems-frontend/build/book/",
"md:clean": "mdbook clean ../guide/ -d=../atcoder-problems-frontend/build/book/",
"md:serve": "mdbook serve ../guide/",
"prepare-ci": "rm -rf serve && mkdir serve && cp -r build serve/atcoder",
"prettier": "prettier --write --ignore-path .gitignore \"**/*.{css,scss,html,js,json,jsx,md,ts,tsx}\"",
"sort-package-json": "sort-package-json",
Expand Down
22 changes: 22 additions & 0 deletions atcoder-problems-frontend/src/utils/LanguageNormalizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,26 @@ test("normalize language", () => {
expect(normalizeLanguage("C++11 (Clang++ 3.4)")).toBe("C++");
expect(normalizeLanguage("Scala (2.11.7)")).toBe("Scala");
expect(normalizeLanguage("Fortran(GNU Fortran 9.2.1)")).toBe("Fortran");
expect(normalizeLanguage("Ada2012 (GNAT 9.2.1)")).toBe("Ada");
expect(normalizeLanguage("Haxe (4.0.3); js")).toBe("Haxe");
expect(normalizeLanguage("C++11 (Clang++ 3.4)")).toBe("C++");
expect(normalizeLanguage("C++ 20 (gcc 12.2)")).toBe("C++");
expect(normalizeLanguage("C# 11.0 (.NET 7.0.7)")).toBe("C#");
expect(normalizeLanguage("C# 11.0 AOT (.NET 7.0.7)")).toBe("C#");
expect(normalizeLanguage("Visual Basic 16.9 (...)")).toBe("Visual Basic");
expect(normalizeLanguage("><> (fishr 0.1.0)")).toBe("><>");
expect(normalizeLanguage("プロデル (...)")).toBe("プロデル");

// mapped individually
expect(normalizeLanguage("Assembly x64")).toBe("Assembly x64");
expect(normalizeLanguage("Awk (GNU Awk 4.1.4)")).toBe("AWK");
expect(normalizeLanguage("IOI-Style C++ (GCC 5.4.1)")).toBe("C++");
expect(normalizeLanguage("LuaJIT (2.0.4)")).toBe("Lua");
expect(normalizeLanguage("Objective-C (Clang3.8.0)")).toBe("Objective-C");
expect(normalizeLanguage("PyPy2 (7.3.0)")).toBe("Python");
expect(normalizeLanguage("Python (Cython 0.29.34)")).toBe("Cython");
expect(normalizeLanguage("Cython (0.29.16)")).toBe("Cython");
expect(normalizeLanguage("Seed7 (Seed7 3.2.1)")).toBe("Seed7");

expect(normalizeLanguage("1234")).toBe("Unknown");
});
22 changes: 18 additions & 4 deletions atcoder-problems-frontend/src/utils/LanguageNormalizer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
const mapping: [string, string][] = [
["PyPy", "Python"],
["Python (Cython", "Cython"],
["Assembly x64", "Assembly x64"],
["Awk", "AWK"],
["IOI-Style", "C++"],
["LuaJIT", "Lua"],
["Seed7", "Seed7"],
["Perl6", "Raku"],
["Objective-C", "Objective-C"],
];

export const normalizeLanguage = (language: string): string => {
if (language.startsWith("Perl6")) {
return "Raku";
} else {
return language.replace(/\d*\s*\(.*\)$/, "");
for (const [beginning, normalized] of mapping) {
if (language.startsWith(beginning)) {
return normalized;
}
}

return language.replace(/\s*[\d(-].*/, "") || "Unknown";
};