Skip to content

Instantly share code, notes, and snippets.

@bmabey
Created October 24, 2012 15:55
Show Gist options
  • Save bmabey/3946906 to your computer and use it in GitHub Desktop.
Save bmabey/3946906 to your computer and use it in GitHub Desktop.
Example of using the java lib UserAgentUtils in clojure to parse user agent strings
(ns rbl.feature-extraction.user-agent
(:use [clojure.core.memoize :only [memo]])
(:require [clojure.tools.logging :as log])
(:import [nl.bitwalker.useragentutils UserAgent DeviceType Browser OperatingSystem]))
(defn str->features [string]
(try
(let [user-agent (UserAgent. (or string ""))]
{:browser_group (-> user-agent .getBrowser .getGroup .getName)
:os_group (-> user-agent .getOperatingSystem .getGroup .getName)
:device_type (-> user-agent .getOperatingSystem .getDeviceType .getName)})
(catch Exception e
(log/error (str "Could not derive the user-agent from " string) e)
(str->features nil))))
(def possible-features
(memo (fn []
{:os_group (set (map #(-> % .getGroup .getName) (OperatingSystem/values)))
:browser_group (set (map #(-> % .getGroup .getName) (Browser/values)))
:device_type (set (map #(.getName %) (DeviceType/values)))})))
(ns rbl.feature-extraction.user-agent-test
(:use [com.leadtune.predict.user-agent])
(:use midje.sweet))
(facts "#'str->features"
(let [user-agent "mozilla/5.0 (linux; u; android 2.2.1; de-de; htc_wildfire_a3333 build/frg83d) applewebkit/533.1 (khtml, like gecko) version/4.0 mobile safari/533.1"]
(fact
(str->features user-agent) => {:browser_group "Safari"
:os_group "Android"
:device_type "Mobile"}))
(fact
(str->features "") => {:browser_group "Unknown"
:os_group "Unknown"
:device_type "Unknown"})
(fact
(str->features nil) => {:browser_group "Unknown"
:os_group "Unknown"
:device_type "Unknown"}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment