Skip to content

Instantly share code, notes, and snippets.

@bruinbrown
Created May 24, 2013 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bruinbrown/5644671 to your computer and use it in GitHub Desktop.
Save bruinbrown/5644671 to your computer and use it in GitHub Desktop.
Decided to take the Twitter search (http://byteonmyside.wordpress.com/2013/05/17/a-twitter-search-client-in-10-lines-of-code-with-f-and-the-json-type-provider/) and roughly calculate Twitter's attitude to a topic. Very rough approximation of attitude but it gave me a chance to try some more F# stuff. Some changes which I know I can make next: ty…
open System.Linq
type T = FSharp.Data.JsonProvider<"http://search.twitter.com/search.json?q=%23fsharp&lang=en&rpp=1&page=1">
let positives = [| "good"; "awesome"; "sweet"; "nice"; "cool" |]
let negatives = [| "crap"; "terrible"; "poor"; "bad"; "awful" |]
let tweets (tag:string) (since: System.DateTime) =
let enc = System.Net.WebUtility.UrlEncode : string -> string
let rec page n =
if n < 16 then
let data = T.Load(sprintf "http://search.twitter.com/search.json?q=%s&rpp=100&page=%d&since=%4d-%02d-%02d" (enc tag) n since.Year since.Month since.Day)
seq{
yield! data.Results
if not (Seq.isEmpty data.Results) then yield! page (n+1)
}
else
Seq.empty
page 1
let rec positiveString (str:string list) =
match str with
| [] -> false
| head::tail -> (if positives.Contains(head) then
true
else positiveString tail)
let (|PositiveTweet|_|) (tweet:string) =
let lowerString = tweet.ToLower().Split(' ')
|> Array.toList
let result = positiveString lowerString
match result with
| true -> Some ()
| false -> None
let rec negativeString (str:string list) =
match str with
| [] -> false
| head :: tail -> (if negatives.Contains(head) then
true
else
negativeString tail)
let (|NegativeTweet|_|) (tweet:string) =
let lowerString = tweet.ToLower().Split(' ')
|> Array.toList
let result = negativeString lowerString
match result with
| true -> Some ()
| false -> None
[<EntryPoint>]
let main argv =
let searchTerm = Array.get argv 0
let posCount, negCount, indifferentCount = ref 0, ref 0, ref 0
tweets searchTerm (System.DateTime.UtcNow - new System.TimeSpan(5,0,0,0))
|> Seq.iter (fun t ->
match t.Text with
| PositiveTweet tweet -> posCount := !posCount + 1
| NegativeTweet tweet -> negCount := !negCount + 1
| _ -> indifferentCount := !indifferentCount + 1 )
let basicPosPercentage:float = ((float !posCount) / ((float !posCount) + (float !negCount))) * 100.0
printfn "Found %d positive tweets and %d negative tweets\n" !posCount !negCount
printfn "Twitter is %f%% positive about %s" basicPosPercentage searchTerm
System.Console.ReadLine()
|> ignore
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment