eland
eland copied to clipboard
Can't instantiate a LTRModelConfig from JSON (e.g. via exported LTRModelConfig.to_dict())
The LTRModelConfig class provides the to_dict() method as easy way to export the object as a JSON:
with open(<filepath>, "w") as f:
json.dump(ltr_config.to_dict(), f, indent=4)
The exported JSON can be checked into version control and referenced later.
But the class doesn't support a way to instantiate a new LTRModelConfig object from an exported JSON:
with open(<filepath>, "r") as f:
config_dict = json.load(f)
def load_ltr_config(config_dict: dict) -> LTRModelConfig:
feature_extractors = []
for q in config_dict.get("learning_to_rank", {}).get("feature_extractors", {}):
q = q.get("query_extractor", {})
feature_name = q.get("feature_name")
query = q.get("query")
if feature_name and query:
feature_extractors.append(
QueryFeatureExtractor(feature_name=feature_name, query=query)
)
return LTRModelConfig(feature_extractors=feature_extractors)
A new method from_dict() would allow users to more easily import a LTR model config from JSON:
@classmethod
def from_dict(cls, d: Mapping[str, Any]) -> "LTRModelConfig":
if TYPE_LEARNING_TO_RANK not in d:
raise ValueError(
f"Invalid LTR model config, missing '{TYPE_LEARNING_TO_RANK}' key"
)
feature_extractors = []
for feature_extractor in d[TYPE_LEARNING_TO_RANK]["feature_extractors"]:
if "query_extractor" in feature_extractor:
fe = feature_extractor["query_extractor"]
feature_extractors.append(
QueryFeatureExtractor(
feature_name=fe["feature_name"],
query=fe["query"],
default_score=fe.get("default_score"),
)
)
else:
raise ValueError(
f"Unknown feature extractor type: {list(feature_extractor.keys())}"
)
return cls(feature_extractors=feature_extractors)