Example #1
0
void mod_huddraw(unsigned int vidfr)
{
        // draw HUD here!

        // popups
        unsigned int vidfrmod = vidfr % maxframes;
        int i;
        for( i=0; i<maxobjs; i++ )
        {
                object *ob = fr[vidfrmod].objs+i;
                if( ob->type != popup_type ) continue;
                popup *pop = ob->data;

                if( pop->visible )
                {
                        V *pos  = flex(ob, pos);
                        V *hull = flex(ob, hull);
                        if( i==gui_hover && pop->enabled )
                        {
                                SJGL_SetTex( 0 );
                                SJGL_SetTex( 2 ); // FIXME: NO NO NO!
                                SJGL_Blit( &(REC){30, 30, hull[1].x, hull[1].y}, pos->x, pos->y, 0 );
                        }
                        drawtext( pos->x, pos->y, FONT_LEFT, "%s", pop->text );
                }
        }
}
Example #2
0
	void FLEX(Machine & machine){
		flex(machine.stack.peek().asNumber());
	}
Example #3
0
int main(int argc, char* argv[]) {
	
	if (argc != 4) {
		std::cout << "Aufruf mit Parametern: <französiche Trainigsdaten> <englische Trainingsdaten> <Alignment der Trainingsdaten>\n"
			<< "Folgende Ausgabe: relfreq_f relfreq_e # quellphrase # zielphrase # singlecf singlece # source_to_target target_to_source # unigram-sprachmodell\n";
		return 0;
	}
	Lexicon flex(french);
	Lexicon elex(english);
	PTree<std::pair<int, PTree<int> > > pTree;
	PTree<unsigned int> eSinglecount;

	uint eCount = 0;				//Gesamtzahl der englischen Wörter

	std::unordered_map<uint,Wordinfo> ef_pair, fe_pair;	//Einzelwortbasierte Übersetzungshäufigkeit von e nach f (und umgekehrt)
	
	igzstream f_in(argv[1]), e_in(argv[2]), a_in(argv[3]);
	std::string f_line, e_line, a_line;

	while (getline(f_in, f_line) && getline(e_in, e_line)) {
		
		/*==========Lies Wörter beider Sätze, sowie zugehörige Alignments aus entsprechenden Dateien aus==========*/
		std::string token;
		std:: istringstream f_ist(f_line), e_ist(e_line);
		std::vector<std::pair<uint, std::vector<unsigned int> > > f_vec, e_vec;	//Speichern alle Wörter jeweiliger Sprache und ihre Alignments
		
		//Füge alle französichen Wörter in ein Lexicon ein und schreibe ihre IDs in einen Vektor
		while(f_ist >> token) {
			uint id = flex.getWord_or_add(token);
			std::pair<uint, std::vector<unsigned int> > pair_tmp;
			pair_tmp.first = id;
			f_vec.push_back(pair_tmp);
		}

		//Füge alle englischen Wörter in ein Lexicon ein und schreibe ihre IDs in einen Vektor
		while (e_ist >> token) {
			uint id = elex.getWord_or_add(token);
			std::pair<uint, std::vector<unsigned int> > pair_tmp;
			pair_tmp.first = id;
			e_vec.push_back(pair_tmp);
			eCount++;
		}
		
		getline(a_in, a_line);	//"SEND:" abfangen
		do {
			getline(a_in, a_line);
			if(a_line == "") break;	//Alignment eine Satzes zu Ende
			else {
				std::istringstream a_ist(a_line);
				int f_ind, e_ind;
				std::string s;
				a_ist >> s >> f_ind >> e_ind;
				f_vec[f_ind].second.push_back(e_ind);	//Speichere einzelnes Alignment in f_vec
				e_vec[e_ind].second.push_back(f_ind);	//Speichere einzelnes Alignment in e_vec

				uint& f_id = f_vec[f_ind].first, e_id = e_vec[e_ind].first;
				fe_pair[f_id].pairs[e_id]++;		//Paircount für f nach e erhöhen
				fe_pair[f_id].singlecount++;		//Singlecount für f nach e erhöhen
				ef_pair[e_id].pairs[f_id]++;		//Paircount für e nach f erhöhen
				ef_pair[e_id].singlecount++;		//Singlecount für e nach f erhöhen
			}
		} while(true);

		/*==========Beide Sätze liegen nun in Vektoren gespeichert vor, die Alignments jedes Wortes sind in einem Vektor gespeichert==========
		 *==========Führe darauf nun den vorgegebenen Algorithmus aus, um alle Phrasen zu finden und im Präfixbaum zu speichern==========*/
		
		for(unsigned int j1 = 0; j1 < f_vec.size(); j1++)
			for(unsigned int j2 = j1; j2 < std::min(j1+3,(unsigned int)f_vec.size()); j2++) {	//Länge der Quellphrase maximal 3
				unsigned int i1, i2;
				bool set_i = false;						//hält mit, ob i1 und i2 gesetzt wurden, oder nicht.
				for(unsigned int k = j1; k <= j2; k++) 
					if(f_vec[k].second.size() && set_i) {
						i1 = std::min(i1, f_vec[k].second.front());	//Minimales Alignment innerhalb der Phrase finden => i1
						i2 = std::max(i2, f_vec[k].second.back());	//Maximales Alignment innerhalb der Phrase finden => i2
					} else if(f_vec[k].second.size() && !(set_i)) {
						i1 = f_vec[k].second.front();
						i2 = f_vec[k].second.back();
						set_i = true;
					}

				if (set_i){ 					//leere Phrasen werden nicht geprüft sondern direkt verworfen
					if(j1 == j2) {					//Einzelwortphrasen auf Quellseite werden IMMER extrahiert
						
						std::vector<uint> f_vec_tmp, e_vec_tmp;
						for (unsigned int k = j1; k <= j2; k++)
							f_vec_tmp.push_back(f_vec[k].first);	//Quellphrase in Vektor zusammenstellen
						for (unsigned int k = i1; k <= i2; k++)
							e_vec_tmp.push_back(e_vec[k].first);	//Zielphrase in Vektor zusammenstellen
						std::pair<int, PTree<int> > pair_tmp;
						pair_tmp.first = 0;
						pTree.traverse(f_vec_tmp,true,pair_tmp)->c.first++;				//Quellphrase in Baum einfügen
						pTree.traverse(f_vec_tmp,false)->c.second.traverse(e_vec_tmp,true,0)->c++;	//Zielphrase in "Unter-Baum" einfügen
						eSinglecount.traverse(e_vec_tmp,true,0)->c++;
					} else if (i2-i1 < 4) {	//Länge der Zielphrase maximal 4

						unsigned int j1_test, j2_test;
						bool set_j_test = false;			//hält mit, ob j1_test und j2_test gesetzt wurden, oder nicht
						for (unsigned int k = i1; k <= i2; k++)
							if (e_vec[k].second.size() && set_j_test) {
								j1_test = std::min(j1_test, e_vec[k].second.front());
								j2_test = std::max(j2_test, e_vec[k].second.back());
							} else if (e_vec[k].second.size() && !(set_j_test)) {
								j1_test = e_vec[k].second.front();
								j2_test = e_vec[k].second.back();
								set_j_test = true;
							}
						
						if (set_j_test)					//leere Phrasen werden nicht geprüft sondern sofort verworfen
						if ((j1_test >= j1) && (j2_test <= j2)) {	//Phrasen, die den Test bestehen, werden extrahiert
							
							std::vector<uint> f_vec_tmp, e_vec_tmp;
							for (unsigned int k = j1; k <= j2; k++)
								f_vec_tmp.push_back(f_vec[k].first);
							for (unsigned int k = i1; k <= i2; k++)
								e_vec_tmp.push_back(e_vec[k].first);
							std::pair<int, PTree<int> > pair_tmp;
							pair_tmp.first = 0;
							pTree.traverse(f_vec_tmp,true,pair_tmp)->c.first++;			//Quellphrase in Baum einfügen
							pTree.traverse(f_vec_tmp,false)->c.second.traverse(e_vec_tmp,true,0)->c++;	//Zielphrase in "Unter-Baum" einfügen
							eSinglecount.traverse(e_vec_tmp,true,0)->c++;
						}
					}
				}
				
			}
			/*==========Jetzt sind alle erlaubten Phrasen aus diesem Satzpaar im Präfixbaum gespeichert==========*/
			/*==========Damit ist die Bearbeitung dieses Satzpaares abgeschlossen und das nächste rückt nach==========*/
	}
	/*==========Nun sind alle erlaubten Phrasen aller Satzpaare - also der gesamten Testdaten - im Präfixbaum gespeichert==========*/
	/*==========Im Anschluss muss also der Präfixbaum in eine Phrasentabelle ausgegeben werden==========*/

	for (PTree<std::pair<int, PTree<int> > >::iterator itor1 = pTree.begin(); itor1 != pTree.end(); itor1++) {	//Durchlaufe den Baum

		int singlecount_f = (&*itor1) -> c.first;		//Zähler für Quellphrase auslesen
		if (singlecount_f) {
			std::vector<uint> source_id = (&*itor1) -> phrase();//Quellphrase (in IDs) auslesen
			std::string source_phrase = "";
			for (unsigned int k = 0; k < source_id.size(); k++)	//ID-Phrase in Stringphrase umwandeln
				source_phrase += flex.getString(source_id[k]) + " ";
			
			for(PTree<int>::iterator itor2 = (&*itor1) -> c.second.begin(); itor2 != (&*itor1) -> c.second.end(); itor2++) {	//Durchlaufe den "Unter-Baum"

				int paircount = (&*itor2) -> c;			//Zähler für Zielphrase auslesen
				if(paircount != 0) {
					std::vector<uint> target_id = (&*itor2) -> phrase();//Zielphrase (in IDs) auslesen
					std::string target_phrase = "";
					for (unsigned int k = 0; k < target_id.size(); k++)	//ID-Phrase in Stringphrase umwandeln
						target_phrase += elex.getString(target_id[k]) + " ";

					double source_to_target = 1;
					for (unsigned int k = 0; k < target_id.size(); k++) {
						double  sum_stt = 0;
						for (unsigned int l = 0; l < source_id.size(); l++) {
							sum_stt += (double) fe_pair[source_id[l]].pairs[target_id[k]] / (double) fe_pair[source_id[l]].singlecount;
						}
						source_to_target *= sum_stt / source_id.size();
					}
					source_to_target = -log(source_to_target);

					double target_to_source = 1;
					for (unsigned int k = 0; k < source_id.size(); k++) {
						double sum_tts = 0;
						for (unsigned int l = 0; l < target_id.size(); l++) {
							sum_tts += (double) ef_pair[target_id[l]].pairs[source_id[k]] /  (double) ef_pair[target_id[l]].singlecount;
						}
						target_to_source *= sum_tts / target_id.size();
					}
					target_to_source = -log(target_to_source);

					uint singlecount_e = eSinglecount.traverse(target_id)->c;
					double relFreqF = log(singlecount_f) - log(paircount);	//Bestimmen der relativen Wahrscheinlichkeit (negativer Logarithmus)
					double relFreqE = log(singlecount_e) - log(paircount);
					
					double unigram = log(eCount) - log(singlecount_e);
					
					std::cout << relFreqF << " " << relFreqE << " # " << source_phrase << "# " << target_phrase << "# " << singlecount_f << " "<< singlecount_e << " # " << source_to_target << " " << target_to_source << " # " << unigram << "\n";	//Ausgabe
				}
			}
		}
	}
	return 0;	
}
Example #4
0
void render()
{
  const SDL_VideoInfo *vidinfo;
  int x,y,w,h;
  int i;
  char buf[1000];
  Uint32 vidfr = (metafr-1);
  Uint32 vidfrmod = vidfr%maxframes;

  Uint32 render_start = SDL_GetTicks();
  static Uint32 total_start = 0;
  Uint32 tmp;

  if( metafr==0 || vidfr<=drawnfr ) //==0 prevent never-draw bug
    return;

  if( soon==1 )
    setvideo(soon_w,soon_h,soon_full,0);
  if( soon>0 )
    soon--;

  vidinfo  = SDL_GetVideoInfo();
  w = v_w  = vidinfo->current_w;
  h = v_h  = vidinfo->current_h;
  pad_left = 0;
  pad_top  = 0;
  if( v_center ) {
    pad_left = (w - NATIVEW*scale)/2;
    pad_top  = (h - NATIVEH*scale)/2;
  }

  glMatrixMode(GL_TEXTURE);
  glLoadIdentity();
  glScalef(1.0f/256.0f, 1.0f/256.0f, 1);

  glColor4f(1.0f,1.0f,1.0f,1.0f);
  glEnable(GL_TEXTURE_2D);
  if( v_usealpha )
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  else
    glBlendFunc(GL_ONE, GL_ZERO);
  glEnable(GL_BLEND);

  glAlphaFunc(GL_GREATER,0.01);
  glEnable(GL_ALPHA_TEST);

  glDepthFunc(GL_LEQUAL);
  glEnable(GL_DEPTH_TEST);

  glClear(GL_DEPTH_BUFFER_BIT);

  // viewport and matrixes for game objects
  glViewport(pad_left,h-NATIVEH*scale-pad_top,NATIVEW*scale,NATIVEH*scale);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,NATIVEW,NATIVEH,0,-NATIVEH*3-1,NATIVEH*3+1);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  int camx = NATIVEW/2-(int)v_camx, camy = NATIVEH/2-(int)v_camy;
  glTranslatef(camx,camy,0);

  SJGL_SetTex( (GLuint)-1 ); //forget previous texture name
  mod_predraw(vidfr);

  //display objects
  for(i=0;i<maxobjs;i++) {
    OBJ_t *o = fr[vidfrmod].objs+i;
    if( o->flags&OBJF_VIS )
      mod_draw(i,o); // have the mod draw the actual thing
  }

  mod_postdraw(vidfr);

  glDisable(GL_DEPTH_TEST);

  //display hulls and object numbers
  if( v_drawhulls ) {
    glBindTexture( GL_TEXTURE_2D, 0 );
    glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );

    if( mycontext ) {
      CONTEXT_t *co = fr[vidfrmod].objs[mycontext].data;
      int x,y,z;
      for( z=0; z<co->z; z++ )
        for( y=0; y<co->y; y++ )
          for( x=0; x<co->x; x++ ) {
            int pos = co->x*co->y*z + co->x*y + x;
            int flags;

            if( co->dmap[ pos ].flags & CBF_NULL )
              flags = co->map[  pos ].flags;
            else
              flags = co->dmap[ pos ].flags;

            if( flags & CBF_SOLID ) {
              glColor4f(1,0,0,1);
              SJGL_Blit( &(SDL_Rect){0,0,16,16}, x*16,   y*16,   z );
              SJGL_Blit( &(SDL_Rect){0,0,12,12}, x*16+2, y*16+2, z );
            } else if( flags & CBF_PLAT ) {
              glColor4f(0,1,0,1);
              SJGL_Blit( &(SDL_Rect){0,0,16, 2}, x*16,   y*16,   z );
            }
          }
    }

    glColor4f(1,1,1,1);
    for(i=0;i<maxobjs;i++) {
      OBJ_t *o = fr[vidfrmod].objs+i;
      V *pos  = flex(o,OBJF_POS);
      V *hull = flex(o,OBJF_HULL);
      if( pos && hull ) {
        SDL_Rect rect = (SDL_Rect){0, 0, hull[1].x-hull[0].x, hull[1].y-hull[0].y};
        SJGL_Blit( &rect, pos->x+hull[0].x, pos->y+hull[0].y, 0 );
      }
    }

    glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

    for(i=0;i<maxobjs;i++) {
      OBJ_t *o = fr[vidfrmod].objs+i;
      V *pos  = flex(o,OBJF_POS);
      if( pos ) {
        sprintf(buf,"%d",i);
        SJF_DrawText(pos->x, pos->y, buf);
      }
    }
  }

  // viewport and matrixes for HUD
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,w,h,0,-1,1);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  //paint black over the border areas, subtractively with v_oob
  {
    int outerl = 0;   int innerl = pad_left;
    int outert = 0;   int innert = pad_top;
    int outerr = w;   int innerr = pad_left + NATIVEW*scale;
    int outerb = h;   int innerb = pad_top  + NATIVEH*scale;
    glDisable(GL_TEXTURE_2D);
    glPushAttrib(GL_COLOR_BUFFER_BIT);
    if( v_oob ) {
      glColor4f(0.02,0.02,0.02,0.02);
      glBlendFunc(GL_ONE,GL_ONE);
      if( GLEW_EXT_blend_equation_separate )
        glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
    }
    else
      glColor4f(0,0,0,1.0f);
    glBegin(GL_QUADS);
    glVertex2i(outerl,outert); glVertex2i(outerr,outert); glVertex2i(outerr,innert); glVertex2i(outerl,innert); //top
    glVertex2i(outerl,innerb); glVertex2i(outerr,innerb); glVertex2i(outerr,outerb); glVertex2i(outerl,outerb); //bottom
    glVertex2i(outerl,innert); glVertex2i(innerl,innert); glVertex2i(innerl,innerb); glVertex2i(outerl,innerb); //left
    glVertex2i(innerr,innert); glVertex2i(outerr,innert); glVertex2i(outerr,innerb); glVertex2i(innerr,innerb); //right
    glEnd();
    glPopAttrib();
    glEnable(GL_TEXTURE_2D);
    glColor4f(1.0f,1.0f,1.0f,1.0f);
  }

  SJGL_SetTex( (GLuint)-1 ); //forget previous texture name
  mod_outerdraw(vidfr,w,h);

  //display console
  if(console_open) {
    int conh = h/2 - 40;
    if(conh<40) conh = 40;
    glColor4f(0.15,0.15,0.15,0.85);
    glDisable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glVertex2i(0,   0); glVertex2i(w,   0);
    glVertex2i(w,conh); glVertex2i(0,conh);
    glEnd();
    glEnable(GL_TEXTURE_2D);
    glColor4f(1.0f,1.0f,1.0f,1.0f);
    x = 10;
    y = conh-20;
    if((ticks/200)%2)
      SJF_DrawChar(x+SJF_TextExtents(SJC.buf[0]), y, '_');
    for(i=0;y>0;i++) {
      if(SJC.buf[i])
        SJF_DrawText(x,y,SJC.buf[i]);
      y -= 10;
    }
    if( SJC.buf[0] && SJC.buf[0][0] ) {
      char s[10];
      sprintf(s,"%d",SJC.buf[0][strlen(SJC.buf[0])-1]);
      SJF_DrawText(w-20,conh-20,s);
    }
  }

  //display stats
  total_time += (tmp = SDL_GetTicks()) - total_start;
  render_time += tmp - render_start;
  total_start = tmp;
  Uint32 unaccounted_time = total_time - (idle_time + render_time + adv_move_time + adv_collide_time + adv_game_time);
  if( v_showstats ) {
    Uint32 denom = vidfrmod+1;
    sprintf(buf,"idle_time %4d"       ,       idle_time/denom);
    SJF_DrawText(w-20-SJF_TextExtents(buf),10,buf);
    sprintf(buf,"render_time %4d"     ,     render_time/denom);
    SJF_DrawText(w-20-SJF_TextExtents(buf),20,buf);
    sprintf(buf,"adv_move_time %4d"   ,   adv_move_time/denom);
    SJF_DrawText(w-20-SJF_TextExtents(buf),30,buf);
    sprintf(buf,"adv_collide_time %4d",adv_collide_time/denom);
    SJF_DrawText(w-20-SJF_TextExtents(buf),40,buf);
    sprintf(buf,"adv_game_time %4d"   ,   adv_game_time/denom);
    SJF_DrawText(w-20-SJF_TextExtents(buf),50,buf);
    sprintf(buf,"unaccounted_time %4d",unaccounted_time/denom);
    SJF_DrawText(w-20-SJF_TextExtents(buf),60,buf);
    sprintf(buf,"adv_frames  %2.2f"   ,(float)adv_frames/(float)denom);
    SJF_DrawText(w-20-SJF_TextExtents(buf),70,buf);
    sprintf(buf,"fr: idx=%d meta=%d vid=%d hot=%d",metafr%maxframes,metafr,vidfr,hotfr);
    SJF_DrawText(w-20-SJF_TextExtents(buf),80,buf);
  }

  SDL_GL_SwapBuffers();
  setdrawnfr(vidfr);

  if( (int)vidfrmod==maxframes-1 ) { // reset time stats
    total_time       = 0;
    idle_time        = 0;
    render_time      = 0;
    adv_move_time    = 0;
    adv_collide_time = 0;
    adv_game_time    = 0;
    adv_frames       = 0;
  }
}