Affichage hyponyms

Résolu/Fermé
azhe Messages postés 8 Date d'inscription dimanche 3 juin 2012 Statut Membre Dernière intervention 8 septembre 2012 - 24 juil. 2012 à 19:15
azhe Messages postés 8 Date d'inscription dimanche 3 juin 2012 Statut Membre Dernière intervention 8 septembre 2012 - 31 juil. 2012 à 22:52
Bonjour,


Salut,
J'entraine de faire un projet sur netbeans (java) avec wordnet, j'ai un problème avec ce code :
private void demonstrateTreeOperation(IndexWord word) throws JWNLException {
// Get all the hyponyms (children) of the first sense of <var>word</var>
PointerTargetTree hyponyms = PointerUtils.getInstance().getHyponymTree(word.getSense(1));
System.out.println("Hyponyms of \"" + word.getLemma() + "\":"); hyponyms.print();
}
Qu'il permet d'afficher l' hyponyms sur la console.
SVP aide moi comment s'affiche l' hyponyms dans un fichier .txt, et est ce que si possible d'afficher graphiquement sous forme d'un arbre.
Et MERCI d'avance.

5 réponses

KX Messages postés 16752 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 août 2024 3 018
Modifié par KX le 24/07/2012 à 23:31
Je ne suis pas sûr d'avoir compris tout ce que tu voulais faire, mais d'un point de vue "théorique" chaque nom ne devrait avoir qu'un seul hyponyme, donc récursivement tu n'obtiendras pas un arbre, mais une liste !
Si tu veux un arbre il va falloir considérer les hyperonymes du mot considéré. Je ne connais pas WordNet, j'ignore donc si cela est possible directement, ou s'il faut à chaque fois itérer tout le dictionnaire pour identifier les hyperonymies en considérant les mots qui ont pour hyponyme le mot recherché. Mais si le but est de faire un arbre complet d'hyperonymes (ou plus vraisemblablement une forêt) il va falloir un bon algorithme pour ne pas faire exploser la combinatoire...La confiance n'exclut pas le contrôle
0
azhe Messages postés 8 Date d'inscription dimanche 3 juin 2012 Statut Membre Dernière intervention 8 septembre 2012
25 juil. 2012 à 02:00
ce code affiche ça (à l'aide de la méthode hyponyms.print(); ) dans la console :

Hyponyms of "car":



[PointerTargetTreeNode: [Synset: [Offset: 2853224] [POS: noun] Words: car, auto, automobile, machine, motorcar -- (4-wheeled motor vehicle; usually propelled by an internal combustion engine; "he needs a car to get to work")] null [Has Parent? true] [Has Children? true] [Has Pointers? false]]
[PointerTargetTreeNode: [Synset: [Offset: 2608716] [POS: noun] Words: ambulance -- (a vehicle that takes people to and from hospitals)] [PointerType: [Label: hyponym] [Key: ~] Applies To: noun, verb] [Has Parent? true] [Has Children? true] [Has Pointers? false]]
[PointerTargetTreeNode: [Synset: [Offset: 3279543] [POS: noun] Words: funny_wagon -- (an ambulance used to transport patients to a mental hospital)] [PointerType: [Label: hyponym] [Key: ~] Applies To: noun, verb] [Has Parent? false] [Has Children? false] [Has Pointers? false]]
[PointerTargetTreeNode: [Synset: [Offset: 2715796] [POS: noun] Words: beach_wagon, station_wagon, wagon, beach_waggon, station_waggon, waggon -- (a car that has a long body and rear door with space behind rear seat)] [PointerType: [Label: hyponym] [Key: ~] Applies To: noun, verb] [Has Parent? true] [Has Children? true] [Has Pointers? false]]
[PointerTargetTreeNode: [Synset: [Offset: 4041413] [POS: noun] Words: shooting_brake, estate_car -- (another name for a station wagon)] [PointerType: [Label: hyponym] [Key: ~] Applies To: noun, verb] [Has Parent? false] [Has Children? false] [Has Pointers? false]]
[PointerTargetTreeNode: [Synset: [Offset: 2820476] [POS: noun] Words: bus, jalopy, heap -- (a car that is old and unreliable; "the fenders had fallen off that old bus")] [PointerType: [Label: hyponym] [Key: ~] Applies To: noun, verb] [Has Parent? true] [Has Children? false] [Has Pointers? false]]
[PointerTargetTreeNode: [Synset: [Offset: 2826514] [POS: noun] Words: cab, hack, taxi, taxicab -- (a car driven by a person whose job is to take passengers where they want to go in exchange for money)] [PointerType: [Label: hyponym] [Key: ~] Applies To: noun, verb] [Has Parent? true] [Has Children? true] [Has Pointers? false]]
[PointerTargetTreeNode: [Synset: [Offset: 3345513] [POS: noun] Words: gypsy_cab -- (a taxicab that cruises for customers although it is licensed only to respond to calls)] [PointerType: [Label: hyponym] [Key: ~] Applies To: noun, verb] [Has Parent? false] [Has Children? false] [Has Pointers? false]]
[PointerTargetTreeNode: [Synset: [Offset: 3628748] [POS: noun] Words: minicab -- (a minicar used as a taxicab)] [PointerType: [Label: hyponym] [Key: ~] Applies To: noun, verb] [Has Parent? false] [Has Children? false] [Has Pointers? false]]
....

et wordnet affiche comme ça :


2 of 5 senses of car

Sense 1
car, auto, automobile, machine, motorcar -- (4-wheeled motor vehicle; usually propelled by an internal combustion engine; "he needs a car to get to work")
=> ambulance -- (a vehicle that takes people to and from hospitals)
=> funny wagon -- (an ambulance used to transport patients to a mental hospital)
=> beach wagon, station wagon, wagon, beach waggon, station waggon, waggon -- (a car that has a long body and rear door with space behind rear seat)
=> shooting brake, estate car -- (another name for a station wagon)
=> bus, jalopy, heap -- (a car that is old and unreliable; "the fenders had fallen off that old bus")
=> cab, hack, taxi, taxicab -- (a car driven by a person whose job is to take passengers where they want to go in exchange for money)
=> gypsy cab -- (a taxicab that cruises for customers although it is licensed only to respond to calls)
=> minicab -- (a minicar used as a taxicab)
....

mais comment l'affiche dans un fichier .txt par exemple ?
0
KX Messages postés 16752 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 août 2024 3 018
25 juil. 2012 à 12:00
Il faut te référer à la documentation, ce qui va dépendre de quelle implémentation tu utilises. Le site de WordNet référence 9 implémentations Java (cf. liste).

Vu que dans ton code tu as une JWNLException, je vais considérer que tu utilises extJWNL dont la documentation est ici, même si tu utilises des méthodes qui n'y sont pas référencés (IndexWord.getSense(int) par exemple).

Pour enregistrer les résultats dans un fichier il ne faudra pas utiliser la méthode print de PointerTargetTree, mais une de ses autres méthodes (il faudra que tu cherches précisément laquelle) qui te renverras une liste, voici un exemple(je ne l'ai pas testé)

private void demonstrateTreeOperation(IndexWord word, File file)
{
    FileOutputStream out = new FileOutputStream(file);
    out.write(("Hyponyms of \"" + word.getLemma() + "\":").getBytes());

    for (Synset synset : word.getSenses()) // Pour chaque sens du mot
    for (PointerTargetNodeList node : PointerUtils.getInstance().getHyponymTree(synset).toList()) // Pour chaque noeud de l'arbre
        node.print(out); // Imprimer le résultat dans le fichier

    out.close();
}
0
azhe Messages postés 8 Date d'inscription dimanche 3 juin 2012 Statut Membre Dernière intervention 8 septembre 2012
25 juil. 2012 à 16:32
salut,
Merci KX, je vais tester ça.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
azhe Messages postés 8 Date d'inscription dimanche 3 juin 2012 Statut Membre Dernière intervention 8 septembre 2012
31 juil. 2012 à 22:52
j'ai utilise extJWNL ça marche bien, et voici le code:

package exp;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.extjwnl.JWNLException;
import net.sf.extjwnl.data.*;
import net.sf.extjwnl.data.list.PointerTargetTreeNode;
import net.sf.extjwnl.data.list.PointerTargetTreeNodeList;
import net.sf.extjwnl.dictionary.Dictionary;

import java.io.*;
import java.util.*;



public class test {
private static final String defaultConfig = "file_properties.xml";

//private static final DecimalFormat df = new DecimalFormat("00000000");
static BufferedWriter fichier=null;
test(){
try{
fichier = new BufferedWriter( new FileWriter("monfichier.txt"));

fichier.newLine();

arbre();
fichier.close();
}
catch(Exception e){}

}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JWNLException, FileNotFoundException {
// TODO code application logic here
new test();
}
void arbre() throws JWNLException, FileNotFoundException {
//initialize(new FileInputStream("file_properties.xml"));

Dictionary d = null;
File config = new File(defaultConfig);
d = Dictionary.getInstance(new FileInputStream(config));

IndexWordSet set = d.lookupAllIndexWords("car");
// Turn it into an array of IndexWords
IndexWord[] ws = set.getIndexWordArray();

POS p = ws[0].getPOS();

IndexWord iw = d.lookupIndexWord(p,"car");
if (null != iw) {
System.out.println("\nHypernyms of " + p.getLabel() + " " + iw.getLemma());
try {
fichier.newLine();
fichier.write("\nHypernyms of " + p.getLabel() + " " + iw.getLemma());
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
tracePointers(iw, PointerType.HYPERNYM, PointerUtils.INFINITY,0,true,true,true,true,true);// needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys);
}

}


private static void tracePointers(IndexWord iw, PointerType pt, int depth, int needSense, boolean needGloss, boolean needLex, boolean needOffset, boolean needSenseNum, boolean needSenseKeys) throws JWNLException {
Map<Synset, PointerTargetTreeNodeList> ptrs = new HashMap<Synset, PointerTargetTreeNodeList>();
for (Synset synset : iw.getSenses()) {
PointerTargetTreeNodeList list = PointerUtils.makePointerTargetTreeList(synset, pt, depth);
if (null != list && 0 < list.size()) {
ptrs.put(synset, list);
}
}
if (0 < ptrs.size()) {
System.out.println("\n" + ptrs.size() + " of " + iw.getSenses().size() + " senses of " + iw.getLemma());
try {
fichier.newLine();
fichier.write("\n" + ptrs.size() + " of " + iw.getSenses().size() + " senses of " + iw.getLemma());
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0; i < iw.getSenses().size(); i++) {
if (0 == needSense || i == (needSense - 1)) {
Synset synset = iw.getSenses().get(i);
PointerTargetTreeNodeList list = ptrs.get(synset);
if (null != list) {
System.out.println("\nSense " + (i + 1));
try {
fichier.newLine();
fichier.write("\nSense " + (i + 1));
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
printSense("", synset, needGloss, needLex, needOffset, needSenseNum, needSenseKeys);
for (PointerTargetTreeNode node : list) {
printPointerTargetTree("\t", node, needGloss, needLex, needOffset, needSenseNum, needSenseKeys);
}
}
}
}
}
}



private static void printSense(String lead, Synset synset, boolean needGloss, boolean needLex, boolean needOffset, boolean needSenseNum, boolean needSenseKeys) throws JWNLException {
System.out.print(lead+"=> ");
try {
fichier.write(lead + "=> ");
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}

/* if (needOffset) {
System.out.print("{" + df.format(synset.getOffset()) + "} ");
}
if (needLex) {
System.out.print("<" + synset.getLexFileName() + "> ");
}*/
List<Word> words = synset.getWords();
for (int i = 0; i < words.size(); i++) {
Word word = words.get(i);
System.out.print(word.getLemma());
try {
fichier.write(word.getLemma());
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
/* if (needLex && 0 < word.getLexId()) {
System.out.print(word.getLexId());
}
if (needSenseNum) {
System.out.print("#" + (getSenseNo(word) + 1));
}*/
/* if (needSenseKeys) {
System.out.print(" [" + word.getSenseKey() + "]");
}*/
if (i < words.size() - 1) {
System.out.print(", ");
}
}
if (needGloss) {
System.out.print(" -- (" + synset.getGloss() + ")");
try {
fichier.write(" -- (" + synset.getGloss() + ")");
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println();
try {
fichier.newLine();
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
}



private static void printPointerTargetTree(String lead, PointerTargetTreeNode hypTree, boolean needGloss, boolean needLex, boolean needOffset, boolean needSenseNum, boolean needSenseKeys) throws JWNLException {
printSense(lead, hypTree.getSynset(), needGloss, needLex, needOffset, needSenseNum, needSenseKeys);
if (null != hypTree.getChildTreeList()) {
for (PointerTargetTreeNode node : hypTree.getChildTreeList()) {
printPointerTargetTree(lead + "\t", node, needGloss, needLex, needOffset, needSenseNum, needSenseKeys);
}
}
}


private static int getSenseNo(Word word) throws JWNLException {
IndexWord iw = word.getDictionary().getIndexWord(word.getPOS(), word.getLemma());
for (int i = 0; i < iw.getSenses().size(); i++) {
if (iw.getSenses().get(i).getOffset() == word.getSynset().getOffset()) {
return i;
}
}
return -1;
}
}


ET Merci ;)
0