Exemplo n.º 1
0
LexicalAnalyzer::LexicalAnalyzer(const string& keywordFilePath, const string& symbolFilePath)
{
	ifstream keywordFile(keywordFilePath);
	assert(keywordFile);

	string keyword;
	int keywordNum;
	keywordFile >> keywordNum;
	for(int i = 0; i < keywordNum; ++i)
	{
		keywordFile >> keyword;
		keywordTab.push_back(keyword);
		keywordTrie.insert(keyword);
	}

	keywordFile.close();

	ifstream symbolFile(symbolFilePath);
	assert(symbolFile);

	string symbol;
	int symbolNum;
	symbolFile >> symbolNum;
	for(int i = 0; i < symbolNum; ++i)
	{
		symbolFile >> symbol;
		symbolTab.push_back(symbol);
		symbolTrie.insert(symbol);
	}

	symbolFile.close();
}
Exemplo n.º 2
0
tGame::tGame(int gridSizeX, int gridSizeY)
{
    // pre-compute the sensor offsets
    // to maintain the same order of inputs, start counting sensors from the inside.
    // e.g. for 7x7:
    
    // 43 42 41 40 39 38 37
    // 44 21 20 19 18 17 36
    // 45 22 7  6  5  16 35
    // 46 23 8  0  4  15 34
    // 47 24 1  2  3  14 33
    // 48 9  10 11 12 13 32
    // 25 26 27 28 29 30 31
    
    int offsetX = 0, offsetY = 0;
    int offsetAmount = 0;
    
    for (int sensor = 0; sensor < 3 * 3; ++sensor)
    {
        int root = sqrt(sensor);
        if (root % 2 == 1 && root * root == sensor)
        {
            ++offsetAmount;
            offsetX = -offsetAmount;
            offsetY = -offsetAmount;
        }
        else if (offsetX != offsetAmount && offsetY == -offsetAmount)
        {
            ++offsetX;
        }
        else if (offsetX == offsetAmount && offsetY != offsetAmount)
        {
            ++offsetY;
        }
        else if (offsetX != -offsetAmount && offsetY == offsetAmount)
        {
            --offsetX;
        }
        else if (offsetX == -offsetAmount && offsetY != -offsetAmount)
        {
            --offsetY;
        }
        
        vector<int> offsets;
        offsets.push_back(offsetX);
        offsets.push_back(offsetY);
        sensorOffsetMap.push_back(offsets);
    }
    
    ifstream symbolFile ("mnist.train.discrete.28x28-only100");
    string line;
    string key = "";
    vector< vector<int> > symbol;
    int symbolRow = 0;

    while ( getline (symbolFile, line) )
      {
	if (line.find("-") != string::npos)
	  {
	    key = line;
	    symbol.clear();
	    symbolRow = 0;
	  }
	
	else if (line.find("0") != string::npos || line.find("1") != string::npos)
	  {
	    symbol.resize(symbol.size() + 1);
	    
	    for (int i = 0; i < line.length(); ++i)
	      {
		if (line.substr(i, 1) != " ")
		  {
		    symbol[symbolRow].push_back(atoi(line.substr(i, 1).c_str()));
		  }
	      }
	    
	    ++symbolRow;
	  }

	else
	  {
	    symbols[key] = symbol;
	    symbol_keys.push_back(key);
	    symbol_labels.push_back(atoi(key.substr(0, key.find("-")).c_str()));
	  }
      }

    symbolFile.close();
    
    // place the digits in the digit grid for the edd agent to view
    int num_symbol_keys = (int)symbol_keys.size();
    digitGrid.resize(num_symbol_keys);
    
    for (int digit = 0; digit < num_symbol_keys; ++digit)
    {
        digitGrid[digit].resize(gridSizeX);
        for (int x = 0; x < gridSizeX; ++x)
        {
            digitGrid[digit][x].resize(gridSizeY);
        }
    }
    
    // if the digits are being randomly placed, place all 10 digits (0-9)
    // in random spots on the grid at the beginning of every simulation
    // otherwise, place all 10 digits (0-9) centered in the grid
    for (int digit = 0; digit < num_symbol_keys; ++digit)
    {
        int digitCenterX = (int)(gridSizeX / 2.0), digitCenterY = (int)(gridSizeY / 2.0);
        
        placeDigit(digitGrid, digit, digitCenterX, digitCenterY);
    }
    
    // visualize the digits
    /*for (int digit = 0; digit < num_symbol_keys; ++digit)
     {
     cout << symbol_keys[digit] << endl;
     
     for (int x = 0; x < gridSizeX; ++x)
     {
	    for (int y = 0; y < gridSizeY; ++y)
     {
     cout << digitGrid[digit][x][y] << " ";
     }
	    cout << endl;
     }
     cout << "---" << endl;
     }
     exit(0);*/
}