damccorm commented on code in PR #38215: URL: https://github.com/apache/beam/pull/38215#discussion_r3154691992
########## sdks/python/apache_beam/examples/ml_transform/mltransform_generate_vocab.py: ########## @@ -0,0 +1,269 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""Batch-only deterministic vocabulary generation pipeline. + +This pipeline creates a deterministic vocabulary artifact from one or more +input columns. + +Key properties: +- Batch only (no streaming path). +- Deterministic ordering: + 1) token frequency descending + 2) token text ascending for tie-breaks +- Reserved OOV token is always written first. +- Output format: one token per line. +""" + +import argparse +import json +import logging +import re +from typing import Any + +import apache_beam as beam +from apache_beam.options.pipeline_options import PipelineOptions + +SUPPORTED_TOKENIZERS = ('whitespace', 'regex') +DEFAULT_REGEX_PATTERN = r"[A-Za-z0-9_]+" + + +def parse_bool_flag(value: str) -> bool: + value_lc = value.strip().lower() + if value_lc in ('1', 'true', 't', 'yes', 'y'): + return True + if value_lc in ('0', 'false', 'f', 'no', 'n'): + return False + raise ValueError( + f'Invalid boolean value {value!r}. Expected true/false style value.') + + +def normalize_text(value: Any, lowercase: bool = True) -> str: + if value is None: + return '' + text = str(value).strip() + if lowercase: + text = text.lower() + return text + + +def tokenize_text( Review Comment: The docs/pr indicates we're using MLTransform, but it looks like all of this is custom DoFns. We should be using something like https://beam.apache.org/documentation/transforms/python/elementwise/mltransform/#example-2 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
