<reponame>gustavo-momente/word2vec_variations<gh_stars>1-10
#!/usr/bin/python

__author__ = 'vilelag'

import sys
import argparse
import numpy as np


def create_parsers():
    #parser for the main program
    parser = argparse.ArgumentParser(description='Print all the n classes with most elements, only takes into account'
                                                 'words in the vocabulary')
    parser.add_argument('-v', '-vocab', metavar='<file>', required=True,
                        help='Vocabulary file generated by word2vec after analysing a corpus')
    parser.add_argument('-c', '-class', metavar='<file>', required=True,
                        help='File with information about which classes a word belongs, this file is the one generated'
                             ' by ../create_class.py')
    parser.add_argument('-n', '-number', metavar='<int>', default=20, type=int,
                        help='Number of classes to print, (default: 20)')
    return parser


def read_vocab(fin):
    with open(fin, 'r') as f:
        content = f.read().splitlines()
    words = dict()
    for line in content:
        tmp = line.split(' ')
        words[tmp[0].upper()] = int(tmp[1])
    try:
        words['</s>'] = sys.maxint
    except KeyError:
        pass

    words = sorted(words, key=words.__getitem__, reverse=True)
    return words


def read_class(fin, vocab):
    with open(fin, 'r') as f:
        content = f.read().splitlines()
    classes = dict()
    word_inclusion = dict()

    for line in content:
        tmp = line.split(' ')
        if tmp[0].upper() not in vocab:
            continue
        word_inclusion[tmp[0]] = tmp[1:]
        for _class in tmp[1:]:
            try:
                classes[_class] += 1
            except KeyError:
                classes[_class] = 1

    classes = sorted(classes, key=classes.__getitem__, reverse=True)
    # classes contains all the classes, the first is the class with most occurrences and the last with the least
    return classes, word_inclusion


def main():
    parser = create_parsers()
    args = vars(parser.parse_args())
    vocab = args['v']
    _class = args['c']
    num = args['n']

    vocab = read_vocab(vocab)
    classes, wi = read_class(_class, vocab)
    print len(wi)
    if num > len(classes):
        num = len(classes)

    for i in xrange(num):
        print classes[i]


main()
#!/usr/bin/python

__author__ = 'vilelag'

import sys
import argparse
import numpy as np


def create_parsers():
    #parser for the main program
    parser = argparse.ArgumentParser(description='Print all the n classes with most elements, only takes into account'
                                                 'words in the vocabulary')
    parser.add_argument('-v', '-vocab', metavar='<file>', required=True,
                        help='Vocabulary file generated by word2vec after analysing a corpus')
    parser.add_argument('-c', '-class', metavar='<file>', required=True,
                        help='File with information about which classes a word belongs, this file is the one generated'
                             ' by ../create_class.py')
    parser.add_argument('-n', '-number', metavar='<int>', default=20, type=int,
                        help='Number of classes to print, (default: 20)')
    return parser


def read_vocab(fin):
    with open(fin, 'r') as f:
        content = f.read().splitlines()
    words = dict()
    for line in content:
        tmp = line.split(' ')
        words[tmp[0].upper()] = int(tmp[1])
    try:
        words['</s>'] = sys.maxint
    except KeyError:
        pass

    words = sorted(words, key=words.__getitem__, reverse=True)
    return words


def read_class(fin, vocab):
    with open(fin, 'r') as f:
        content = f.read().splitlines()
    classes = dict()
    word_inclusion = dict()

    for line in content:
        tmp = line.split(' ')
        if tmp[0].upper() not in vocab:
            continue
        word_inclusion[tmp[0]] = tmp[1:]
        for _class in tmp[1:]:
            try:
                classes[_class] += 1
            except KeyError:
                classes[_class] = 1

    classes = sorted(classes, key=classes.__getitem__, reverse=True)
    # classes contains all the classes, the first is the class with most occurrences and the last with the least
    return classes, word_inclusion


def main():
    parser = create_parsers()
    args = vars(parser.parse_args())
    vocab = args['v']
    _class = args['c']
    num = args['n']

    vocab = read_vocab(vocab)
    classes, wi = read_class(_class, vocab)
    print len(wi)
    if num > len(classes):
        num = len(classes)

    for i in xrange(num):
        print classes[i]


main()
#!/usr/bin/python #parser for the main program # classes contains all the classes, the first is the class with most occurrences and the last with the least