update attribute a element in arraylist on java?
I have a class
Class TextChunks extends Token {
ArrayList<Token> arrt = new ArrayList<Token>();
}
extent fron class :
class Token {
String s;
int frequency = 1 ;// Tern frequency in TextChunk
}
Now in token i have arraylist token , i want to update attribute frequency of token in Texchunks when have more than one tokens same .
For clearly a give a example :
Texchunks :" in particular in domain and range in some "
So have 8 token : in,particular,in,domain,and,range,in,some
i want update attribute frequency for token : in this example when i get attribute frequency of token "in" must return 3
it mean when i call : get frequency of Texchunks when dislay :
in 3
particular 1
in 3
domain 1
and 1
range 1
in 3
some 1
here my code :
public TextChunks updateFrequencyOfTokenInTextChunks (TextChunks tc) throws CloneNotSupportedException {
TextChunks result = (TextChunks) tc.clone();
for (int i =0 ; i< result.arrt.size() ; i++ ){
int j=i+1;
if (result.arrt.get(i).compareTwoToken(res开发者_如何学Goult.arrt.get(j))== true )
{
// help here how to update attribute result.arrt.get(i)
// and result.arrt.get(J) = ++ and
}
}
return tc;
}
Here is method compare two token
public boolean compareTwoToken(Token tk){
if(this.s.toLowerCase().trim().equals(tk.s.toLowerCase()))
return true;
return false;
}
Your incomplete algorithm doesn't work because j
doesn't find in previous position of i
.
A posible solution could be:
updateFrequencyOfTokenInTextChunks
method:public static void updateFrequencyOfTokenInTextChunks (TextChunks tc) { Hashtable<String,Integer> visited = new Hashtable<String,Integer>(); for (Token token : tc.arrt){ if (visited.containsKey(token.s)) { token.frequency = visited.get(token.s); } else { int n = count(token, tc); visited.put(token.s, n); token.frequency = n; } } }
My solution doesn't return any type, because I understand the update (updateFrequencyOfTokenInTextChunks
) should modify the parameter (TextChunks tc
) and no return a clone.
count
auxiliar method:private static int count(Token t, TextChunks tc) { int cont = 0; for (Token token : tc.arrt) { if ( t.compareTwoToken(token) ) { cont++; } } return cont; }
Good luck!
First of all, your loop is subtly broken. You use:
for (int i =0 ; i< result.arrt.size() ; i++ )
But then you reference an item at index i+1
(through j
), so you already have an off-by-one error. You need to change the loop to go up to result.arrt.size()-1
. Secondly, it is not necessary to explicitly compare a boolean value to true; it is redundant and makes things unnecessary cluttered and confusing and is generally considered poor style. Thirdly, unless you are planning to modify the result
object, your cloning is completely unnecessary and wasteful. Now, to answer your question, save the elements in variables to make your life easier, and then just update the field like the following:
ArrayList<Token> tokens = tc.aart;
for (int i = 0; i < tokens.size() - 1; i++ ){
Token current = tokens.get(i);
Token next = tokens.get(i+1);
if ( current.compareTwoToken(next) ){
current.frequency = /* new value of frequency */
next.frequency = /* new value of frequency */
}
}
Note though, that since the field frequency
has not been declared public, this would need to be executed by code that has package access to the Token class. Also, you reference compareTwoToken
, but in the snippet you have posted, you do not provide such a function.
You can use this code
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TokenFrequency {
public static void main(String[] args) {
String text = "in particular in domain and range in some";
String[] tokens = text.split(" ");
System.out.println(Arrays.toString(tokens));
Set<String> uniqueTokens = new LinkedHashSet<String>(Arrays.asList(tokens)) ;
List<Token> list = new ArrayList<Token>();
for(String uniqueToken : uniqueTokens){
String regex = "\\b" + uniqueToken + "\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
int frequency = 0;
while (matcher.find()) {
frequency++;
}
Token token = new Token();
token.frequency = frequency;
token.s = uniqueToken;
list.add(token);
}
System.out.println(list);
}
}
Here frequency of word is the number of times it is present in the sentence. You can use a small code for that
String sent ="in particular in domain and range in some";
StringTokenizer str = new StringTokenizer(sent, " ");
ArrayList<String> list = new ArrayList<String>();
while(str.hasMoreTokens()){
//System.out.println(str.nextToken());
list.add(str.nextToken());
}
The above code is just to get particular word which you have done in your way. The code below is helpful in finding the frquency:
HashSet<String> st = new HashSet<String>();
st.addAll(list);
ArrayList<Token> arrt = new ArrayList<Token>();
for(String s:st){
Token token = new Token();
token.s=s;
token.frequency=Collections.frequency(list, s);
arrt.add(token);
}
Thus Collections.frequency() method will give you the frequency of each word in a collection.
精彩评论