<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rafadamar!! &#187; programming</title>
	<atom:link href="http://rafadamar.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://rafadamar.com</link>
	<description>Hate me for who I am not who you think I am.</description>
	<lastBuildDate>Sat, 28 Jan 2012 02:12:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Spell Checker</title>
		<link>http://rafadamar.com/2009/11/spell-checker/</link>
		<comments>http://rafadamar.com/2009/11/spell-checker/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 01:53:50 +0000</pubDate>
		<dc:creator>Ralph</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[#include &#60;iostream&#62; #include &#60;fstream&#62; #include &#60;string&#62; #include &#60;map&#62; #include &#60;vector&#62; using namespace std; // Make know words map map&#60;string,int&#62; get_words(istream &#38;input){ map&#60;string,int&#62; words; string word; // Go through big file collecting words while(!input.eof()){ input &#62;&#62; word; // Make sure string is a word int len = word.length(); bool isWord = true; for( int x = [...]]]></description>
			<content:encoded><![CDATA[<pre class="brush: cpp">
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;map&gt;
#include &lt;vector&gt;
using namespace std;

// Make know words map
map&lt;string,int&gt; get_words(istream &amp;input){
  map&lt;string,int&gt; words;
  string word;
  // Go through big file collecting words
  while(!input.eof()){
    input &gt;&gt; word;
    // Make sure string is a word
    int len = word.length();
    bool isWord = true;
    for( int x = 0; x &lt; len; x++){
      word[x]=tolower(word[x]);
      if(!isalpha(word[x]))
        isWord = false;
    }
    // Put words into map that keeps track
    // of count
    if(isWord)
      words[word]++;
  }
  return words;
}

// Make words of distance one
vector&lt;string&gt; edits1(string word){
  vector&lt;string&gt; words;
  int n = word.length();
  // Deletions
  for(int x = 0;x &lt; n;x++){
    string temp = word;
    temp.erase(temp.begin() + x);
    words.push_back(temp);
  }

  // Transpositions
  for(int x = 0; x &lt; n-1; x++){
    string temp = word;
    char c;
    c = temp[x];
    temp[x] = temp[x+1];
    temp[x+1] = c;
    words.push_back(temp);
  }  

  // Alterations
  string alphabet = &quot;abcdefghijklmnopqrstuvwxyz&quot;;
  for(int x = 0; x &lt; n; x++){
    for(int y = 0; y &lt; 26; y++){
      string temp = word;
      temp[x] = alphabet[y];
      words.push_back(temp);
    }
  }

  // Insertions
  for(int x=0;x&lt;=n;x++&#41;&#123;
		for&#40;int y=0;y&lt;26;y++&#41;&#123;
			string temp = word;
			temp.insert&#40;x,1,alphabet&#91;y&#93;&#41;;
			words.push_back&#40;temp&#41;;
		&#125;
	&#125;
&nbsp;
	return words;
&nbsp;
&#125;
&nbsp;
// Make known words of distance 2
vector&lt;string&gt; known_edits2&#40;string word, map&lt;string,int&gt; known_words&#41;&#123;
	vector&lt;string&gt; words,words2,temp;
&nbsp;
	// Get words of distance 1
	words = edits1&#40;word&#41;;
&nbsp;
	int count = words.size&#40;&#41;;
&nbsp;
	// Get words of distance 2
	for&#40;int x=0;x&lt;count;x++&#41;&#123;
		temp = edits1&#40;words&#91;x&#93;&#41;;
&nbsp;
		int count = temp.size&#40;&#41;;
&nbsp;
		// Insert known words
		for&#40;int x=0;x&lt;count;x++&#41;
			if&#40;known_words&#91;&#40;temp&#91;x&#93;&#41;&#93;&#41;
				words2.push_back&#40;temp&#91;x&#93;&#41;;
&nbsp;
	&#125;
&nbsp;
	return words2;
&#125;
&nbsp;
// Find known words with input as vector
vector&lt;string&gt; known&#40;vector&lt;string&gt; temp,map&lt;string,int&gt; known_words&#41;&#123;
	vector&lt;string&gt; words;
&nbsp;
	int count = temp.size&#40;&#41;;
&nbsp;
	// Push back known words
	for&#40;int x=0;x&lt;count;x++&#41;&#123;
		if&#40;known_words&#91;&#40;temp&#91;x&#93;&#41;&#93;&#41;
			words.push_back&#40;temp&#91;x&#93;&#41;;
	&#125;
&nbsp;
	return words;
&#125;
&nbsp;
// Find know words with input as string
vector&lt;string&gt; known&#40;string temp,map&lt;string,int&gt; known_words&#41;&#123;
	vector&lt;string&gt; words;
&nbsp;
	// is the word known?
	if&#40;known_words&#91;temp&#93;&#41;
		words.push_back&#40;temp&#41;;
&nbsp;
	return words;
&#125;
&nbsp;
// Find the correct word
string correct&#40;string word, map&lt;string,int&gt; known_words&#41;&#123;
	vector&lt;string&gt; candidates;
&nbsp;
	// Is the word known?
	candidates = known&#40;word,known_words&#41;;
&nbsp;
	// If not, get words from distance 1
	if&#40;candidates.size&#40;&#41; == 0&#41;
		candidates = known&#40;edits1&#40;word&#41;,known_words&#41;;
&nbsp;
	// If nothing from distance 1 is a word,
	// get words from distance 2
	if&#40;candidates.size&#40;&#41; == 0&#41;
		candidates = known_edits2&#40;word,known_words&#41;;
&nbsp;
	// If nothing return word
	if&#40;candidates.size&#40;&#41; == 0&#41;
		return word;
&nbsp;
	int count = candidates.size&#40;&#41;;
	int max=0,temp=0;
	string candidate;
&nbsp;
	// Get word with the highest usage from candidates
	for&#40;int x=0;x&lt;count;x++&#41;&#123;
		temp = known_words&#91;&#40;candidates&#91;x&#93;&#41;&#93;;
		if&#40;temp &gt; max&#41;&#123;
			max = temp;
			candidate = candidates&#91;x&#93;;
		&#125;
	&#125;
&nbsp;
	return candidate;
&nbsp;
&#125;
&nbsp;
int main&#40;int argc, char* argv&#91;&#93;&#41;&#123;
&nbsp;
	if&#40;argc != 2&#41;&#123;
		cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; argv&#91;0&#93; &lt;&lt; &quot;string&quot; &lt;&lt; endl;
		return -1;
	&#125;
&nbsp;
	// Get word from user input
	string word = argv&#91;1&#93;;
&nbsp;
	// File that has huge collection of words
	ifstream file&#40;&quot;big.txt&quot;&#41;;
&nbsp;
	// Create map of words
	map&lt;string,int&gt; known_words = get_words&#40;file&#41;;
&nbsp;
	// Get correct word
	string correction = correct&#40;word, known_words&#41;;
&nbsp;
	cout &lt;&lt; &quot;correct( &quot; &lt;&lt; word &lt;&lt; &quot; ) == &quot; &lt;&lt; correction &lt;&lt; endl;
&nbsp;
	return 0;
&#125;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://rafadamar.com/2009/11/spell-checker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is Prime</title>
		<link>http://rafadamar.com/2009/11/is-prime/</link>
		<comments>http://rafadamar.com/2009/11/is-prime/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 01:50:50 +0000</pubDate>
		<dc:creator>Ralph</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[#include &#60;iostream&#62; #include &#60;vector&#62; #include &#60;cmath&#62; &#160; using namespace std; &#160; // Tests whether a function is prime bool test_prime&#40;int num&#41;&#123; int count = 0,x=2; double numx = &#40;double&#41; num; // Go through all primes for&#40;x=0;x&#60;sqrt&#40;numx&#41;;x++&#41;&#123; if&#40;num % x == 0&#41;&#123; return false; &#125; &#125; &#160; return true; &#125; &#160; int main&#40;int argc, char *argv&#91;&#93;&#41;&#123; [...]]]></description>
			<content:encoded><![CDATA[<pre class="brush: cpp">
#include &lt;iostream&gt;

#include &lt;vector&gt;

#include &lt;cmath&gt;

&nbsp;

using namespace std;

&nbsp;

// Tests whether a function is prime

bool test_prime&#40;int num&#41;&#123;

	int count = 0,x=2;

	double numx = &#40;double&#41; num;

	// Go through all primes

	for&#40;x=0;x&lt;sqrt&#40;numx&#41;;x++&#41;&#123;

		if&#40;num % x == 0&#41;&#123;

			return false;

		&#125;

	&#125;

&nbsp;

	return true;	

&#125;

&nbsp;

int main&#40;int argc, char *argv&#91;&#93;&#41;&#123;

	int num;

&nbsp;

	if&#40;argc != 2&#41;&#123;

		cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; argv&#91;0&#93; &lt;&lt; &quot; positive-integer&quot; &lt;&lt; endl;

		return -1;

	&#125;else&#123;

		num = atoi&#40;argv&#91;1&#93;&#41;;

	&#125;

&nbsp;

	bool is_prime = test_prime&#40;num&#41;;

	if&#40;is_prime&#41;&#123;

		cout &lt;&lt; num &lt;&lt; &quot; is prime&quot; &lt;&lt; endl;

	&#125;else&#123;

		cout &lt;&lt; num &lt;&lt; &quot; isn&#039;t prime&quot; &lt;&lt; endl;

	&#125;

&nbsp;

&#125;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://rafadamar.com/2009/11/is-prime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

