stdunbar wrote:Ultimately you're trying to do natural language parsing - a very difficult thing to do with any programming language. You might consider breaking the sentence into individual words (see String.split() for details) and then find out how many words match. However, that will mean that two sentences that may or may not be related but have a high number of the same words would be considered similar even if they are not.
the problem comes when the user might write a sentence with 100 ways. i cant compare all his words using loops or for methods.
i thought to make two variables
string end= " "
String Word= end+Str.charAt() + end
but how can i number each variable and make it look at the second string with no problem
view plaincopy to clipboardprint?
1. import java.io.*;
2. class Ex11
3. {
4.
5. public static void main(String[] args)
6. {
7. Console console=System.console();
8. System.out.println("please write a sentence"+" ");
9. String Str;
10. Str=console.readLine();
11.
12.
13. System.out.println("please write another sentence"+" ");
14. String Str2;
15. Str2=console.readLine();
16.
17.
18.
19. for(int i=0; i<Str.length(); i++){
20. for(int j=0; j><Str2.length(); j++){
21. if(Str.charAt(i)==Str2.charAt(j)){
22. char Str3;
23. Str3 += Str.charAt(i);
24. System.out.println(Str3);
25.
26. }
27. }
28.
29.
30. }
31.
32.
33.
34.
35.
36. }
37. }
38.
39.
40.
import java.io.*; class Ex11 { public static void main(String[] args) { Console console=System.console(); System.out.println("please write a sentence"+" "); String Str; Str=console.readLine(); System.out.println("please write another sentence"+" "); String Str2; Str2=console.readLine(); for(int i=0; i><Str.length(); i++){ for(int j=0; j><Str2.length(); j++){ if(Str.charAt(i)==Str2.charAt(j)){ char Str3; Str3 += Str.charAt(i); System.out.println(Str3); } } } } } >
i tried.
i found that if you add all the characters, you get no where.
Example
lets say i remove all the " " from Sentence A and B and compare between them.
i went to school yesterday
i went to a hospital yesterday
that would be:
iwenttoschoolyesterday
iwenttoahospitalyesterday
the computer wont identify similary charactaristics, cause the length of the string is different from the start. you wont be able to identify yesterday as similar, cause their character number is different.
The second thing that i discovered..
is that you cant isolate words and loop each one, that looping might continue for ever, cause i dont know how long a sentence can be
what i thought that would work is if i tried to define a variable called "Word"
but if " " character, i cant tell java to stop counting words before that space, can i ?
is there a way i could make java count all characters till " ", and then add all that character string to a variable called Word. then make java pass onto another String (after " ") find its characters till it reaches " ",
and add it to a word?
another variable
Words=Word+Words
is this direction possible? can it be done?
This message was edited 2 times. Last update was at 06/29/2010 00:58:57
|