if (FALSE) {
# Aspect-based segmentation of a hotel review (character vector input
# returns a data.frame).
review <- paste(
"The room was clean and tidy, despite being rather basic in its furnishings.",
"The location of the hotel was really great, however.",
"We loved the proximity to both public transport and to the city's main attractions."
)
cb_absa <- qlm_codebook(
name = "Aspect-based segmentation",
instructions = paste(
"Segment the text according to the distinct aspects (topics or features).",
"Each segment will continue as long as it is part of the same aspect.",
"An aspect-based segment may be more than one sentence or may be just a",
"part of a sentence.",
"",
"Aspects in hotel reviews include: cleanliness, features, location, service,",
"and value. Return each aspect segment with its verbatim text and a short",
"aspect label."
),
schema = type_object(
aspect = type_string("Short aspect label"),
sentiment = type_enum(c("negative", "neutral", "positive"),
"Sentiment toward this aspect")
)
)
segs <- qlm_segment(review, cb_absa, model = "anthropic")
quanteda::docvars(segs)
# docid segid aspect sentiment
# 1 text1 1 cleanliness positive
# 2 text1 2 features negative
# 3 text1 3 location positive
# Corpus input preserves existing docvars
reviews_corp <- quanteda::corpus(
c(hotel_a = review),
docvars = data.frame(city = "London", stars = 4L)
)
segs_corp <- qlm_segment(reviews_corp, cb_absa, model = "anthropic")
quanteda::docvars(segs_corp)
}
Run the code above in your browser using DataLab