HITOMIi23

Untitled

Feb 4th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. import sys
  2. import argparse
  3. import numpy as np
  4. from pyspark import SparkContext
  5. import csv
  6.  
  7. def toLowerCase(s):
  8.     """ Convert a sting to lowercase. E.g., 'BaNaNa' becomes 'banana'
  9.    """
  10.     return s.lower()
  11.  
  12. def stripNonAlpha(s):
  13.     """ Remove non alphabetic characters. E.g. 'B:a,n+a1n$a' becomes 'Banana' """
  14.     return ''.join([c for c in s if c.isalpha()])
  15.  
  16.  
  17. if __name__ == "__main__":
  18.     parser = argparse.ArgumentParser(description = 'Text Analysis through TFIDF computation',formatter_class=argparse.ArgumentDefaultsHelpFormatter)    
  19.     parser.add_argument('mode', help='Mode of operation',choices=['TF','IDF','TFIDF','SIM','TOP'])
  20.     parser.add_argument('input', help='Input file or list of files.')
  21.     parser.add_argument('output', help='File in which output is stored')
  22.     parser.add_argument('--master',default="local[20]",help="Spark Master")
  23.     parser.add_argument('--idfvalues',type=str,default="idf", help='File/directory containing IDF values. Used in TFIDF mode to compute TFIDF')
  24.     parser.add_argument('--other',type=str,help = 'Score to which input score is to be compared. Used in SIM mode')
  25.     args = parser.parse_args()
  26.  
  27.     sc = SparkContext(args.master, 'Text Analysis')
  28.     print "Hello there"
  29.  
  30.     if args.mode=='TF':
  31.         # Read text file at args.input, compute TF of each term,
  32.         # and store result in file args.output. All terms are first converted to
  33.         # lowercase, and have non alphabetic characters removed
  34.         # (i.e., 'Ba,Na:Na.123' and 'banana' count as the same term). Empty strings, i.e., ""
  35.         # are also removed
  36.         # pass
  37.         # rdd = sc.parallelize()
  38.         rdd1 = sc.textFile(args.input)
  39.         rdd1.flatMap(lambda s: s.split()).map(toLowerCase).map(stripNonAlpha)\
  40.            .map(lambda word: (word, 1)).reduceByKey(lambda x, y: x + y)\
  41.            .sortBy(lambda (x,y):y, ascending=False).saveAsTextFile(args.output)
  42.          
  43.        
  44.  
  45.     if args.mode=='TOP':
  46.         # Read file at args.input, comprizing strings representing pairs of the form (TERM,VAL),
  47.         # where TERM is a string and VAL is a numeric value. Find the pairs with the top 20 values,
  48.         # and store result in args.output
  49.         rdd2 = sc.textFile(args.input)
  50.        
  51.         rdd2.flatMap(lambda s: s.split()).map(toLowerCase).map(stripNonAlpha)\
  52.            .map(lambda word: (word, 1)).reduceByKey(lambda x, y: x + y)\
  53.            .takeOrdered(20,lambda (x,y):-y)#.saveAsTextFile(args.output)
  54.            #.sortBy(lambda (x,y):y, ascending=False).take(20)
  55.         #rdd2.saveAsTextFile(args.output)
  56.         svFile = sc.parallelize(rdd2)
  57.         svFile.collect().saveAsTextFile(args.output)
  58.  
  59.  
  60.  
  61.  
  62.        
  63.     if args.mode=='IDF':
  64.         # Read list of files from args.input, compute IDF of each term,
  65.         # and store result in file args.output.  All terms are first converted to
  66.         # lowercase, and have non alphabetic characters removed
  67.         # (i.e., 'Ba,Na:Na.123' and 'banana' count as the same term). Empty strings ""
  68.         # are removed
  69.         pass
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.     if args.mode=='TFIDF':
  77.         # Read  TF scores from file args.input the IDF scores from file args.idfvalues,
  78.         # compute TFIDF score, and store it in file args.output. Both input files contain
  79.         # strings representing pairs of the form (TERM,VAL),
  80.         # where TERM is a lowercase letter-only string and VAL is a numeric value.
  81.         pass
  82.  
  83.  
  84.  
  85.  
  86.  
  87.        
  88.     if args.mode=='SIM':
  89.         # Read  scores from file args.input the scores from file args.other,
  90.         # compute the cosine similarity between them, and store it in file args.output. Both input files contain
  91.         # strings representing pairs of the form (TERM,VAL),
  92.         # where TERM is a lowercase, letter-only string and VAL is a numeric value.
  93.         pass
Advertisement
Add Comment
Please, Sign In to add comment