-
Notifications
You must be signed in to change notification settings - Fork 386
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
CLDR-17106 add more fixed candidate items #3681
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,49 @@ | ||
package org.unicode.cldr.web; | ||
|
||
import static org.unicode.cldr.web.FixedCandidateProvider.forEnumWithFixedXpath; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.logging.Logger; | ||
import javax.annotation.Nonnull; | ||
import org.unicode.cldr.util.CLDRFile; | ||
import org.unicode.cldr.util.CLDRLocale; | ||
import org.unicode.cldr.util.Factory; | ||
import org.unicode.cldr.util.GrammarInfo; | ||
import org.unicode.cldr.util.GrammarInfo.GrammaticalFeature; | ||
import org.unicode.cldr.util.GrammarInfo.GrammaticalScope; | ||
import org.unicode.cldr.util.GrammarInfo.GrammaticalTarget; | ||
import org.unicode.cldr.util.PathHeader; | ||
import org.unicode.cldr.util.SupplementalDataInfo; | ||
import org.unicode.cldr.util.XMLSource; | ||
import org.unicode.cldr.util.personname.PersonNameFormatter; | ||
import org.unicode.cldr.web.FixedCandidateProvider.PatternCacheCandidateProvider; | ||
|
||
/** Cache for on-disk immutable data. */ | ||
public class DiskDataCache { | ||
static final Logger logger = Logger.getLogger(DiskDataCache.class.getSimpleName()); | ||
|
||
private final Factory factory; | ||
private final CLDRFile english; | ||
private final CLDRFile english; // TODO: Unused? | ||
private final PathHeader.Factory phf; | ||
final SupplementalDataInfo sdi; | ||
|
||
final List<FixedCandidateProvider> personNameProviders = | ||
ImmutableList.of( | ||
forEnumWithFixedXpath( | ||
"//ldml/personNames/parameterDefault[@parameter=\"formality\"]", | ||
PersonNameFormatter.Formality.values()), | ||
forEnumWithFixedXpath( | ||
"//ldml/personNames/parameterDefault[@parameter=\"length\"]", | ||
PersonNameFormatter.Length.values())); | ||
|
||
/** this is the immutable cousin of STFactory.PerLocaleData, for the on-disk data */ | ||
class DiskDataEntry { | ||
|
@@ -27,18 +52,57 @@ class DiskDataEntry { | |
final CLDRFile diskFile; | ||
final Set<String> pathsForFile; | ||
|
||
private final List<FixedCandidateProvider> fixedCandidateProviders = new LinkedList<>(); | ||
|
||
public DiskDataEntry(CLDRLocale locale) { | ||
this.locale = locale; | ||
diskData = factory.makeSource(locale.getBaseName()).freeze(); | ||
diskFile = factory.make(locale.getBaseName(), true).freeze(); | ||
pathsForFile = getPathHeaderFactory().pathsForFile(diskFile); | ||
|
||
addFixedCandidateProviders(); | ||
} | ||
|
||
private void addFixedCandidateProviders() { | ||
// Add all Candidate Providers here | ||
fixedCandidateProviders.add(new GrammarCandidateProvider()); | ||
fixedCandidateProviders.addAll(personNameProviders); | ||
} | ||
|
||
/** Candidate provider for a regex */ | ||
class GrammarCandidateProvider extends PatternCacheCandidateProvider { | ||
final GrammarInfo grammarInfo = sdi.getGrammarInfo(locale.getBaseName()); | ||
|
||
public GrammarCandidateProvider() { | ||
super("^//ldml/units/unitLength.*/unit.*gender"); | ||
} | ||
|
||
@Override | ||
protected Collection<String> getCandidates() { | ||
return grammarInfo.get( | ||
GrammaticalTarget.nominal, | ||
GrammaticalFeature.grammaticalGender, | ||
GrammaticalScope.units); | ||
} | ||
} | ||
Comment on lines
+73
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one is locale dependent depends on the supplementalDataInfo in the enclosing class |
||
|
||
/** | ||
* @returns a list of values (or empty list) of any 'fixed' candidates for this xpath | ||
*/ | ||
Collection<String> getFixedCandidates(final String xpath) { | ||
for (final FixedCandidateProvider fcp : fixedCandidateProviders) { | ||
final Collection<String> r = fcp.apply(xpath); | ||
if (r != null) return r; | ||
} | ||
return Collections.emptySet(); | ||
} | ||
} | ||
|
||
public DiskDataCache(Factory f, CLDRFile english) { | ||
public DiskDataCache(Factory f, CLDRFile english, SupplementalDataInfo sdi) { | ||
this.factory = f; | ||
this.english = english; | ||
this.phf = PathHeader.getFactory(english); | ||
this.sdi = sdi; | ||
} | ||
|
||
public PathHeader.Factory getPathHeaderFactory() { | ||
|
@@ -51,7 +115,7 @@ public PathHeader.Factory getPathHeaderFactory() { | |
new CacheLoader<CLDRLocale, DiskDataEntry>() { | ||
|
||
@Override | ||
public DiskDataEntry load(CLDRLocale l) throws Exception { | ||
public DiskDataEntry load(@Nonnull CLDRLocale l) throws Exception { | ||
return new DiskDataEntry(l); | ||
} | ||
}); | ||
|
84 changes: 84 additions & 0 deletions
84
tools/cldr-apps/src/main/java/org/unicode/cldr/web/FixedCandidateProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.unicode.cldr.web; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import org.unicode.cldr.util.PatternCache; | ||
|
||
abstract class FixedCandidateProvider | ||
implements java.util.function.Function<String, Collection<String>> { | ||
/** | ||
* @returns a list of values (or null if not applicable) of any 'fixed' candidates for this | ||
* XPath | ||
*/ | ||
public abstract Collection<String> apply(String xpath); | ||
|
||
/** helper function to convert an Enum to an array of strings */ | ||
static final <T extends Enum<T>> Collection<String> enumValueStrings(T forValues[]) { | ||
final List<String> l = new ArrayList<String>(forValues.length); | ||
for (final T t : forValues) { | ||
l.add(t.toString()); | ||
} | ||
return ImmutableList.copyOf(l); | ||
} | ||
|
||
/** Candidate provider using a Pattern Cache */ | ||
abstract static class PatternCacheCandidateProvider extends FixedCandidateProvider { | ||
final Pattern pattern; | ||
|
||
public PatternCacheCandidateProvider(String patternString) { | ||
pattern = PatternCache.get(patternString); | ||
} | ||
|
||
public Collection<String> apply(String xpath) { | ||
if (pattern.matcher(xpath).matches()) { | ||
return getCandidates(); | ||
} else { | ||
return null; // not applicable | ||
} | ||
} | ||
|
||
protected abstract Collection<String> getCandidates(); | ||
} | ||
/** Candidate provider for a single string (not regex) */ | ||
abstract static class StringCandidateProvider extends FixedCandidateProvider { | ||
final String pattern; | ||
|
||
public StringCandidateProvider(String xpath) { | ||
pattern = xpath; | ||
} | ||
|
||
public Collection<String> apply(String xpath) { | ||
if (pattern.equals(xpath)) { | ||
return getCandidates(); | ||
} else { | ||
return null; // not applicable | ||
} | ||
} | ||
|
||
protected abstract Collection<String> getCandidates(); | ||
} | ||
|
||
/** create a provider that matches an fixed XPath and returns a set of values */ | ||
public static <T extends Enum<T>> FixedCandidateProvider forEnumWithFixedXpath( | ||
String xpath, T[] values) { | ||
return new EnumStringCandidateProvider( | ||
xpath, FixedCandidateProvider.enumValueStrings(values)); | ||
} | ||
|
||
private static class EnumStringCandidateProvider extends StringCandidateProvider { | ||
private final Collection<String> values; | ||
|
||
@Override | ||
protected Collection<String> getCandidates() { | ||
return values; | ||
} | ||
|
||
EnumStringCandidateProvider(final String xpath, final Collection<String> values) { | ||
super(xpath); | ||
this.values = values; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are not locale dependent, so kept in a separate static list.