Stéphanie C
Stéphanie C

Reputation: 829

How to convert any object in character in R?

I use wordnet library and I want to get the wordnet index of a token for future purpose.

library(wordnet)
filter <- getTermFilter("ExactMatchFilter", "proud", TRUE)
terms <- getIndexTerms("ADJECTIVE", 1, filter)
terms


[1] "Java-Object{Lemma: proud  POS: adj  Tag-Sense-Count: 2\nList of Synsets (2)\n  #1: 1817423\n  #2: 1231908\nList of Pointers (4)\n  #1: ! (Antonym)\n  #2: & (Similar to)\n  #3: ^ (Also see)\n  #4: = (Attribute)}"

terms is a Java-Object with some methods :

names(terms[[1]])
[1] "getLemma()"         "getSynsets()"       "getPointers()"      "getPartOfSpeech()"  "getSynsetCount()"   "getTagSenseCount()" "getPointerCount()" 
[8] "toString()"         "compareTo("         "compareTo("         "wait("              "wait("              "wait()"             "equals("           
[15] "hashCode()"         "getClass()"         "notify()"           "notifyAll()"    

but they don't all work, it seems

getLemma(terms[[1]])
[1] "proud"

toString(terms[[1]])
Erreur dans as.character.default(<S4 object of class "jobjRef">) : 
pas de méthode pour convertir automatiquement cette classe S4 en vecteur

I want to extract the code, here "#1: 1817423" from terms[[1]], I thought I could use substring or regex but so far I just can't convert the object in string so that I can work with it. Any idea ?

str(terms[[1]]) 
Formal class 'jobjRef' [package "rJava"] with 2 slots
..@ jobj  :<externalptr> 
..@ jclass: chr "java/lang/Object"

unclass(terms[[1]])
<S4 Type Object>
attr(,"jobj")
<pointer: 0x000000000cb81050>
attr(,"jclass")
[1] "java/lang/Object"

Upvotes: 0

Views: 804

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226547

It looks like the jobjRef class acts like (or may actually be) a reference class, which uses a slightly different object-orientation system. In this world, the members/elements/slots of an object are methods that can be called via object$method() to run the corresponding method on the object, e.g. terms[[1]]$toString() (However, I don't know/can't explain why getLemma(terms[[1]]) works and toString(terms[[1]]) doesn't ...)

This is the basic idea but I'm not sure I got all of the details right. If someone else wants to correct my terminology either here or in an alternative answer, that would be fine with me ...

Upvotes: 5

Related Questions