#!./bin/python

#
# jojinator to mimic becca's behavior
# by jtnimoy aka ojijo
# aug 2013
#

import nltk,string
from curses.ascii import isdigit

cmudict = nltk.corpus.cmudict.dict() 
def nsyl(word):
  try:
    return [len(list(y for y in x if isdigit(y[-1]))) for x in cmudict[word.lower()]]
  except KeyError:
	return False

jiCount = 0

def jojinate(sentence):
  jlist = []
  tokens = nltk.word_tokenize(sentence)
  tagged = nltk.pos_tag(tokens)
  entities = nltk.chunk.ne_chunk( filter( lambda a: a != (',',',') , tagged) )
  jojiCount = 0

  #print entities
  #print ''
  wordCount = 0
  
  #break words up into 4/4 count phrases
  counts = [[]]
  for i in tagged:
	if i[0] == ',':
	  counts += [[]]
	else:
	  counts[-1] += [i]

  time = 0
  for i in counts:
	wordCount = 0
	joCount = 0

	for j in i:
	  n = nsyl(j[0])
	  if n == False:
		jlist += [j[0]]
	  elif j[1] == 'NN' or j[1]=='VBG' or time % 4 == 2 or len(counts)-1 == time:
		if n[-1] == 1:
		  if joCount % 4 == 0:
			jlist += ['jo']
		  elif joCount % 4 == 1:
			jlist += ['ji']
		  elif joCount % 4 == 2:
			jlist += ['jo']
		  else:
			jlist += ['ji']
		  joCount += 1
		elif n[-1] == 2:
		  if jojiCount % 2 == 0:
			jlist += ['oji']
		  else:
			jlist += ['joji']
		  joCount += 2
		  jojiCount += 1

		elif n[-1] == 3:
		  jlist += ['ojijo']
		  joCount += 3
		elif n[-1] == 4:
		  jlist += ['ojijoji']
		  joCount += 4

		  
	  else:
		jlist += [j[0]]
	  wordCount += 1
	time += 1

  #post process
  newlist = []
  joCount = 0
  prev = ''
  wordCount = 0
  for i in jlist:

	if wordCount < len(jlist)-1:
	  nxt = jlist[wordCount+1]
	else:
	  nxt = ''

	n = nsyl(i)
	if i == 'jo' or i == 'ji':
	  if wordCount == len(jlist)-1:
		global jiCount
		if jiCount % 2 == 1 :
		  newlist += ['ji'] #unconditional ji conversion at end
		else:
		  newlist += ['jyo']
		jiCount += 1
	  elif joCount == 0 and prev != 'oji' and prev != 'joji' and prev != 'ojijo' and nxt == 'ji':
		newlist += ['o']
	  else:
		newlist += [i]
	  joCount += 1
	elif n != False and n[-1] == 1 and prev == 'o' and nsyl(nxt) == False:
	  newlist += ['ji']
	else:
	  joCount = 0
	  if i == 'a' and (nxt == 'oji' or nxt == 'ojijo' or nxt == 'ojijoji'):
		newlist += ['an']
	  else:
		newlist += [i]

	prev = newlist[-1]
	wordCount += 1

  #cluster jojitoms to make bigger jojicules
  for j in range(0,10):
	list2 = []
	list2counter = 0
	for i in newlist:
	  if list2counter > 0:
		prev = newlist[list2counter-1]
	  else:
		prev = ''
	  if prev == 'oji' and i == 'jo':
		list2[-1] += 'jo'
	  elif prev == 'o' and i == 'ji':
		list2[-1] += 'ji'
	  elif prev == 'o' and i == 'jiji':
		list2[-1] += 'jiji'
	  elif prev == 'o' and i == 'jijo':
		list2[-1] += 'jijo'
	  elif prev == 'jo' and i == 'ji':
		list2[-1] += 'ji'
	  elif prev == 'oji' and i == 'ji':
		list2[-1] += 'ji'
	  elif prev == 'ojijo' and i == 'ji':
		list2[-1] += 'ji'
	  elif prev == 'jo' and i == 'oji':
		list2[-1] = 'ojijo'
	  else:
		list2 += [i]
		list2counter += 1
	newlist = list2

  # oji joji alternation
  list2 = []
  jojiCount = 0
  for i in newlist:
	if i == 'oji' or i == 'joji':
	  if jojiCount % 2 == 1:
		list2 += ['joji']
	  else:
		list2 += ['oji']
	  jojiCount += 1
	else:
	  list2 += [i]
	  
	  

  return string.join(list2,' ')

print jojinate("we dont, need no, education")
print jojinate(",hey, teachers, leave, those kids a , lone ! ,,,,, all in all, its just a, nother brick, in the wall")

print jojinate("mister, sandman ! ,,, give, me a, treat")
print jojinate("will, you still , need me, will, you, still, feed me, when, im, sixty, four")
print jojinate("strangers, in the night,, exchanging glances, wondering, in the night,, what were the chances, wed, be sharing love,, before the night, was through")
print jojinate("youve lost, that, loving, feeling")
print jojinate(",i was, working, as a, waitress, in a, cocktail, ,bar , , ,that, much, is , true")
print jojinate(", our, house, , , in the, middle, of our, street")
print jojinate(",I want to, fuck you, like an, animal")
print jojinate("billie, jean, is, not my, lover,,, shes just, a girl, who, claims that,, i, am the, one,,but the kid, is not, my son")
print jojinate("are, you going, to scarborough, fair,,, parsley sage, rosemary, and thyme")
print jojinate(",,,God bless, you please, mrs, robin , son, heaven, holds, a place, for those, who pray,,, hey, hey hey")

print jojinate("you, must be, my lucky, star,,,, cause, you shine, on, me, where ever, you are")
print jojinate(",thats me, in the corner,,, thats me, in the spot , light, losing, my religion")
print jojinate("bye, bye, miss american, pie, drove my chevy, to the levi, but the levi, was dry, the good, old boys, drinking whisky, and rye, singing this, will be the day, that i die")
print jojinate("here in, my car, i feel, safest, of, all, i can, lock, all my, doors, its, the only, way to, live, in, cars")

print jojinate("R E, S P, E C T, find out, what it, means to me")
print jojinate(",You are, the apple, of, my eye,,,,, thats why, ill always, be, around")
print jojinate(", you can stand under my umberella, ella, ella, eh, eh ")
print jojinate(", dude looks like a, lady")
print jojinate("ragdoll, living, in a movie")

print jojinate("its close, to mid, night, and something, evils lurking, from the dark")
print jojinate(",You would, not believe, your eyes,, if ten, thousand fi yer, flies,, lit up, the world, as i fell, asleep")
print jojinate("brass monkey, that chunky monkey, brass monkey, funky that funky monkey")
print jojinate("Been there, done that, messed a, round I, having, fun dont, put me, down")
print jojinate("Ill never, let you, sweep me, off my, feet")
print jojinate("This time, baby, Ill be, bullet ,, proof")
print jojinate("This time, baby, Ill be, bullet ,, proof")