C# Eggheads

    • C# Eggheads

      Anyone here got some talent with C# and can push me in the correct direction? I'm working on an application that reads a .txt file full of hashtags from Twitter and I am trying to figure out:


      1. How many hashtags are in the file
      2. Number of individual hashtags

      To be honest, I'm completely stumped. Also, if there's an alternative method for stopping a file from being read, instead of a while loop with != null then that'd be great too. As I found somewhere that a while loop (or probably even recommended it) would stop the file writing the lines if it was equal to null, but it fails every time.
    • Re: C# Eggheads

      I might be misunderstanding something about Twitter, but aren't both those problems the same problem?

      Personally in a situation like this I would probably use a regular expression.

      Also, to exit a loop early you can use the break statement.

      Can you post a sample of the file you're reading?
      "I've never understood ethnic or national pride, because to me pride should be reserved for something you achieve or attain on your own, not something that happens by accident of birth."
      - George Carlin

      Striker88;1062839033 wrote:

      You know why nobody has gotten evidence? God hasn't allowed that and won't.
    • Re: C# Eggheads

      This is the file I'm reading:

      Source Code

      1. <pre><p>#vote1direction
      2. #paintobray
      3. #phonehand
      4. #1direction
      5. #harry
      6. #twibbon
      7. #1directiontowin
      8. #harryhill
      9. #harryhillstvburp
      10. #casualty
      11. #one
      12. #harryhill.
      13. #aiden
      14. #teamfollowback
      15. #1direction
      16. #rebeccaferguson
      17. #tweet
      18. #1direction
      19. #teamminogue!!!
      20. #teamcheryl
      21. #strickly
      22. #scd
      23. #twibbon
      24. #1directiontowin
      25. #birmingham
      26. #tvburp
      27. #scd
      28. #scd
      29. #strictly
      30. #panic
      31. #cherlloyd
      32. #scd
      33. #cherandkatiemakemeangry
      34. #harryhill
      35. #scd
      36. #1direction
      37. #tvburp
      38. #tvburp
      39. #bieberfact
      40. #nlp
      41. #scd
      42. #storm's
      43. #shutup!!!
      44. #ladygaga
      45. #scd
      46. #twibbon
      47. #nicolotowin
      48. #harryhillstvburp
      49. #bieberfact
      50. #storm's
      51. #tvburp
      52. #itv1
      53. #twibbon
      54. #1directiontowin
      55. #twibbon
      56. #matttowin
      57. #vote1direction
      58. #strictlycomedancing
      59. #dassupertalent
      60. #vote1direction
      61. #bitchfest
      62. #vote1direction
      63. #thexfactor
      64. #xtrafactor
      65. #fml
      66. #offline
      67. #scd
      68. #bieberfact
      69. #twibbon
      70. #1directiontowin
      71. #higher
      72. #teamsats
      73. #vote1direction
      74. #bieberfact
      75. #harryhill
      76. #neonrainbow
      77. #timetofacethemusic
      78. #yourweekendstartshere
      79. #tvburp.
      80. #strictlycomedancing
      81. #bieberfact
      82. #deportationfactor
      83. #vote1direction
      84. #higher
      85. #teamsats
      86. #twibbon
      87. #chertowin
      88. #scd
      89. #moreteavicar
      90. #harryhill
      91. #teammary
      92. #bringbackdivafever
      93. #thexfactor
      94. #strictly.
      95. #johnadeleyex!
      96. #1direction
      97. #tescomary
      98. #itv
      99. #strictly
      100. #hmm.
      101. #very
      102. #ineedalife.
      103. #1direction
      104. #strictlycomedancing
      105. #strictlycomedancing
      106. #tvburp
      107. #twitter
      108. #twibbon
      109. #1directiontowin
      110. #harryhill
      111. #harryhill
      112. #modernfamily
      113. #cherylcole
      114. #in10years
      115. #goodluck1d
      116. #grandprix
      117. #teamcheryl
      118. #scd
      119. #nfl
      120. #onlycoswehaveabigteleupstairs
      121. #teamminogue
      122. #scd
      123. #offline
      124. #strictly
      125. #scd
      126. #harry
      127. #teamcheryl
      128. #teamdermot
      129. #teamchermot
      130. #fb
      131. #scd
      132. #scd
      133. #itbegins
      134. #teamcheryl
      135. #teamcherlloyd
      136. #twibbon
      137. #matttowin
      138. #1direction
      139. #ineedalife.
      140. #harryhill
      141. #nowwatching
      142. #1direction
      143. #1direction!
      144. #vote1direction
      145. #vote1direction
      146. #scd
      147. #onedirection
      148. #sytycd
      149. #onedirection
      150. #twibbon
      151. #chertowin
      152. #vote1direction
      153. #ff
      154. #fs
      155. #cherylcole
      156. #mattcardle
      157. #cherylsoldiers
      158. #vote1direction
      159. #gamu
      160. #cherylcole
      161. #gamu
      162. #scd
      163. #thedeparture
      164. #fail
      165. #twitter
      166. #bringbackgamu
      167. #teamdannii
      168. #onedirection?
      169. #vote1direction
      170. #corrie
      171. #reallysurprising
      172. #nom
      173. #celebrityjuice
      174. #harryhill
      175. #scd
      176. #simoncowell!
      177. #strictlycomedancing
      178. #cherlloyd
      179. #katie
      180. #makeovers
      181. #bullet4cheryl
      182. #nowwatching
      183. #cardle
      184. #strictlycomedancing
      185. #stonecoldgamu
      186. #fb
      187. #scd.
      188. #scd.
      189. #fyd.
      190. #merlin
      191. #rebecca
      192. #twist
      193. #now
      194. #tvburp
      195. #vote1direction
      196. #scd.
      197. #onedirection
      198. #donotdisturb
      199. #belami
      200. #yay
      201. #matt
      202. #mary
      203. #rebecca
      204. #teambelleamie
      205. #itv1
      206. #fyd
      207. #omgxfactor!!!!!!!!!!!!
      208. #itv1
      209. #stv
      210. #utv
      211. #live
      212. #direkt
      213. #scd
      214. #teamminogue
      215. #stormlee
      216. #twistfactor
      217. #stonecol
      218. #stonecol
      219. #teamcherlloyd
      220. #johnlennon
      221. #merlin
      222. #bbc1!#merlin
      223. #letsgo
      224. #divafever
      225. #miss
      226. #boycott
      227. #fail
      228. #cherleader
      229. #cherlloyd
      230. #thexfactor
      231. #doctorwho
      232. #tvburp!
      233. #endtimes
      234. #1970election
      235. #scd
      236. #bloodylovesaturdaynightsinfrontofthetelly
      237. #cherlloyd
      238. #bringbackmattscap
      239. #bitchfest</pre>
      Display All


      Also, just to point out I'm no amazing pro at C# either. So I only at the moment maybe basics. I came up with a theory to attempt to count # each time it was read, but that failed dramatically; I would count per line, but that doesn't mean it was a hashtag. As for counting individual tags (disregarding repeated tags) I haven't attempted yet, and doubt I could until I crack how to count.
    • Re: C# Eggheads

      I might misunderstood what problem is but, as I get
      you can simply count how much lines are there (with some TXT editor)
      and then use
      for(int n=0; n<C; n++)

      where C is number of lines in TXT

      and it would stop reading from files, untill it reaches C (number of lines)

      The post was edited 1 time, last by Rave ().

    • Re: C# Eggheads

      ah, so you want to count a lines depending on # quantity?

      I guess, you can simply use strings, while and if, like
      to get how many ' # ' is in in sentense, you can use

      string Str;
      int counter =0, i =0;
      ifstream ifs("sample.txt");
      ifs>>Str;
      ///////getline(Str);
      while(i < Str.size()){
      if( Str == '#' )
      counter++;
      ++i;
      }





      I'm not sure if it does work, but you can still try..

      ---------- Post added at 10:53 PM ---------- Previous post was at 10:51 PM ----------

      and to get it on command line ..
      cout<<"there are "<<counter<<" lines "<<endl;



      but that will work only, if only 1 # is per line.. because it counts quantity of #, and not lines.
      unfortunetely, I don't know another much tricky way :D

      anyways, for this time I only know some basics of C++, dont even know if it corresponds C#


      Wait, there doesn't need
      getline
      <<

      The post was edited 1 time, last by Rave ().

    • Re: C# Eggheads

      Yeah, your code didn't work for me too well but has helped a tad; I think. I managed to get what you gave me to work to a certain degree, though it didn't really help me at all as the results ended up being like this:




      Here's the code of what I was messing with:

      Source Code

      1. line = HashFile.ReadLine();
      2. int counter = 0, i = 0;
      3. FileStream ifs = new FileStream("tweets.txt", FileMode.Open, FileAccess.Read);
      4. while ((line = HashFile.ReadLine()) != null)
      5. {
      6. // Write a line of text to the file
      7. //theHTML.WriteLine(line);
      8. Console.WriteLine(line);
      9. // Read the next line
      10. line = HashFile.ReadLine() + "r";
      11. while(i < line.Length)
      12. {
      13. if( line[i] == '#' )
      14. {
      15. counter++;
      16. }
      17. ++i;
      18. }
      19. Console.WriteLine("{0}t{1}", counter, i);
      20. }
      Display All
    • Re: C# Eggheads

      well, I wrote there C++ code, (the only thing I know now :D ) wanted to show you an idea, not actually a correct code to make it work.

      anyways, Im happy I helped you at least a little :p



      while(i < line.Length)
      {
      if( line == '#' )
      {
      counter++;
      }
      ++i;
      }


      yeah, that;s the main thing I suggest, to count only # ( as there was only one # per line ), that would have worked

      ---------- Post added at 11:46 PM ---------- Previous post was at 11:39 PM ----------

      So, does this work?

      line = HashFile.ReadLine();

      int counter = 0, i = 0;
      FileStream ifs = new FileStream("tweets.txt", FileMode.Open, FileAccess.Read);

      while ((line = HashFile.ReadLine()) != null)
      {
      // Write a line of text to the file
      //theHTML.WriteLine(line);
      Console.WriteLine(line);

      // Read the next line
      line = HashFile.ReadLine() + "r";

      while(i < line.Length)
      {
      if( line[i] == '#' )
      {
      counter++;
      }
      ++i;
      }
      Console.WriteLine("{0}t{1}", counter, i);
      }
    • Re: C# Eggheads

      Altered the code slightly and I'm getting somewhere now, although I did a line count in Word and it reports 239 lines where as my code reports 119 #'s yet that's wrong. =/

      Source Code

      1. line = HashFile.ReadLine();
      2. int counter = 0, i = 0;
      3. FileStream ifs = new FileStream("tweets.txt", FileMode.Open, FileAccess.Read);
      4. while ((line = HashFile.ReadLine()) != null)
      5. {
      6. // Write a line of text to the file
      7. //theHTML.WriteLine(line);
      8. Console.WriteLine(line);
      9. // Read the next line
      10. line = HashFile.ReadLine() + "r";
      11. if (line[i] == '#')
      12. {
      13. counter++;
      14. }
      15. }
      16. Console.WriteLine("{0}", counter);
      Display All
    • Re: C# Eggheads

      Matt wrote:

      Altered the code slightly and I'm getting somewhere now, although I did a line count in Word and it reports 239 lines where as my code reports 119 #'s yet that's wrong. =/

      Source Code

      1. line = HashFile.ReadLine();
      2. int counter = 0, i = 0;
      3. FileStream ifs = new FileStream("tweets.txt", FileMode.Open, FileAccess.Read);
      4. while ((line = HashFile.ReadLine()) != null)
      5. {
      6. // Write a line of text to the file
      7. //theHTML.WriteLine(line);
      8. Console.WriteLine(line);
      9. // Read the next line
      10. line = HashFile.ReadLine() + "r";
      11. if (line[i] == '#')
      12. {
      13. counter++;
      14. }
      15. }
      16. Console.WriteLine("{0}", counter);
      Display All


      can you PrintScreen what was the result?
    • Re: C# Eggheads

      I would read each line of the file into an array list and then go from there.

      EDIT: If you're testing for duplicates, check the array list for the current value. If it exists, move on. If it doesn't, add it.
      "I've never understood ethnic or national pride, because to me pride should be reserved for something you achieve or attain on your own, not something that happens by accident of birth."
      - George Carlin

      Striker88;1062839033 wrote:

      You know why nobody has gotten evidence? God hasn't allowed that and won't.

      The post was edited 1 time, last by DamnImGood ().

    • Re: C# Eggheads

      I could do this in nice and quick in Java, but then no one would probably touch it with a 10 foot pole :p :lol:
      [LEFT][COLOR="Black"]“Happiness can be found, even in the darkest of times, if one only remembers to turn on the light.”[/COLOR][/LEFT]
      [COLOR="DarkRed"][RIGHT]Albus Percival Wulfric Brian Dumbledore - Just chillin' out down here at Kings Cross Station y'all! It's a bit foggy though... what's up with that?[/RIGHT][/COLOR]
      [CENTER][/CENTER]
    • Re: C# Eggheads

      After a good rest, and a re-read of a website and copying it, then altering it, I've finally managed to get my result. The only issue I have now is, I thought it'd be more suitable for the code to have it's own method, then I can call it in the Main but I can't return a variable in an If statement. =/

      EDIT: Scratch my return issue, I fixed it.

      Secondly, is anyone willing to tell me what each line in the code box below does? So I at least can get a better understanding of it.

      Source Code

      1. {
      2. //Array to store frequencies.
      3. int[] c = new int[(int)char.MaxValue];
      4. //Read entire text file.
      5. string s = File.ReadAllText("tweets.txt");
      6. // Iterate over each character.
      7. foreach (char t in s) // What has t got to do with this?
      8. {
      9. // Increment table.
      10. c[(int)t]++; //Unsure if I understand this.
      11. }
      12. // Write all letters found.
      13. for (int i = 0; i < (int)char.MaxValue; i++) //For every time char.MaxVal is more then i, increment by one. Right?
      14. {
      15. if (c[i] > 0 &&
      16. char.IsPunctuation((char)i))
      17. //This is where I got lucky, thanks to VS. I changed a part and replaced it with IsPunctuation, but I'm unsure about the rest of that if statement.
      18. //All I can make from it, from testing it was.. If the array c is more than 0 and at the same time the char i is a puncuation, then do shit in the code below.
      19. {
      20. if ((char)i == '#') // This is where I shine, I wrote this based on parts of code above. =]
      21. {
      22. Hashresults = c[i]; //I added this, and I wanted to use it as a return, but it is complete failure at the moment.
      23. //Console.WriteLine("Letter: {0} Frequency: {1}", (char)i, c[i]);
      24. }
      25. }
      26. }
      Display All
    • Re: C# Eggheads

      I'm not sure why they're doing all the typecasting in the code. Should be able to do it without it, but whatever works.

      One quick tip when writing code: give your variables meaningful names. "c", "t", "s", etc. aren't very meaningful; they tell you nothing about what's being stored in those variables and it makes reading and debugging much more difficult. It's an unfortunate trend with JavaScript developers. They do it to conserve script size (and to obfuscate their code), but it's annoying as hell.

      Matt wrote:

      Secondly, is anyone willing to tell me what each line in the code box below does? So I at least can get a better understanding of it.

      It's been a while since I worked with .NET but I'll give it a quick shot.

      Source Code

      1. //Array to store frequencies.
      2. int[] c = new int[(int)char.MaxValue];
      Creates the array.

      Source Code

      1. //Read entire text file.
      2. string s = File.ReadAllText("tweets.txt");
      Pulls the entire text file into a string (wouldn't recommend doing this).

      Source Code

      1. // Iterate over each character.
      2. foreach (char t in s) // What has t got to do with this?
      3. {
      4. // Increment table.
      5. c[(int)t]++; //Unsure if I understand this.
      6. }
      The "t" variable is just a temporary variable used for the foreach loop. What it's doing is for each char in the variable "s", increase that char's count in your main array. Remember strings are just a sequence of characters (hence the name "strings" - "string of characters").

      Source Code

      1. // Write all letters found.
      2. for (int i = 0; i < (int)char.MaxValue; i++) //For every time char.MaxVal is more then i, increment by one. Right?
      3. {
      4. if (c[i] > 0 &&
      5. char.IsPunctuation((char)i))
      6. //This is where I got lucky, thanks to VS. I changed a part and replaced it with IsPunctuation, but I'm unsure about the rest of that if statement.
      7. //All I can make from it, from testing it was.. If the array c is more than 0 and at the same time the char i is a puncuation, then do shit in the code below.
      8. {
      9. if ((char)i == '#') // This is where I shine, I wrote this based on parts of code above. =]
      10. {
      11. Hashresults = c[i]; //I added this, and I wanted to use it as a return, but it is complete failure at the moment.
      12. //Console.WriteLine("Letter: {0} Frequency: {1}", (char)i, c[i]);
      13. }
      14. }
      15. }
      Display All
      The for loop is looping through every possible character and seeing if it's a hash character. I think.

      Looking back at this issue I would have done this as a two-dimensional array. The total hashes is easy since each line only has one hash, therefore the total lines is the total hashes.

      For the second problem I would test each hash if it exists in my array. If it doesn't, then add the hash to the array in the first dimension, and in the second dimension with a value of 1 (the number of occurrences - will be 1 for the first time of course). If it does exist, then increase the corresponding hash's second dimension by 1. PHP has an in_array() function. I imagine .NET has something similar.

      I'm going to do this in PHP with the sample you posted and then post the code.
      "I've never understood ethnic or national pride, because to me pride should be reserved for something you achieve or attain on your own, not something that happens by accident of birth."
      - George Carlin

      Striker88;1062839033 wrote:

      You know why nobody has gotten evidence? God hasn't allowed that and won't.