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

Transformer for RelativeTimeFormat component #2822

Merged
merged 9 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
"experimental/ixdtf",
"experimental/tutorials",
"experimental/displaynames",
"experimental/relativetime",
pdogr marked this conversation as resolved.
Show resolved Hide resolved
"utils/databake",
"utils/databake/derive",
"experimental/segmenter",
Expand Down
3 changes: 2 additions & 1 deletion components/icu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ icu_timezone = { version = "1.0.0", path = "../timezone", default-features = fal
icu_casemapping = { version = "0.7.0", path = "../../experimental/casemapping", default-features = false, optional = true }
icu_segmenter = { version = "0.7.0", path = "../../experimental/segmenter", default-features = false, optional = true }
icu_displaynames = { version = "0.7.0", path = "../../experimental/displaynames", default-features = false, optional = true }
icu_relativetime = { version = "0.1.0", path = "../../experimental/relativetime", default-features = false, optional = true }

# For docs links
icu_provider = { version = "1.0.0", path = "../../provider/core", default-features = false }
Expand Down Expand Up @@ -90,7 +91,7 @@ serde = [
serde_human = [
"icu_list/serde_human"
]
experimental = ["icu_casemapping", "icu_segmenter", "icu_datetime/experimental", "icu_displaynames"]
experimental = ["icu_casemapping", "icu_segmenter", "icu_datetime/experimental", "icu_displaynames", "icu_relativetime"]

[[example]]
name = "tui"
Expand Down
35 changes: 35 additions & 0 deletions experimental/relativetime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This file is part of ICU4X. For terms of use, please see the file
# called LICENSE at the top level of the ICU4X source tree
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

[package]
name = "icu_relativetime"
version = "0.1.0"
authors = ["The ICU4X Project Developers"]
edition = "2018"
repository = "https://github.com/unicode-org/icu4x"
license-file = "LICENSE"
categories = ["internationalization"]
# Keep this in sync with other crates unless there are exceptions
include = [
"data/*",
"src/**/*",
"examples/**/*",
"benches/**/*",
"tests/**/*",
"Cargo.toml",
"LICENSE",
"README.md"
]

[dependencies]
icu_provider = { version = "1.0.0", path = "../../provider/core", features = ["macros"] }
zerovec = { version = "0.9", path = "../../utils/zerovec", features = ["yoke"] }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"], optional = true }
databake = { version = "0.1.0", path = "../../utils/databake", optional = true, features = ["derive"]}

[features]
default = []
std = ["icu_provider/std"]
serde = ["dep:serde", "zerovec/serde"]
datagen = ["std", "serde", "dep:databake", "zerovec/databake"]
51 changes: 51 additions & 0 deletions experimental/relativetime/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
UNICODE, INC. LICENSE AGREEMENT - DATA FILES AND SOFTWARE

See Terms of Use <https://www.unicode.org/copyright.html>
for definitions of Unicode Inc.’s Data Files and Software.

NOTICE TO USER: Carefully read the following legal agreement.
BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.'S
DATA FILES ("DATA FILES"), AND/OR SOFTWARE ("SOFTWARE"),
YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
TERMS AND CONDITIONS OF THIS AGREEMENT.
IF YOU DO NOT AGREE, DO NOT DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE
THE DATA FILES OR SOFTWARE.

COPYRIGHT AND PERMISSION NOTICE

Copyright © 1991-2022 Unicode, Inc. All rights reserved.
Distributed under the Terms of Use in https://www.unicode.org/copyright.html.

Permission is hereby granted, free of charge, to any person obtaining
a copy of the Unicode data files and any associated documentation
(the "Data Files") or Unicode software and any associated documentation
(the "Software") to deal in the Data Files or Software
without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, and/or sell copies of
the Data Files or Software, and to permit persons to whom the Data Files
or Software are furnished to do so, provided that either
(a) this copyright and permission notice appear with all copies
of the Data Files or Software, or
(b) this copyright and permission notice appear in associated
Documentation.

THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT OF THIRD PARTY RIGHTS.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THE DATA FILES OR SOFTWARE.

Except as contained in this notice, the name of a copyright holder
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in these Data Files or Software without prior
written authorization of the copyright holder.


Portions of ICU4X may have been adapted from ICU4C and/or ICU4J.
ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others.
17 changes: 17 additions & 0 deletions experimental/relativetime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# icu_relativetime [![crates.io](https://img.shields.io/crates/v/icu_relativetime)](https://crates.io/crates/icu_relativetime)

🚧 \[Experimental\] Relative time formatting

This module is published as its own crate ([`icu_relativetime`](https://docs.rs/icu_relativetime/latest/icu_relativetime/))
and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.

<div class="stab unstable">
🚧 This code is experimental; it may change at any time, in breaking or non-breaking ways,
including in SemVer minor releases. It can be enabled with the "experimental" feature
of the icu meta-crate. Use with caution.
</div>


## More Information

For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x).
21 changes: 21 additions & 0 deletions experimental/relativetime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! 🚧 \[Experimental\] Relative time formatting
//!
//! This module is published as its own crate ([`icu_relativetime`](https://docs.rs/icu_relativetime/latest/icu_relativetime/))
//! and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.
//!
//! <div class="stab unstable">
//! 🚧 This code is experimental; it may change at any time, in breaking or non-breaking ways,
//! including in SemVer minor releases. It can be enabled with the "experimental" feature
//! of the icu meta-crate. Use with caution.
//! </div>
//!

#![warn(missing_docs)]

extern crate alloc;

pub mod provider;
99 changes: 99 additions & 0 deletions experimental/relativetime/src/provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

// Provider structs must be stable
#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]

//! Data provider struct definitions for this ICU4X component.
//!
//! Read more about data providers: [`icu_provider`]

use alloc::borrow::Cow;
use icu_provider::{yoke, zerofrom};
use zerovec::ZeroMap;

/// Relative time patterns data struct.
#[icu_provider::data_struct(
StandardRelativeTimeV1Marker = "relativetime/standard@1",
pdogr marked this conversation as resolved.
Show resolved Hide resolved
NarrowRelativeTimeV1Marker = "relativetime/narrow@1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought: I'm wondering whether we want to slice this smaller. For example, I know ECMA-402 delays the unit choice to the format function, but should we be making it an option in the constructor instead? And how about the direction (past/future)? We're going to get some pretty big data keys out of this, which begs the question about whether we should be slicing more granularly.

It's OK to make constructors that load multiple data keys for convenience, of course.

One possible axis that is already in ECMA-402 is "numeric". If the user requests numeric: "always", then they shouldn't need to load the non-numeric data. So, we could maybe move that data into a separate data key.

Copy link
Contributor Author

@pdogr pdogr Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data keys are pretty big currently with most of the fields being 'None'.

I'm in favour of splitting it further into at least | "unit" x "width" | data keys. This would remove the need for having an array of "RelativeTimePattern".

Then we can focus on an efficient representation of "PluralRulesCategory" => "Singular substitution pattern" mapping.
Using a "ZeroMap<PluralRulesCategory, SubstitutionPattern>" would be good as we don't store the unnecessary mappings but I haven't been able to make it work.

Copy link
Member

@sffc sffc Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to do unit x width (8*3). What do you think about unit x width x numeric (8*3*2)?

Yes, let's brainstorm the best representation for PluralCategory => Pattern.

There are some advantages to expanding to separate fields instead of using ZeroMap. Note that None fields do not take up much space in Postcard.

ShortRelativeTimeV1Marker = "relativetime/short@1"
)]
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_relativetime::provider)
)]
#[yoke(prove_covariance_manually)]
pub struct RelativeTimePatternsV1<'data>(
#[cfg_attr(feature = "serde", serde(borrow))]
// Contains patterns in order second, minute, hour, day, week, month, quarter, year
pub [RelativeTimePattern<'data>; 8],
pdogr marked this conversation as resolved.
Show resolved Hide resolved
);

impl<'data> RelativeTimePatternsV1<'data> {
/// Create a new RelativeTimePatternsV1
pub fn new(patterns: [RelativeTimePattern<'data>; 8]) -> Self {
Self(patterns)
}
}

/// A single relative time pattern.
#[derive(Debug, Clone, Default, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_relativetime::provider)
)]
#[yoke(prove_covariance_manually)]
pub struct RelativeTimePattern<'data> {
/// The display name of the pattern
pub display_name: Option<Cow<'data, str>>,
pdogr marked this conversation as resolved.
Show resolved Hide resolved
/// Mapping for relative time fields.
#[cfg_attr(feature = "serde", serde(borrow))]
pub relatives: ZeroMap<'data, i8, str>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought: What is the full set of number offsets? In most locales it is -1, 0, 1; there are some that go to -2, -1, 0, 1, 2. This is an okay use of ZeroMap, but if there is never going to be more than -2 to 2, we may want to consider expanding that into the struct itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The offsets are from [-2, 2].

/*
// Plural rules mapping for past.
#[cfg_attr(feature = "serde", serde(borrow))]
pub past_zero: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub past_one: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub past_two: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub past_few: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub past_many: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub past_other: Option<SingularSubPattern<'data>>,
// Plural rules mapping for future.
#[cfg_attr(feature = "serde", serde(borrow))]
pub future_zero: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub future_one: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub future_two: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub future_few: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub future_many: Option<SingularSubPattern<'data>>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub future_other: Option<SingularSubPattern<'data>>,
*/
}

/// Singular substitution pattern string.
#[derive(Debug, Clone, Default, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_relativetime::provider)
)]
pub struct SingularSubPattern<'data> {
pdogr marked this conversation as resolved.
Show resolved Hide resolved
pattern: Cow<'data, str>,
index: u8,
}
3 changes: 2 additions & 1 deletion provider/datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ icu_timezone = { version = "1.0.0", path = "../../components/timezone", features
icu_casemapping = { version = "0.7.0", path = "../../experimental/casemapping", features = ["datagen"], optional = true }
icu_segmenter = { version = "0.7.0", path = "../../experimental/segmenter", features = ["datagen", "lstm"], optional = true }
icu_displaynames = { version = "0.7.0", path = "../../experimental/displaynames", features = ["datagen"], optional = true }
icu_relativetime = { version = "0.1.0", path = "../../experimental/relativetime", features = ["datagen"], optional = true }

# ICU provider infrastructure
icu_provider = { version = "1.0.0", path = "../core", features = ["std", "log_error_context", "datagen"]}
Expand Down Expand Up @@ -95,7 +96,7 @@ icu = { path = "../../components/icu" }

[features]
default = []
experimental = ["icu_casemapping", "icu_segmenter", "icu_datetime/experimental", "icu_displaynames"]
experimental = ["icu_casemapping", "icu_segmenter", "icu_datetime/experimental", "icu_displaynames", "icu_relativetime"]
bin = ["clap", "eyre", "simple_logger"]

[[bin]]
Expand Down
5 changes: 5 additions & 0 deletions provider/datagen/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use icu_casemapping::provider::*;
#[cfg(feature = "experimental")]
use icu_displaynames::provider::*;
#[cfg(feature = "experimental")]
use icu_relativetime::provider::*;
#[cfg(feature = "experimental")]
use icu_segmenter::provider::*;

macro_rules! registry {
Expand Down Expand Up @@ -220,6 +222,9 @@ registry!(
SentenceBreakDataV1Marker,
UCharDictionaryBreakDataV1Marker,
WordBreakDataV1Marker,
StandardRelativeTimeV1Marker,
ShortRelativeTimeV1Marker,
NarrowRelativeTimeV1Marker,
}
);

Expand Down
1 change: 1 addition & 0 deletions provider/datagen/src/transform/cldr/cldr_serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ pub mod numbering_systems;
pub mod numbers;
pub mod parent_locales;
pub mod plurals;
pub mod relativetime;
pub mod time_zones;
pub mod week_data;
Loading