|
It filters out empty strings - word.trim() removes whitespace, so it returns either a full word with no spaces, or if there was only blank spaces, trim() returns the empty string "". Basically the string version of "!= null"
|
# ? Nov 21, 2022 18:32 |
|
|
# ? Jun 8, 2024 02:47 |
|
Tesseraction posted:It filters out empty strings - word.trim() removes whitespace, so it returns either a full word with no spaces, or if there was only blank spaces, trim() returns the empty string "". ok, that makes sense. I'm looking at stream commands, and I'm not sure what equates to an "if" statement.
|
# ? Nov 21, 2022 18:33 |
|
Going by the example provided in the doc I linked:code:
(As an aside, I'm assuming you recognise from the capitalisation that Transaction.GROCERY is an enumeration.)
|
# ? Nov 21, 2022 18:36 |
|
Tesseraction posted:Going by the example provided in the doc I linked: I thought enumerations were not able to be created, but inbuilt? Where does GROCERY come from?
|
# ? Nov 21, 2022 19:09 |
|
Hmm, added filter, but got an error. The method filter((<no type> word) -> {}) is undefined for the type String my code is: document.stream().peek(word->word.toLowerCase().filter(word->word.equals(word.trim)));
|
# ? Nov 21, 2022 19:12 |
|
samcarsten posted:I thought enumerations were not able to be created, but inbuilt? Where does GROCERY come from? This is different from an enumerable like an Array. It's usually referred to as an Enum to keep confusion to a minimum. Documentation is here.
|
# ? Nov 21, 2022 19:14 |
|
samcarsten posted:Hmm, added filter, but got an error. Hmm, I don't think this captures the spirit of what the teacher wants you to do. I assume the goal is to count the frequency of words appearing? To apply a function to every element in the stream you want to use map(), and then filter doesn't get you a wordcount, it merely filters out all elements that don't match your predicate. It would probably be better to create a wrapper function for map() that 1) performs the toLowerCase() function (and if necessary a trim() and filtering out of "") 2) then interacts with wordFrequency
|
# ? Nov 21, 2022 19:34 |
|
hmm, trying that, but still getting an error. document.stream().peek(word->word.toLowerCase().peek(word->word.trim)) The method peek((<no type> word) -> {}) is undefined for the type String
|
# ? Nov 21, 2022 19:51 |
|
Peek appears to more be a debugging function for seeing what the stream looks like at a certain point in operation, at least according to the documentation https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-
|
# ? Nov 21, 2022 19:55 |
|
You practically never need to use peek for anything. It will only evaluate the function's side effects, nothing permanent will happen. The key bits for your Stream are map, filter and groupingBy.
|
# ? Nov 21, 2022 19:58 |
|
Tesseraction posted:Hmm, I don't think this captures the spirit of what the teacher wants you to do. I assume the goal is to count the frequency of words appearing? Right, but what commands do I use to do that?
|
# ? Nov 21, 2022 19:59 |
|
Same error. document.stream().filter(word->word.toLowerCase().filter(word->word.trim)); The method filter((<no type> word) -> {}) is undefined for the type String
|
# ? Nov 21, 2022 20:03 |
|
samcarsten posted:Same error. You're not closing your parentheses in the right place with your stream operations.
|
# ? Nov 21, 2022 20:06 |
|
New error. document.stream().filter(word->word.toLowerCase()).filter(word->word.trim()); Type mismatch: cannot convert from String to boolean
|
# ? Nov 21, 2022 20:12 |
|
samcarsten posted:New error. Hint: Having the documentation at the ready is like having a map handy when you want to get to a specific destination.
|
# ? Nov 21, 2022 20:16 |
|
cool, so replacing it with map worked, but I'm not sure of the lambda statement that is equivalent to: !"".equals(word.trim)
|
# ? Nov 21, 2022 20:23 |
|
Something along the lines of t -> !"".equals(t.trim()) Lamba functions are basically a "find/replace" for whatever is on the left of the arrow. For everything in the list given, call it (in this case) t and then apply everything to the right of the arrow.
|
# ? Nov 21, 2022 20:31 |
|
samcarsten posted:cool, so replacing it with map worked, but I'm not sure of the lambda statement that is equivalent to: It's not really. Honestly, I'm not sure why the filter works in this case. But assuming you want to filter out words consisting of only whitespace, the filter should actually compare it to an empty string. So you could use the bit from the loop directly ( filter(word -> !"".equals(word.trim())) ), but since that looks ugly as gently caress to me, I'd rather you do filter(word -> !word.trim().equals("")).
|
# ? Nov 21, 2022 20:35 |
|
I suspect it was deliberately put that way to confuse the students, or at least give them pause to think what it means.
|
# ? Nov 21, 2022 20:37 |
|
ok, so my current statement is: .map((Integer cnt) -> cnt.wordFrequency.get(word)) but it's giving me the error: Type mismatch: cannot convert from Function<Integer,? extends R> to Function<? super String,? extends R>
|
# ? Nov 21, 2022 20:40 |
|
That's an interesting lambda. When .map() is called the only thing it's aware of is the stream, anything inside the brackets has to be defined in terms the stream. Currently you're telling it to parse through the stream and as it does so, declare an integer called cnt but don't instantiate it, then push that into a lambda function... and ask for that integer to then access a wordFrequency (which isn't part of the Integer class) and from that wordFrequency call the get() function. I think you might benefit from learning the principles of lambda calculus (at least to the extent it's used in Java / other languages). The idea is say you have a array [1,2,3,4] that applying "lambda a -> a * a" will output [1,4,9,16] because for each element in the array, it is now called a, and on the other side of the arrow, the function says that you should return the value of a * a.
|
# ? Nov 21, 2022 20:50 |
|
samcarsten posted:ok, so my current statement is: Honestly, I think you need to go back a bit and try and understand the syntax before you write any more code. A stream is basically a pipeline of commands. You usually start from a list, then add commands which either perform a function for all members of the list, filter stuff out etc. The stuff you've been doing to this point. So stuff like.. list.map(variable -> variable.doStuff()).filter(variable -> variable > tooSmall) etc. The part that you're trying to replicate is this... code:
What you want to do after the filter is something called a collect. You can look it up in the documentation. But the syntax for that command is very fiddly, so you should definitely look up some practical examples for it. You can even google a word frequency example which does exactly what is wanted in your homework. But because the syntax is fiddly, and you don't have a good grasp of java syntax yet, I'd really take a breather, find a syntax cheat sheet and take your time with it, so you know exactly what you're trying to do when you write code. Edit: Oh, and being able to read the documentation is a skill of it's own. To get better at it, you probably want to look up some methods that are familiar to you and that you have a good grasp of and see how the documentation presents those. Kuule hain nussivan fucked around with this message at 20:53 on Nov 21, 2022 |
# ? Nov 21, 2022 20:50 |
|
Kuule hain nussivan posted:Honestly, I think you need to go back a bit and try and understand the syntax before you write any more code. A stream is basically a pipeline of commands. You usually start from a list, then add commands which either perform a function for all members of the list, filter stuff out etc. The stuff you've been doing to this point. So stuff like.. Right, but collect is a terminal operation and there's still more to go.
|
# ? Nov 21, 2022 21:09 |
|
samcarsten posted:Right, but collect is a terminal operation and there's still more to go. Is there? If you break the loop into bits, it's... 1. Make lowercase 2. Filter out empties 3. Count frequencies
|
# ? Nov 21, 2022 21:15 |
|
Kuule hain nussivan posted:Is there? If you break the loop into bits, it's... Ugh, this is all so confusing. So I only need one filter statement, despite the inner loop?
|
# ? Nov 21, 2022 21:21 |
|
samcarsten posted:Ugh, this is all so confusing. So I only need one filter statement, despite the inner loop? Yup, eveything after the first if, the trim check, is handled by the collect.
|
# ? Nov 21, 2022 21:24 |
|
Ok, I think I am close, but there is one more error. My code is: wordFrequency = document.stream().map(word->word.toLowerCase()).map(word->word.trim()).filter(word -> !"".equals(word.trim())).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); The error is: Type mismatch: cannot convert from Map<String,Long> to Map<String,Integer>
|
# ? Nov 21, 2022 21:35 |
|
samcarsten posted:Ok, I think I am close, but there is one more error. My code is: Not bad. The second map is superfluous and can be removed, since you do the trim in the filter. Try casting (changing the type) of the count to Integer by doing (Integer) Collectors.counting().
|
# ? Nov 21, 2022 21:40 |
|
Kuule hain nussivan posted:Not bad. The second map is superfluous and can be removed, since you do the trim in the filter. Try casting (changing the type) of the count to Integer by doing (Integer) Collectors.counting(). Cannot cast from Collector<Object,capture#1-of ?,Long> to Integer
|
# ? Nov 21, 2022 21:42 |
|
samcarsten posted:Cannot cast from Collector<Object,capture#1-of ?,Long> to Integer
|
# ? Nov 21, 2022 21:53 |
|
God these polymorphic parameterisations. Back in my day everything was an object and you prayed it worked. Look at the API it shows counting() is static <T> Collector<T,?,Long> So maybe Collectors<?,?,Integer>.counting()? If that doesn't work I'll sleep on it as well, since you say you've got two weeks for this bastard. I've not messed around with generics in Java and Monday night is not the best time to wrap my head around it.
|
# ? Nov 21, 2022 22:33 |
|
Tesseraction posted:God these polymorphic parameterisations. Back in my day everything was an object and you prayed it worked. Okay, did that, but its giving me an error indication on the comma between function and collectors. wordFrequency = document.stream().map(word->word.toLowerCase()).filter(word -> !"".equals(word.trim())).collect(Collectors.groupingBy(Function.identity(), Collectors<?,?,Integer>.counting()));
|
# ? Nov 21, 2022 22:54 |
|
Yeah I tried throwing it in Eclipse and it's yelling at me about how it's not actually generic. I'll look at this properly tomorrow.
|
# ? Nov 21, 2022 23:06 |
|
Tesseraction posted:Yeah I tried throwing it in Eclipse and it's yelling at me about how it's not actually generic. I'll look at this properly tomorrow. Maybe it's just me being optimistic about this class and the intended lesson, but I suspect we may be over-complicating the solution by getting too deep into generics. How is wordFrequency defined? From looking at the error I'd speculate it's something along the lines of Map<String, Integer>, right? So the first question I'd ask (since we see that the issue is the Map wants an Integer but is getting a Long) is can we change the definition of wordFrequency so that it supports the simple solution of leveraging Collectors.counting()? If we can't what other methods does the Collectors class have that we may be able to leverage in order to produce an Integer in the returned Collector?
|
# ? Nov 22, 2022 00:26 |
|
Tesseraction posted:God these polymorphic parameterisations. Back in my day everything was an object and you prayed it worked. The only way I could get this to work easily was to use: Java code:
|
# ? Nov 22, 2022 01:14 |
|
ada shatan posted:Maybe it's just me being optimistic about this class and the intended lesson, but I suspect we may be over-complicating the solution by getting too deep into generics. Yeah, you're right that for homework this feels overly complicated. That might be why they apparently get two weeks for this one, but could well be we're missing the forest. Might be worth samcarsten showing us the full homework brief including document. Kilson posted:The only way I could get this to work easily was to use: Ooh, nice.
|
# ? Nov 22, 2022 10:17 |
|
The full home work brief is less than a paragraph. Individual Term Project - Frequent Words & Spelling Analysis Modify the program SCFWStreamsFX.java to remove the loops and replace them with stream statements in the btMostFrequentWords.setOnAction() and btMisspelledWordCount.setOnAction() methods. Note that "Most Frequent Words" feature, as given, works correctly; you can use the given version to test against your modified version. Same with the "MIsspelled Word Count" feature.
|
# ? Nov 22, 2022 15:09 |
|
samcarsten posted:The full home work brief is less than a paragraph. Can you share the source for SCFWStreamsFX.java?
|
# ? Nov 22, 2022 17:01 |
|
ada shatan posted:Can you share the source for SCFWStreamsFX.java? sure. let me throw it on google drive. https://drive.google.com/file/d/1hi6k0AxSMG-nJ-odpJ0uwObswwCPgHBH/view?usp=share_link
|
# ? Nov 22, 2022 20:45 |
|
|
# ? Jun 8, 2024 02:47 |
|
samcarsten posted:sure. let me throw it on google drive. The relevant file is here: https://controlc.com/1d390e17 password is "goonhomework" It looks like you'd be well within the guidelines of the assignment to just change the map to use a Long, but if that isn't your intent Kilson's solution looks great. I'm sure myself and others would feel a lot more comfortable if you explained how Kilson's solution works before you hand it in as your own, but you do what works for you.
|
# ? Nov 22, 2022 21:45 |