Guest User

Untitled

a guest
Jun 6th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 2.33 KB | None | 0 0
  1. import httpclient, math, tables, strutils, queues, sequtils, os
  2.  
  3.  
  4. randomize(100) # To know for sure
  5.  
  6. var wordTable = initTable[int, string]()
  7.  
  8. var wordCount = 0
  9. for line in "/usr/share/dict/words".lines:
  10.         wordTable[wordCount] = line
  11.         wordCount = wordCount + 1
  12.  
  13.  
  14. proc getRandomString(wordTable: Table[int,string], countOfWordsInTable: int) :  tuple[comment:string,tags:string] =
  15.         var words = initQueue[string]()
  16.         var tags = initQueue[string]()
  17.         var numberOfWords = random(10) + 3
  18.         for i in (0..numberOfWords-1):
  19.                 var r = random(countOfWordsInTable)
  20.                 var word = wordTable[r]
  21.                 words.add(word)
  22.                 if i < 3:
  23.                         tags.add("\"$1\"" % word)
  24.         var c = join(toSeq(words.items), " ")
  25.         var t = join(toSeq(tags.items),",")
  26.         (c, t)
  27.  
  28.  
  29. proc getPostBody(comment:string, sentiment:string, date:string, tags: string): string=
  30.         """{ "comment" : "$1", "sentiment": "$2", "date": "$3", "tag": [$4] }""" % [comment, sentiment, date, tags]
  31.  
  32.  
  33. proc upload(body:string): string=
  34.         try:
  35.                 postContent(url="http://localhost:9200/feedback/comment/", body=body)
  36.         except:
  37.                echo "ERROR CAUSED"
  38.                echo body
  39.                sleep(5000)
  40.                postContent("http://localhost:9200/feedback/comment/", body=body)
  41.  
  42.  
  43. proc getSentiment(month:string):string=
  44.         var r = random(10)
  45.         var dividor = 7
  46.         if month == "01" or month == "06":
  47.                 dividor = 3
  48.         if r < dividor:
  49.                 "neg"
  50.         else:
  51.                 "pos"
  52.  
  53.  
  54.  
  55. proc getDate(): tuple[day: string, month: string, year: string]=
  56.         var id = random(28) + 1
  57.         var idf = if id < 10: "0$1" else: "$1"
  58.         var d = format (idf, id)
  59.         var im = random(12) + 1
  60.         var imf = if im < 10: "0$1" else: "$1"
  61.         var m = format(imf,im)
  62.         var y = "2014"
  63.  
  64.         (d, m, y)
  65.  
  66.  
  67.  
  68. for i in (1..20000):
  69.         var (d,m,y)=getDate()
  70.         var date="$1/$2/$3" % [y,m,d]
  71.         var sentiment=getSentiment(m)
  72.         var (comment,tags) = getRandomString(wordTable, wordCount)
  73.  
  74.         var body=getPostBody(comment, sentiment, date, tags)
  75.         var result=upload(body)
  76.         echo body
  77.         echo result
  78.         echo "--------"
Advertisement
Add Comment
Please, Sign In to add comment