code: def main(args):
      
      import sys
      import json
  
      for line in sys.stdin:
          line = line.rstrip()
  
          # Constraints are in fields 2+
          source, *restrictions = line.split('\t')
  
          obj = {'text': source}
          constraints = []
          avoid_list = []
          for item in restrictions:
              if args.avoid:
                  avoid_list.append(item)
              else:
                  constraints.append(item)
  
          if constraints:
              obj['constraints'] = constraints
          if avoid_list:
              obj['avoid'] = avoid_list
  
          print(json.dumps(obj, ensure_ascii=False), flush=True)
docs: Usage: python3 -m sockeye.lexical_constraints [--bpe BPE_MODEL]
  
      Reads sentences and constraints on STDIN (tab-delimited) and generates the JSON format
      that can be used when passing `--json-input` to sockeye.translate. It supports both positive
      constraints (phrases that must appear in the output) and negative constraints (phrases that
      must *not* appear in the output).
  
      e.g.,
  
          echo -e "Das ist ein Test .\tThis is\ttest" | python3 -m sockeye.lexical_constraints
  
      will produce the following JSON object:
  
          { "text": "Das ist ein Test .", "constraints": ["This is", "test"] }
  
      If you pass `--avoid` to the script, the constraints will be generated as negative constraints, instead:
  
          echo -e "Das ist ein Test .\tThis is\ttest" | python3 -m sockeye.lexical_constraints --avoid
  
      will produce the following JSON object (note the new keyword):
  
          { "text": "Das ist ein Test .", "avoid": ["This is", "test"] }
  
      Make sure you apply all preprocessing (tokenization, BPE, etc.) to both the source and the target-side constraints.
      You can then translate this object by passing it to Sockeye on STDIN as follows:
  
          python3 -m sockeye.translate -m /path/to/model --json-input --beam-size 20 --beam-prune 20
  
      Note the recommended Sockeye parameters. Beam pruning isn't needed for negative constraints.