ua-parser / uap-core

The regex file necessary to build language ports of Browserscope's user agent parser.
Other
747 stars 449 forks source link

Recent HbbTV-related commit specifies non-existent capture group on replacement #51

Closed russellwhitaker closed 9 years ago

russellwhitaker commented 9 years ago

This recent commit introduced a reference to a non-existing capture group: https://github.com/ua-parser/uap-core/commit/a4312396449c29150656363fd5bae5979c443864

→ git blame -L4337,+4 regexes.yaml
a4312396 (Joe Green 2015-04-15 12:23:22 +0100 4337)   - regex: '(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;Vendor/(THOMSON);'
a4312396 (Joe Green 2015-04-15 12:23:22 +0100 4338)     device_replacement: '$1'
a4312396 (Joe Green 2015-04-15 12:23:22 +0100 4339)     brand_replacement: 'Thomson'
a4312396 (Joe Green 2015-04-15 12:23:22 +0100 4340)     model_replacement: '$3'

Also, the second (and last) actually specified capture group yields "THOMSON" but the brand_replacement specifies "Thomson"; this should be rationalized. Attn. @mrjgreen

mrjgreen commented 9 years ago

Well spotted - this is interesting. There doesn't seem to be anything in the user agents I'm seeing that could qualify as a model, but setting model_replacement to blank/null or leaving unspecified causes the tests to fail. The tests do pass with the model_replacement set as '$3' so perhaps if other implementations fail in the case of unspecified capture groups being used, then we should implement that same failure in the npm tests for the core.

Does anyone have any suggestions on what this should be set as?

Currently we have the following, which as @russellwhitaker pointed out is clearly not correct:

  - regex: '(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;Vendor/(THOMSON);'
    device_replacement: '$1'
    brand_replacement: 'Thomson'
    model_replacement: '$3'

I'm not sure I understand your second point about the rationalisation of "THOMSON". Are you saying you would prefer to see "THOMSON" in the result, rather than "Thomson"?

russellwhitaker commented 9 years ago

Thanks for the quick followup @mrjgreen . By "rationalisation" I meant this: if the (apparent) intent of the off-by-one reference to the last capture group was to set model_replacement to "THOMSON", does it make sense to then have brand_replacement set to "Thomson" as in this case? I'm not arguing for or against any particular choice, I'm simply wondering what the heuristic is for populating these values. I find it odd that the brand value would be hardcoded here, when it's derivable from capture group 2.

Of course, "THOMSON" or "Thomson" for the value of model, when it's clearly the vendor, doesn't seem right.

mrjgreen commented 9 years ago

Okay I think I see what you mean. No it isn't an off by one error. The parsed values are correct. The error is simply that model_replacement should be blank rather than '$3'.

The brand is correctly identifying as "Thomson".

russellwhitaker commented 9 years ago

@mrjgreen OK good, shall I generate the PR or did you want to do the honors?

mrjgreen commented 9 years ago

I would do, but it seems that leaving the model_replacement blank or unspecified the tests fail, and I've not had a good chance to sit and look at it yet.

I'm happy for you to give it a go and submit the PR if you manage to get it passing! :)

russellwhitaker commented 9 years ago

@mrjgreen did you modify all 3 affected tests? Exploring the device tests - there are 15,933 so far - I see:

uap-clj.core=> (def tests-device (:test_cases
          #_=>   (parse-string
          #_=>     (slurp ".lein-git-deps/uap-core/tests/test_device.yaml"))))
#'uap-clj.core/tests-device

uap-clj.core=> (pprint (filter #(re-matches #"(?i).*THOMSON.*" (:user_agent_string %)) tests-device))
({:user_agent_string
  "Opera/9.80 (Linux armv7l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOMSON;SW-Version/V8-MT51F01-LF1V325;Cnt/HRV;Lan/swe; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0) Presto/2.12.362 Version/12.11",
  :family "HbbTV",
  :brand "Thomson",
  :model nil}
 {:user_agent_string
  "Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V394; en) Presto/2.10.250 Version/11.60",
  :family "HbbTV",
  :brand "THOMSON",
  :model "LF1V394"}
 {:user_agent_string
  "Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V401; en) Presto/2.10.250 Version/11.60",
  :family "HbbTV",
  :brand "THOMSON",
  :model "LF1V401"})
nil

uap-clj.core=> (def regexes-all (parse-string (slurp (clojure.java.io/resource "regexes.yaml"))))
#'uap-clj.core/regexes-all

uap-clj.core=> (def regexes-device (:device_parsers regexes-all))
#'uap-clj.core/regexes-device

uap-clj.core=> (pprint (filter #(re-matches #"(?i).*THOMSON.*" (:regex %)) regexes-device))
({:regex "(HbbTV)/1\\.1\\.1.*CE-HTML/1\\.\\d;Vendor/(THOMSON);",
  :device_replacement "$1",
  :brand_replacement "Thomson",
  :model_replacement "$3"})
nil
uap-clj.core=>

All 3 tests should actually be failing.

russellwhitaker commented 9 years ago

OK, I have a candidate regex:

(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;(Vendor/)*(THOMSON)\s*(LF1V\d+)*;

I'm able to match against the 3 "HbbTV plus Thomson" examples in the devices test yaml set:

uap-clj.core=> (pprint thomson-tests)
({:user_agent_string
  "Opera/9.80 (Linux armv7l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOMSON;SW-Version/V8-MT51F01-LF1V325;Cnt/HRV;Lan/swe; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0) Presto/2.12.362 Version/12.11",
  :family "HbbTV",
  :brand "Thomson",
  :model nil}
 {:user_agent_string
  "Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V394; en) Presto/2.10.250 Version/11.60",
  :family "HbbTV",
  :brand "THOMSON",
  :model "LF1V394"}
 {:user_agent_string
  "Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V401; en) Presto/2.10.250 Version/11.60",
  :family "HbbTV",
  :brand "THOMSON",
  :model "LF1V401"})
nil

uap-clj.core=> (def thomson-regex #"(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;(Vendor/)*(THOMSON)\s*(LF1V\d+)*;")
#'uap-clj.core/thomson-regex

uap-clj.core=> (re-find thomson-regex (:user_agent_string (first thomson-tests)))
["HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOMSON;" "HbbTV" "Vendor/" "THOMSON" nil]
uap-clj.core=> (re-find thomson-regex (:user_agent_string (second thomson-tests)))
["HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V394;" "HbbTV" nil "THOMSON" "LF1V394"]
uap-clj.core=> (re-find thomson-regex (:user_agent_string (last thomson-tests)))
["HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V401;" "HbbTV" nil "THOMSON" "LF1V401"]
uap-clj.core=>

My suggestion for the affected regexes.yaml entry is to discard $2 and use $1 for family, $3 for brand, and $4 for model. What do you think @mrjgreen ? If this looks reasonable to you, I'll generate a PR - I've already forked the repo - and see if Travis is happy with the candidate.

mrjgreen commented 9 years ago

Nice! This looks pretty good to me if the tests pass.

Cheers for sorting this out, and sorry I missed it in the first place!

commenthol commented 9 years ago

Hi, what about this one?

    - regex: '(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;Vendor/(THOM[^;]*);(?:.*SW-Version/.*(LF[^;]+);)?'
      device_repacement: '$1'
      brand_repacement: 'Thomson'
      model_repacement: '$3'

This even catches

Opera/9.80 (Linux armv7l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOM;SW-Version/V8-MT51F01-LF1V307;Cnt/DEU;Lan/bul) Presto/2.12.362 Version/12.11
russellwhitaker commented 9 years ago

@commenthol that regex does catch your own example, which isn't yet in test_device.yaml - I will add it there - and also the first HbbTV/Thomson example:

uap-clj.core=> (def alt-regex #"(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;Vendor/(THOM[^;]*);(?:.*SW-Version/.*(LF[^;]+);)?")
#'uap-clj.core/alt-regex

uap-clj.core=> (re-find alt-regex (:user_agent_string (first thomson-tests)))
["HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOMSON;SW-Version/V8-MT51F01-LF1V325;" "HbbTV" "THOMSON" "LF1V325"]

uap-clj.core=> (re-find alt-regex "Opera/9.80 (Linux armv7l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOM;SW-Version/V8-MT51F01-LF1V307;Cnt/DEU;Lan/bul) Presto/2.12.362 Version/12.11")
["HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOM;SW-Version/V8-MT51F01-LF1V307;" "HbbTV" "THOM" "LF1V307"]

It fails for the other two test_device.yaml examples however:

uap-clj.core=> (re-find alt-regex (:user_agent_string (second thomson-tests)))
nil
uap-clj.core=> (:user_agent_string (second thomson-tests))
"Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V394; en) Presto/2.10.250 Version/11.60"

uap-clj.core=> (re-find alt-regex (:user_agent_string (last thomson-tests)))
nil
uap-clj.core=> (:user_agent_string (last thomson-tests))
"Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V401; en) Presto/2.10.250 Version/11.60"

I propose using my alternate regex to catch these two (per yesterday's suggestion) and yours to catch the other two. In all 4 cases, for regularity, we'll set brand_replacement to 'Thomson'.

russellwhitaker commented 9 years ago

@commenthol please modify my "there are 3 existing test_devices examples plus yours" observation, because "there are 4 existing test_devices examples plus yours":

uap-clj.core=> (pprint (filter #(re-matches #"(?i).*THOM.*" (:user_agent_string %)) tests-device))
({:user_agent_string
  "Opera/9.80 (Linux armv7l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOMSON;SW-Version/V8-MT51F01-LF1V325;Cnt/HRV;Lan/swe; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0) Presto/2.12.362 Version/12.11",
  :family "HbbTV",
  :brand "Thomson",
  :model nil}
 {:user_agent_string
  "Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOM LF1V373; en) Presto/2.10.250 Version/11.60",
  :family "HbbTV",
  :brand "THOM",
  :model "LF1V373"}
 {:user_agent_string
  "Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V394; en) Presto/2.10.250 Version/11.60",
  :family "HbbTV",
  :brand "THOMSON",
  :model "LF1V394"}
 {:user_agent_string
  "Opera/9.80 (Linux armv6l; U; NETRANGEMMH;HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V401; en) Presto/2.10.250 Version/11.60",
  :family "HbbTV",
  :brand "THOMSON",
  :model "LF1V401"})
nil

I'll still use the same approach I mentioned in my previous comment, but will take account of the facts of "THOM number" and "THOMSON number" we're seeing so far.

russellwhitaker commented 9 years ago

Ah, but wait, there's more! OK, so I've modified a version of @commenthol 's suggested regex which covers all 5 cases itself, no need for a second regex:

(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;(Vendor/)*(THOM[^;]*?)[;\s](?:.*SW-Version/.*)*(LF[^;]+);?

So:

uap-clj.core=> (def one-true-regex #"(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;(Vendor/)*(THOM[^;]*?)[;\s](?:.*SW-Version/.*)*(LF[^;]+);?")
#'uap-clj.core/one-true-regex
uap-clj.core=> (re-find one-true-regex (:user_agent_string (nth tests-thomson 0)))
["HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOM;SW-Version/V8-MT51F01-LF1V307;" "HbbTV" "Vendor/" "THOM" "LF1V307"]
uap-clj.core=> (re-find one-true-regex (:user_agent_string (nth tests-thomson 1)))
["HbbTV/1.1.1;CE-HTML/1.0;Vendor/THOMSON;SW-Version/V8-MT51F01-LF1V325;" "HbbTV" "Vendor/" "THOMSON" "LF1V325"]
uap-clj.core=> (re-find one-true-regex (:user_agent_string (nth tests-thomson 2)))
["HbbTV/1.1.1;CE-HTML/1.0;THOM LF1V373;" "HbbTV" nil "THOM" "LF1V373"]
uap-clj.core=> (re-find one-true-regex (:user_agent_string (nth tests-thomson 3)))
["HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V394;" "HbbTV" nil "THOMSON" "LF1V394"]
uap-clj.core=> (re-find one-true-regex (:user_agent_string (nth tests-thomson 4)))
["HbbTV/1.1.1;CE-HTML/1.0;THOMSON LF1V401;" "HbbTV" nil "THOMSON" "LF1V401"]

Here's the latest suggested entry in regexes.yaml:

  - regex: '(HbbTV)/1\.1\.1.*CE-HTML/1\.\d;(Vendor/)*(THOM[^;]*?)[;\s](?:.*SW-Version/.*)*(LF[^;]+);?'
    device_replacement: '$1'
    brand_replacement: 'Thomson'
    model_replacement: '$4'

OK, this is the one I'll include in the PR I've been promising. Really.

commenthol commented 9 years ago

Thanks.

russellwhitaker commented 9 years ago

Done: https://github.com/ua-parser/uap-core/pull/53

mrjgreen commented 9 years ago

Nice one guys!