#!/usr/bin/env python

# This script extracts symbols which appear at least N times in the
# input and writes them to the standard output

import sys

N=1000
REPLACEMENT="65533"

def main():
    command = sys.argv[1]
    if command == 'make':
        vocab = {}
        for line in sys.stdin:
            for word in line.split():
                vocab[word] = vocab.get(word, 0) + 1 
        for word, count in vocab.items():
            if count >= N:
                print word
    if command == 'replace':
        mode = sys.argv[2]
        vocabfile = sys.argv[3]
        vocab = set(open(vocabfile).read().split())
        def replace(w):
            if w in vocab:
                return w
            else:
                return REPLACEMENT
        def identity(w): return w

        for line in sys.stdin:
            words = [ replace(w) if mode == 'gmb' else identity(w) for w in line.split() ]
            print " ".join(words)


if __name__ == '__main__':
    main()

    
