Skip to content

Proposal: r.tokenize #1099

Closed
Closed

Description

@coffeemug
r.expr('hello, world and universe').tokenize(' ', ',')
// returns ['hello', 'world', 'and', 'universe']

The use case for this is as follows. If the new .group proposal passes review, it will make map/reduce functionality incredibly accessible and it's very much worth marketing and writing about. A common hello world map/reduce example is to count the frequency of words in documents, which requires tokenization. We could do it with javascript, but it would make for a much less friendly example. Hence, r.tokenize would be extremely nice to have.

Activity

  1. mlucy commented on Jun 30, 2013

    @mlucy
    Member

    Could we call this split? That's what Ruby and Python call it, I think.

  2. coffeemug commented on Jun 30, 2013

    @coffeemug
    ContributorAuthor

    I was going to say split originally, but thought it might be too general and we might want to use it elsewhere. But if other languages use it, I'm cool with split.

  3. neumino commented on Jun 30, 2013

    @neumino
    Member

    JavaScript uses split too.

  4. jdoliner commented on Jun 30, 2013

    @jdoliner
    Contributor
    • All of these languages take a string with multiple separators rather than being variadic which we should mimic. i.e. split(' ,') not split(' ', ',').
    • split() uses whitespace characters as its separator.
    • All of these functions in host languages take a length limit as a second argument although it does slightly different things. I'm not sure I totally understand where that's useful but maybe we should just include it because it's easy and if it's in all 3 of them chances are it's useful for something.
    • I think it's a good idea to add the inverse of this function join (might want to change that name though) as well. It's incredibly easy because boost has a builtin join method and I could see someone storing their data in a split form because that's convenient for processing but wanting to join the strings back together at some point too. Also string.split().join(' ') is a nice way to strip out tabs and newlines.
  5. neumino commented on Jun 30, 2013

    @neumino
    Member

    👍 for @jdoliner's four bullet points.

  6. coffeemug commented on Jul 1, 2013

    @coffeemug
    ContributorAuthor

    I'm cool with @jdoliner's API.

  7. wmrowan commented on Jul 1, 2013

    @wmrowan
    Contributor

    I actually have the word count example @coffeemug mentioned in the map reduce blog post I'm working on. At first I had to use JS but with the new .match the example has become much better. This would make it better still.

  8. mlucy commented on Jul 2, 2013

    @mlucy
    Member

    @jdoliner: that isn't how split works in Ruby. In Ruby if you write .split(", ") it means "split on the string ', '", not "split on either the string ',' or ' '". Instead, split takes a regexp to handle that case:

    irb(main):3898:0> "a b, c,d".split(", ")
    ["a b", "c,d"]
    irb(main):3899:0> "a b, c,d".split(/[, ]/)
    ["a", "b", "", "c", "d"]

    Slava's proposed behavior seems more intuitive to me. Here's what I would propose:

    • split() does a normal whitespace split
    • split(', ') means "split on the string ', '"
    • split(',', ' ') means "split on either the string ',' or ' '"
    • We skip the length limit, it doesn't seem terribly useful to me.
    • We add .join.
  9. jdoliner commented on Jul 2, 2013

    @jdoliner
    Contributor

    I'm cool with that split behavior. I actually think that's what python does
    and I just misread it. I'd really like to have the length limit since it's
    present in all of the languages we target and there really doesn't seem to
    be a high cost to it.

    On Mon, Jul 1, 2013 at 7:16 PM, Michael Lucy [email protected]:

    @jdoliner https://github.com/jdoliner: that isn't how split works in
    Ruby. In Ruby if you write .split(", ") it means "split on the string ',
    '", not "split on either the string ',' or ' '". Instead, split takes a
    regexp to handle that case:

    irb(main):3898:0> "a b, c,d".split(", ")["a b", "c,d"]irb(main):3899:0> "a b, c,d".split(/[, ]/)["a", "b", "", "c", "d"]

    Slava's proposed behavior seems more intuitive to me. Here's what I would
    propose:

    • split() does a normal whitespace split
    • split(', ') means "split on the string ', '"
    • split(',', ' ') means "split on either the string ',' or ' '"
    • We skip the length limit, it doesn't seem terribly useful to me.
    • We add .join.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/1099#issuecomment-20322687
    .

  10. mlucy commented on Jul 2, 2013

    @mlucy
    Member

    The only cost is that it makes argument parsing a littler hard, because we have a variable number of strings followed by an optional number, but that's probably fine.

  11. markenzie commented on Jul 2, 2013

    @markenzie

    A recent time I needed a limit to a split is when I had previously determined the position of a certain string and needed to split everything leading up to it and then the next section(s) would use different split parameters.

  12. neumino commented on Jul 2, 2013

    @neumino
    Member

    @markenzie I think the limit here is about the number of element returned, not until which character split should work (at least that's what the limit is in Python and JavaScript).
    To split until a certain character, I would do something like

    string.slice(0, <limit>).split()
    

    Note: It looks like we don't such a slice function for string...

    I don't expect split to be variadic. If people want to split with comma and string, they should use a regex, that's what they are for.

    The limit (the number of elements returned) is useful. Suppose I want to create a slug according to the first words of a text. I can do

    text.split(" ").limit(3).join()
    ===== vs =====
    text.split(" ", 3).join()
    

    I would naturally prefer the second syntax for this problem for performance reason.

    +1 for .join

  13. AtnNn commented on Jul 2, 2013

    @AtnNn
    Member

    In python and other languages, the numeric argument to split is the number of splits to do. The last element of the returned array is the remainder of the strings.

    "foo:bar:baz".split(":", 1) == ['foo', 'bar:baz']
    

    It is not the same as doing .split.limit

  14. mlucy commented on Jul 3, 2013

    @mlucy
    Member

    Making split take both regular expressions and strings would be difficult for us because we don't have a distinct regex type; we just have one function that accepts a string and interprets it as a regex. (On the other hand, I suppose we could just make split always take a regex.)

  15. jdoliner commented on Jul 3, 2013

    @jdoliner
    Contributor

    Or just have a regex optarg.

  16. 29 remaining items

  17. neumino commented on Dec 17, 2013

    @neumino
    Member

    I would rather not have split being polymorphic on stream. It just makes the API more confusing.

  18. coffeemug commented on Dec 17, 2013

    @coffeemug
    ContributorAuthor

    Ah, I see.

    I disagree with @neumino. I think as a general rule, we should distribute over sequences unless there is a compelling reason not to for the specific operation. You learn about it once and it's immensely useful.

    For example if downcase could be distributed over sequences, you could write lambda x: x['text'].downcase().split() and lambda x: x['text'].split().downcase(), which I think is really nice.

  19. neumino commented on Dec 17, 2013

    @neumino
    Member

    Splitting an array in 2 is something that is defined (not in ReQL, but in general).
    So having split to recurse in streams and split each element if they are a string is confusing..

    Can someone point out an example where splitting on a stream is useful? All the examples I have in my mind use a group/map after, in which case users can split inside the group/map.

    Also here's an example that is confusing

    r.table("foo")("text").split().contains("hello")
    

    is not the same as

    r.table("foo").map( r.row("text").split().contains("hello") )
    
  20. mlucy commented on Dec 17, 2013

    @mlucy
    Member

    @coffeemug -- the rule right now is basically "operations on objects distribute over streams". We don't distribute any other string operations over streams (although maybe we should).

  21. neumino commented on Dec 17, 2013

    @neumino
    Member

    Also about the argument

    You learn about it once and it's immensely useful.

    I think it's not a valid argument because it still delivers a bad experience.
    .
    I recently saw someone trying to do

    r.table("foo").filter( r.row.hasFields("field").not() ).run(...)
    

    And was stuck with

    r.table("foo").hasFields("field").run(..)
    

    Because not() doesn't recurse in a stream.

  22. mlucy commented on Dec 17, 2013

    @mlucy
    Member

    @coffeemug -- I think @jdoliner's point was that if you split on whitespace, "word," and "word" would be counted as different words. If we want to fix that, we could add splitting by regular expressions, which wouldn't be insanely difficult.

  23. jdoliner commented on Dec 17, 2013

    @jdoliner
    Contributor

    @mlucy I think that's really due to the fact that 90% of streams are objects because that's the only thing you can store in a table.

  24. coffeemug commented on Dec 17, 2013

    @coffeemug
    ContributorAuthor

    @neumino -- ok, that's a good point.

    the rule right now is basically "operations on objects distribute over streams"

    Fair enough. I changed my mind, I'm now not sure whether automatically distributing string operations over sequences is the right thing to do, so I think we should make that a separate proposal (if someone feels strongly enough about doing that).

  25. mlucy commented on Jan 17, 2014

    @mlucy
    Member

    I think we should go with the original plan and just mimic Python's split. Any objections?

  26. AtnNn commented on Jan 17, 2014

    @AtnNn
    Member

    +1 for mimicking Python.

    Haskell's overcomplicated split library is an example of what happens when you try to write a split function that works for every use case.

  27. coffeemug commented on Jan 17, 2014

    @coffeemug
    ContributorAuthor

    No objections on my part.

  28. mlucy commented on Feb 21, 2014

    @mlucy
    Member

    This is back up in review 1240 by @AtnNn .

  29. mlucy commented on Feb 22, 2014

    @mlucy
    Member

    This is in next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions