// search=a word to count
//sent = paragragh or sentence or big string to search in
public int getFrequency(String search, String sent)
{
sent = sent.trim();
search = search.trim();
int index = 0;
int end = 0;
int count = 0;
while(index != -1)
{
index = sent.indexOf(search,end);
end = index + search.length();
if(index!=-1)
{
if(((index == 0) && !isAfterChar(sent,end)) || ((index > 0) && !isBeforeChar(sent,index)) || ((index>0) && !isAfterChar(sent,end) && !isBeforeChar(sent,index)) || ((index==0) && sent.length()==end))
++count;
}
}
return count;
}
public boolean isBeforeChar(String st,int index)
{
boolean flag = false;
if(index>0)
{
if(((((int)st.charAt(index-1))>64) && (((int)st.charAt(index-1))<91)) || ((((int)st.charAt(index-1))>96) && (((int)st.charAt(index-1))<123)))
{
flag = true;
}
}
return flag;
}
public boolean isAfterChar(String st,int end)
{
boolean flag = false;
if(st.length() > end)
{
if(((((int)st.charAt(end))>64) && (((int)st.charAt(end))<91)) || ((((int)st.charAt(end))>96) && (((int)st.charAt(end))<123)))
{
flag = true;
}
}
return flag;
}
Cheers :)
|
My Blog Title
|