r3dux.org

A number-pimping side project from the valleys in *NEW* upside-down flavour.

  • Home
  • ABOUT
  • OLD SITE
  • SEARCH
  • FEEDBACK

How To: Transfer elements between vectors in C++

r3dux | January 16, 2013

Sometimes I end up needing to do this, so I re-implement the functionality… And it segfaults or behaves strangely. This time I’ll write it down somewhere I know where I can find it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
int main()
{
	// Create an empty vector of type string for our shopping list
	vector<string> shoppingList;
 
	// Add a few items to it
	shoppingList.push_back("Milk");
	shoppingList.push_back("Bread");
	shoppingList.push_back("Eggs");
 
	// Create an empty vector of type string for our purchases
	vector<string> purchases;
 
	// Create a string to hold the user's answer
	string answer;
 
	// Transfer items bought from the shopping list to the purchases list (includes removal from the shoppingList vector)
	vector<string>::iterator i = shoppingList.begin();
	while (i != shoppingList.end() )
	{
		cout << "Item: " << *i << endl;
		cout << "Purchase item? (y/n)" << endl;
 
		cin >> answer;
 
		// If we said yes...
		if (answer == "y")
		{
			// ...add the item to the purchases vector and...
			purchases.push_back(*i);
 
			// ...remove the item from the shoppingList vector. Erase returns an
			// iterator which points at the next element in the vector, so we need to
			// skip incrementing the iterator in this case or we might go out of bounds!
			i = shoppingList.erase(i);
		}
		else // Otherwise just move on to the next item!
		{
			i++;
		}
	}
 
	cout << endl;
 
	// Display both lists
	cout << "----- Shopping List ----" << endl;
	for (i = shoppingList.begin(); i != shoppingList.end(); i++)
	{
		cout << *i << endl;
	}
 
	cout << endl;
 
	cout << "----- Purchases ----" << endl;
	for (i = purchases.begin(); i != purchases.end(); i++)
	{
		cout << *i << endl;
	}
 
	cout << endl;
 
	return 0;
}

TLTB ;-)

Comments
No Comments »
Categories
Coding, How-To
Tags
C++, Elements, Transfer, Vector
Comments rss Comments rss
Trackback Trackback

How To: Disable the Proofreader in WordPress

r3dux | December 10, 2012

I liked the WordPress proofreading service at first, but then it started telling me that basically everything I wrote was wrong, and that I should dumb down every single post to include only words which lifetime readers of the Sunday Sport would understand. So I wanted to disable it – only I couldn’t find the option…

It’s under Users > Your-Profile

WordPress Proofreading Settings

If I’d thought of it as a per-user setting I’d have looked there – and admittedly, it makes sense for it to be a per-user setting.

But damned if I could find it.

Comments
No Comments »
Categories
How-To, Site
Tags
Disable, Proofreader, Setting, Wordpress
Comments rss Comments rss
Trackback Trackback

How To: Connect to a MySQL database using Connector/.NET in C#

r3dux | October 16, 2012

Try this:

/** 
 * Note: For this project to work the mysql.data.dll reference has to be added to the project by Right-clicking
 * on References then choosing Add Reference (in the Solution Explorer pane), then clicking on the Browse tab
 * and selecting the file mysql.data.dll - which you should first copy into the project folder along with the
 * other .dll files extracted from the mysql-connector-net-6.5.4-noinstall.zip file from: 
 * http://dev.mysql.com/downloads/connector/net/#downloads
 * 
 * When getting the Connector/.NET be sure to pick the ".NET/Mono" platform and not just "Microsoft Windows", also
 * you likely want to target .Net 4.0 onwards (not .NET 2.0 onwards) so once you've extracted the connector archive
 * be sure to copy the dlls from the "v4" folder and not the "v2" folder!
 **/
 
using System;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
 
namespace SimpleMySQLConnector
{
	class Program
	{
		static void Main(string[] args)
		{             
			string host     = "127.0.0.1"; // The IP address 127.0.0.1 is the same as "localhost" - it just means "this machine"
			string database = "testdb";    // If this database doesn't already exist we'll create it
			string user     = "root";      // Default WampServer MySQL username
			string password = "";          // Default WampServer MySQL password (no password!)
 
			// Create a provider string from our details
			string connectionString = "Data Source=" + host
 						+ ";Database="   + database
						+ ";User ID="    + user
						+ ";Password="   + password;
 
			// Create a new connection to the database (this doesn't actually connect yet - we have to call .Open for that!)
			MySqlConnection dbConn = new MySqlConnection(connectionString);
 
			// Try to actually open the connection to the DBMS and database
			try
			{
				Console.WriteLine("Attempting to connect to: " + database + "@" + host + "...");
				dbConn.Open();               
			}
			// Can't connect to the database? Then let's try creating it...
			catch (MySqlException e)
			{
				Console.WriteLine(e.Message);                
				Console.WriteLine("Could not connect to: " + database + " - attempting to create it...");
 
				try
				{
					// Specify a connection string which doesn't mention the database we're connecting to and try to open
					// the connection. If this fails there's a problem with the DBMS and we're going to be forced to quit.
					string noDBConnString = "Data Source=" + host + ";User ID=" + user + ";Password=" + password;
					dbConn.ConnectionString = noDBConnString;
					dbConn.Open();
 
					// Assuming we can connect to the DBMS at all, attempt to create the database
					string sql = "CREATE DATABASE " + database + "; USE " + database; 
					MySqlCommand cmd = new MySqlCommand(sql, dbConn);
					cmd.ExecuteNonQuery();
 
					// Create a "users" table which contains two fields: ID which is an Int, and name which is a VARCHAR(20)
					cmd.CommandText = "CREATE TABLE users(ID INT, name VARCHAR(20))";
					cmd.ExecuteNonQuery();
 
					Console.WriteLine("Database and tables created successfully!");
				}
				// Can't create the database and/or table? Not much else for it but to quit out...
				catch (MySqlException e2)
				{
					Console.WriteLine(e2.Message);
					Console.ReadLine();
					Environment.Exit(-1);
				}
			}
 
			// Get a valid integer from the user
			bool gotValidInt = false;
			int idNumber = 0;
			while (!gotValidInt)
			{
				// Get some input from the user            
				Console.WriteLine("Please enter a ID number:");
				string idNumberString = Console.ReadLine();
				int idNumberInt;
				if (int.TryParse(idNumberString, out idNumberInt)) // Try to parse the string as an integer
				{
					idNumber = idNumberInt; // Assign the successfully-converted-to-int value to our idNumber
					gotValidInt = true;     // Set our flag to say we got a valid int so can leave the while loop!
				}
				else
				{
					Console.WriteLine("Not an integer!");
				}
			}
 
			// Get a user name from the user
			Console.WriteLine("Please enter a user name:");
			string name = Console.ReadLine();
 
			// Use a prepared statement to insert the values we just got from the user into our database
			try
			{
				string sql = "INSERT INTO users(ID, name) VALUES (@idValue, @nameValue)";
				MySqlCommand cmd = new MySqlCommand(sql, dbConn);
				cmd.Prepare();
				cmd.Parameters.AddWithValue("@idValue", idNumber); // Substitute in the idNumber value for @idValue
				cmd.Parameters.AddWithValue("@nameValue", name);   // Substitute in the name     value for @nameValue
				cmd.ExecuteNonQuery();
 
				Console.WriteLine();
				Console.WriteLine("Data insertion successful!");
				Console.WriteLine();
			}
			catch (MySqlException e)
			{
				Console.WriteLine(e.Message);
			}
 
 
			// Display all the entries in the "users" table
			Console.WriteLine("---- Table Contents ----");
			try
			{
				// Create a SQL query which can be executed on our database connection
				string sqlQuery = "SELECT * FROM users";
				MySqlCommand cmd = new MySqlCommand(sqlQuery, dbConn);
 
				// Create an object to hold the results of our query and run the query
				// Note: The MySqlDataReader object is a read-only onject which allows you to quickly
				// get records out of a database table. It cannot be used to update a database table. 
				MySqlDataReader mysqlReader = cmd.ExecuteReader();
 
				// Display column headings
				Console.WriteLine("ID\tName");
 
				// Loop through our results printing them out to the console
				while (mysqlReader.Read())
				{
					// The first field (field 0) of the "users" table is the ID field (which is an int)
					int idFieldValue = mysqlReader.GetInt32(0);
 
					// The second field (field 1) of the "users" table is the Name field (which is a string)
					string nameFieldValue = mysqlReader.GetString(1); 
 
					// Output the record to the console
					Console.WriteLine(idFieldValue + "\t" + nameFieldValue);
				}
 
				// Close our reader when we're done
				mysqlReader.Close();
			}
			catch (MySqlException e)
			{
				Console.WriteLine(e.Message);
			}
 
			// Close our database connection when we're done
			dbConn.Close();
 
			// Wait for the user to press a key before exiting
			Console.ReadKey();
 
		} // End of Main method
 
	} // End of class
 
} // End of namespace

Comments
2 Comments »
Categories
Coding, How-To
Tags
.NET, C++, Connect, Connector, Database, MySQL, SQL
Comments rss Comments rss
Trackback Trackback

How To: Count word occurences in a String or File using C#

r3dux | October 12, 2012

The number of times a word occurs in a piece of text (often called the term frequency) is a good starting point for all kinds of different text analysis metrics like TF*IDF or Latent Semantic Indexing.

I recently needed perform some TF*IDF document analysis using C#, so this is how I did it:

Please note: This code is deliberately bare-bones and “just-get-the-job-done” in order to keep things as simple as possible. You don’t need to tell me that it’s non-optimal or doesn’t use exception handling etc. Also, I deliberately avoided using regex as the code was to be given to inexperienced programmers to work with.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace SimpleTermFrequencyAnalyser
{
	class Program
	{
		static void Main(string[] args)
		{
			// Read a file into a string (this file must live in the same directory as the executable)
			string filename = "Alice-in-Wonderland.txt";
			string inputString = File.ReadAllText(filename);
 
			// Convert our input to lowercase
			inputString = inputString.ToLower();        
 
			// Define characters to strip from the input and do it
			string[] stripChars = { ";", ",", ".", "-", "_", "^", "(", ")", "[", "]",
						"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\n", "\t", "\r" };
			foreach (string character in stripChars)
			{
				inputString = inputString.Replace(character, "");
			}
 
			// Split on spaces into a List of strings
			List<string> wordList = inputString.Split(' ').ToList();
 
			// Define and remove stopwords
			string[] stopwords = new string[] { "and", "the", "she", "for", "this", "you", "but" };
			foreach (string word in stopwords)
			{
				// While there's still an instance of a stopword in the wordList, remove it.
				// If we don't use a while loop on this each call to Remove simply removes a single
				// instance of the stopword from our wordList, and we can't call Replace on the
				// entire string (as opposed to the individual words in the string) as it's
				// too indiscriminate (i.e. removing 'and' will turn words like 'bandage' into 'bdage'!)
				while ( wordList.Contains(word) )
				{
					wordList.Remove(word);
				}
			}
 
			// Create a new Dictionary object
			Dictionary<string, int> dictionary = new Dictionary<string, int>();
 
			// Loop over all over the words in our wordList...
			foreach (string word in wordList)
			{
				// If the length of the word is at least three letters...
				if (word.Length >= 3) 
				{
					// ...check if the dictionary already has the word.
					if ( dictionary.ContainsKey(word) )
					{
						// If we already have the word in the dictionary, increment the count of how many times it appears
						dictionary[word]++;
					}
					else
					{
						// Otherwise, if it's a new word then add it to the dictionary with an initial count of 1
						dictionary[word] = 1;
					}
 
				} // End of word length check
 
			} // End of loop over each word in our input
 
			// Create a dictionary sorted by value (i.e. how many times a word occurs)
			var sortedDict = (from entry in dictionary orderby entry.Value descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
 
			// Loop through the sorted dictionary and output the top 10 most frequently occurring words
			int count = 1;
			Console.WriteLine("---- Most Frequent Terms in the File: " + filename + " ----");
			Console.WriteLine();
			foreach (KeyValuePair<string, int> pair in sortedDict)
			{
				// Output the most frequently occurring words and the associated word counts
				Console.WriteLine(count + "\t" + pair.Key + "\t" + pair.Value);
				count++;
 
				// Only display the top 10 words then break out of the loop!
				if (count > 10)
				{
					break;
				}
			}
 
			// Wait for the user to press a key before exiting
			Console.ReadKey();
 
		} // End of Main method
 
	} // End of Program class
 
} // End of namespace

Running this on Alice’s Adventures in Wonderland gives output like this:

---- Most Frequent Terms in the File: Alice-in-Wonderland.txt ----
1       said    427
2       was     309
3       alice   262
4       that    211
5       her     204
6       with    185
7       all     161
8       had     158
9       not     130
10      very    123

Now we have the term frequency, if we want to calculate the TF*IDF score of a document we just have the IDF (Inverse Document Frequency) part to go – which is simply the log of the total number of documents divided by the number of documents containing a given keyword.

TF*IDF Worked Example

So, let’s say we search through a paper and find the term “depression” occurs 20 times:

TF = 20

Then, let’s say we have 50 papers in our corpus (i.e. collection of papers), and that 15 of those papers also contain the word “depression”:

IDF = log (50 / 15) = log (3.33333) = 0.52288

However, if the search term does not occur in any of the documents in our corpus it will lead to a divide by zero, so it’s common to perform the calculation as 1 + the number of documents containing the search term, which makes our IDF:

IDF = log (50 / 1 + 15) = log ( 50 / 16 ) = log ( 3.125 ) = 0.49485

Finally, our TF*IDF value is exactly that – the TF value multiplied by the IDF value:

TF*IDF = 20 * 0.49485 = 9.897

To paraphrase Wikipedia:

A high weight in TF*IDF is reached by a high term frequency (in a given document) and a low frequency in the number of documents that contain that term. As the term appears in more documents, the ratio inside the logarithm approaches 1, bringing the IDF and TF-IDF closer to zero.

Wrap Up

The TF*IDF workings above are correct to the best of my knowledge – however if you know any better then please don’t hesitate to chime in and let me know if anything’s not quite 100%.

Also, newline (\n) characters have to be stripped, but the implications are that words could be concatenated accidentally, for example if “the\n” is the last word on one line, and “world” is the first word on the next line down – stripping “\n” MIGHT leave “theworld” as a single word – I haven’t gone through the entire list looking for obvious word-wrap concatenations – if you care about your results you should prolly check that out.

Asides from that – cheers & happy word counting! =D

Comments
8 Comments »
Categories
Coding, How-To
Tags
C++, Count, File, string, Term Frequency, Terms, TF-IDF, TFIDF, Words
Comments rss Comments rss
Trackback Trackback

How to: Get Minecraft working in Linux

r3dux | September 27, 2012

Minecraft Wallpaper

Attempt a quickfix?

If you’re trying to run Minecraft in Linux and it’s failing with errors like:

wrong ELF class: ELFCLASS32 (Possible cause: architecture word width mismatch)

or

Could not find the main class: net.minecraft.Launcher.Frame

Then the chances are you can fix it up by performing the following actions:

  • Set your LD_LIBRARY_PATH variable with:
    export LD_LIBRARY_PATH="the-correct-path-to-your-JRE-java-binary-which-is-NOT-usr/bin"
  • (if needs be) Get rid of OpenJDK and install the official Sun/Oracle Java binaries if required.

Know how do all that? Awesome – carry on! Not so sure? Read on! ;-D

Setting the correct JRE location

First up, check if you have Java installed by typing java -version, if it comes up with something like the below, then you’re got Java installed, now we just need to find out where:

java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

If you didn’t get anything similar to the above you might want to install the official Oracle Java implementation, which I put together a brief step-by-step for here.

Assuming you have some variety of the Java Virtual Machine (JVM) install, check if your LD_LIBRARY_PATH environment variable is set with:

printenv | grep LIBRARY

If it comes up empty, then we need to find out where Java’s installed and set it correctly. To get the correct path for where your particular Java install is located, you might be tempted to try running which java:

$ which java
/usr/bin/java

However, this is a little bit of a fib, as Java is part of the alternatives setup where we can have multiple versions installed at once – which means that /usr/bin/java is really a symbolic link to the true java location. To find where it’s really pointing we can either ask update-alternatives how it’s configured like so (and pick a version different version if we want to):

$ update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
 
  Selection    Path                               Priority   Status
------------------------------------------------------------
  0            /usr/bin/gij-4.6                    1046      auto mode
  1            /usr/bin/gij-4.6                    1046      manual mode
* 2            /usr/lib/jvm/jdk1.7.0_07/bin/java   1         manual mode
 
Press enter to keep the current choice[*], or type selection number: 2

Or we can look closely at the /usr/bin/java symlink:

$ ls -alh /usr/bin/java
lrwxrwxrwx 1 root root 22 Aug 15 05:03 /usr/bin/java -> /etc/alternatives/java

Which if we then look closely at /etc/alternatives/java then points at:

ls -alh /etc/alternatives/java
lrwxrwxrwx 1 root root 33 Aug 15 16:37 /etc/alternatives/java -> /usr/lib/jvm/jdk1.7.0_07/bin/java

Same thing as what the alternatives config’s pointing at ;-)

Now that we have the correct path, we can set it in the following manner (remember to substitute your particular Java location into the below lines!):

For 64-bit Linux:

export LD_LIBRARY_PATH="/usr/lib/jvm/jdk1.7.0_07/jre/lib/amd64"

For 32-bit Linux:

export LD_LIBRARY_PATH="/usr/lib/jvm/jdk1.7.0_07/jre/lib/i386"

With that done, you should be able to see that it’s set by running:

printenv | grep LIBRARY

Which should give you something like:

$ printenv | grep LIBRARY
LD_LIBRARY_PATH=/usr/lib/jvm/jdk1.7.0_07/jre/lib/amd64

Finally, we should be fit to launch Minecraft like this (assuming the minecraft.jar file is in your home folder):

java -Xmx1024M -Xms512M -cp ~/minecraft.jar net.minecraft.LauncherFrame

Or, without setting the memory and classpath variables, like this:

java -jar ~/minecraft.jar

Be aware that the LD_LIBRARY path will only be set for the current session of the current shell, so you’ll have to re-set it a lot. A quick and easy way to do this is to just knock together a quick launcher script as below (substituting your paths in) and throw it in /usr/local/bin:

minecraft.sh

#!/bin/bash
export LD_LIBRARY_PATH="/usr/lib/jvm/jdk1.7.0_07/jre/lib/amd64"
java -Xmx1024M -Xms512M -cp ~/Games/Minecraft/minecraft.jar net.minecraft.LauncherFrame

Final Notes

Not being able to set a persistent LD_LIBRARY_PATH is a pain, because it won’t take if set in the ~/.profile or ~/.bash_profile or ~/.bash_rc config files (I’m not so sure about ~/.pam_environment) – which you can read about here: https://help.ubuntu.com/community/EnvironmentVariables.

There’s a fix which involves disabling ssh-agent in this bug report (see post #21 here), but really for the sake of a two line launcher script I think it’s best to just leave it be.

Happy mining and crafting! =D

Comments
9 Comments »
Categories
Gaming, How-To, Linux
Tags
Java, LD_LIBRARY_PATH, Linux, Minecraft
Comments rss Comments rss
Trackback Trackback

« Previous Entries

Translate

Categories

Archives

Tags

3D ActionScript ActionScript 3.0 Adobe AI Ballarat Bash C++ Class Convert CS4 Effect Error Film Flash FPS GLFW Glitch GLSL Hack How-To install Java Kinect Linux Live Mash-Up Microsoft Motion mount OpenGL Particle Problem PS3 Remix Retro script Slides Sound Ubuntu Video VirtualBox Wii Windows XBox

Gamercard

OpenR3dux

Misc.

Flattr this

RSS Feed

r3dux twitter feed



“Like as the waves make towards, the pebbl'd shore, so do our minutes, hasten to their end.”

 - William Shakespeare

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox