Skip to content
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
40 changes: 40 additions & 0 deletions stationapi/src/domain/ipa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ fn word_to_ipa(token: &str) -> Option<String> {
return Some(String::new());
}

if let Some(ipa) = split_compound_token_to_ipa(&normalized) {
return Some(ipa);
}

if let Some(ipa) = lookup_english_word_ipa(&normalized) {
return Some(ipa.to_string());
}
Expand All @@ -141,6 +145,26 @@ fn word_to_ipa(token: &str) -> Option<String> {
romaji_to_katakana(&normalized).and_then(|katakana| katakana_to_ipa(&katakana))
}

fn split_compound_token_to_ipa(token: &str) -> Option<String> {
const JAPANESE_SUFFIXES: &[&str] = &["kaigan"];

for suffix in JAPANESE_SUFFIXES {
if token.len() <= suffix.len() || !token.ends_with(suffix) {
continue;
}

let stem = &token[..token.len() - suffix.len()];
let stem_ipa = word_to_ipa(stem)?;
let suffix_ipa = word_to_ipa(suffix)?;
if stem_ipa.is_empty() || suffix_ipa.is_empty() {
return None;
}
return Some(format!("{stem_ipa} {suffix_ipa}"));
}

None
}

fn is_name_token_char(c: char) -> bool {
c.is_ascii_alphanumeric()
|| matches!(
Expand Down Expand Up @@ -1254,6 +1278,22 @@ mod tests {
);
}

#[test]
fn test_station_name_ipa_splits_compound_kaigan_suffix() {
assert_eq!(
station_name_to_ipa("イナゲカイガン", Some("Inagekaigan")),
Some("inage ka.igaɴ".to_string())
);
}

#[test]
fn test_station_name_ipa_splits_other_compound_kaigan_suffix() {
assert_eq!(
station_name_to_ipa("オオモリカイガン", Some("Omorikaigan")),
Some("omoɾi ka.igaɴ".to_string())
);
}

#[test]
fn test_station_name_ipa_supports_line_related_english_words() {
assert_eq!(
Expand Down
100 changes: 99 additions & 1 deletion stationapi/src/use_case/dto/station.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ impl From<TransportType> for i32 {
impl From<Station> for GrpcStation {
fn from(station: Station) -> Self {
let name_ipa = katakana_to_ipa(&station.station_name_k).filter(|ipa| !ipa.is_empty());
let name_roman_ipa = station_name_to_ipa("", station.station_name_r.as_deref());
let name_roman_ipa =
station_name_to_ipa(&station.station_name_k, station.station_name_r.as_deref());
Self {
id: station.station_cd as u32,
group_id: station.station_g_cd as u32,
Expand Down Expand Up @@ -53,3 +54,100 @@ impl From<Station> for GrpcStation {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
domain::entity::{gtfs::TransportType, station::Station},
proto::StopCondition,
};

fn create_test_station(name: &str, name_katakana: &str, name_roman: Option<&str>) -> Station {
Station::new(
1,
1,
name.to_string(),
name_katakana.to_string(),
name_roman.map(str::to_string),
None,
None,
vec![],
None,
None,
None,
None,
None,
1,
None,
vec![],
12,
String::new(),
String::new(),
0.0,
0.0,
String::new(),
String::new(),
0,
0,
StopCondition::All,
None,
false,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
TransportType::Rail,
)
}

#[test]
fn test_station_sets_expected_roman_ipa_for_inagekaigan() {
let grpc_station: GrpcStation =
create_test_station("稲毛海岸", "イナゲカイガン", Some("Inagekaigan")).into();

assert_eq!(
grpc_station.name_roman_ipa,
Some("inage ka.igaɴ".to_string())
);
}

#[test]
fn test_station_name_roman_ipa_falls_back_to_katakana() {
let grpc_station: GrpcStation = create_test_station("渋谷", "シブヤ", Some("???")).into();

assert_eq!(grpc_station.name_roman_ipa, Some("ɕibɯja".to_string()));
}
}
6 changes: 4 additions & 2 deletions stationapi/src/use_case/interactor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,10 @@ where

let name_ipa = crate::domain::ipa::katakana_to_ipa(&row.station_name_k)
.filter(|ipa| !ipa.is_empty());
let name_roman_ipa =
crate::domain::ipa::station_name_to_ipa("", row.station_name_r.as_deref());
let name_roman_ipa = crate::domain::ipa::station_name_to_ipa(
&row.station_name_k,
row.station_name_r.as_deref(),
);
proto::StationMinimal {
id: row.station_cd as u32,
group_id: row.station_g_cd as u32,
Expand Down
Loading