Bonjour,
je suis en train de calculer la distance entre deux termes x et y = nombre de mots entre x et y
cette partie que je dois l'ajouter à mon code,
Tout d'abord je dois vérifier que les deux termes existent dans la même phrase et ça c'est bon
Aussi l'extraction de la chaîne entre les deux termes (le code au dessous que j'ai trouvé dans le net) et le calcul de nombre de mots aussi j'ai pu le faire:
Mais ce code pose problème si les deux termes x et y n'ont pas des mots entre eux c'est dire juste une espace "x y" et aussi si y se situent avant x dans la phrase
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 package DistanceEntreMots; public class CalculDistance { public static String subStringBetween(String sentence, String before, String after) { int startSub = CalculDistance.subStringStartIndex(sentence, before); int stopSub = CalculDistance.subStringEndIndex(sentence, after); String newWord = sentence.substring(startSub, stopSub); return newWord; } public static int subStringStartIndex(String sentence, String delimiterBeforeWord) { int startIndex = 0; String newWord = ""; int x = 0; for (int i = 0; i < sentence.length(); i++) { newWord = ""; if (sentence.charAt(i) == delimiterBeforeWord.charAt(0)) { startIndex = i; for (int j = 0; j < delimiterBeforeWord.length(); j++) { try { if (sentence.charAt(startIndex) == delimiterBeforeWord.charAt(j)) { newWord = newWord + sentence.charAt(startIndex); } startIndex++; } catch (Exception e) { } } if (newWord.equals(delimiterBeforeWord)) { x = startIndex; } } } return x; } public static int subStringEndIndex(String sentence, String delimiterAfterWord) { int startIndex = 0; String newWord = ""; int x = 0; for (int i = 0; i < sentence.length(); i++) { newWord = ""; if (sentence.charAt(i) == delimiterAfterWord.charAt(0)) { startIndex = i; for (int j = 0; j < delimiterAfterWord.length(); j++) { try { if (sentence.charAt(startIndex) == delimiterAfterWord.charAt(j)) { newWord = newWord + sentence.charAt(startIndex); } startIndex++; } catch (Exception e) { } } if (newWord.equals(delimiterAfterWord)) { x = startIndex; x = x - delimiterAfterWord.length(); } } } return x; } }![]()
![]()
j'ai essayé de faire une condition de comparer l'indice du premier mot avec l'indice du deuxieme mot:
cette condition n'a pas résolu le problème
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 StringTokenizer tokenizer = new StringTokenizer (phrase," "); String start = m1; String end = m2; int indice1 =0; int indice2 =0; int ind1=0;int ind2=0; while (tokenizer.hasMoreTokens ()) { indice1++;indice2++; String word = tokenizer.nextToken(); if(start.contains(word)){ ind1=indice1; } if(end.contains(word)){ ind2=indice2; } } if(ind1<ind2){ System.out.println(ind1+"<"+ind2); String str =CalculDistance.subStringBetween(phrase, start, end); System.out.println(str); }![]()
exemple: dans une une phrase:" officer a to spot letter ." si x= a et y = letter
le résultat:
mais dans le cas où existe une autre "a" qui se situe dans la phrase comme ceci: "his officer a to spot letter test a test."2<5
to spot
au lieu d'afficher même résultat que le précédent mais il n'affiche rien :/
Partager