我有一列有句子,B栏有一些单词。我想检查词性列B字属于A列中的句子。
目前,我可以使用以下代码获得单个句子的词性:
我试图让词性对应于文本文件中的每个句子。请为此建议代码。
class LitRf{
const char* data_;
Sz size_;
public:
LitRf(const char* /*?*/ s) : data_{s},size_{sizeof(s)} { /*?*/ }
LitRf(char*) = delete;
};
答案 0 :(得分:4)
使用lapply可以标记多个句子。由于您没有提供可重现的数据,因此我创建了自己的数据。
<强>码强>
#Reproducible data - Quotes from Wuthering Heights by Emily Bronte
posText<- "I gave him my heart, and he took and pinched it to death; and flung it back to me.
People feel with their hearts, Ellen, and since he has destroyed mine, I have not power to feel for him."
library(stringr)
#Spliting into sentence based on carriage return
s <- unlist(lapply(posText, function(x) { str_split(x, "\n") }))
library(NLP)
library(openNLP)
tagPOS <- function(x, ...) {
s <- as.String(x)
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- Annotation(1L, "sentence", 1L, nchar(s))
a2 <- annotate(s, word_token_annotator, a2)
a3 <- annotate(s, Maxent_POS_Tag_Annotator(), a2)
a3w <- a3[a3$type == "word"]
POStags <- unlist(lapply(a3w$features, `[[`, "POS"))
POStagged <- paste(sprintf("%s/%s", s[a3w], POStags), collapse = " ")
list(POStagged = POStagged, POStags = POStags)
}
result <- lapply(s,tagPOS)
result <- as.data.frame(do.call(rbind,result))
输出创建一个包含两列的数据框,一列是带有标签的单词,标签用&#34; /&#34;分隔。第二列具有以句子中出现的方式排序的标签集。
<强>输出:强>
> print(result)
POStagged
1 I/PRP gave/VBD him/PRP my/PRP$ heart/NN ,/, and/CC he/PRP took/VBD and/CC pinched/VBD it/PRP to/TO death/NN ;/: and/CC flung/VBD it/PRP back/RB to/TO me/PRP ./.
2 People/NNS feel/VBP with/IN their/PRP$ hearts/NNS ,/, Ellen/NNP ,/, and/CC since/IN he/PRP has/VBZ destroyed/VBN mine/NN ,/, I/PRP have/VBP not/RB power/NN to/TO feel/VB for/IN him/PRP ./.
POStags
1 PRP, VBD, PRP, PRP$, NN, ,, CC, PRP, VBD, CC, VBD, PRP, TO, NN, :, CC, VBD, PRP, RB, TO, PRP, .
2 NNS, VBP, IN, PRP$, NNS, ,, NNP, ,, CC, IN, PRP, VBZ, VBN, NN, ,, PRP, VBP, RB, NN, TO, VB, IN, PRP, .
>
答案 1 :(得分:4)
我维护的标记器包可能会在这里使用,以简化生活。它有一些类似Python的行为,我将在下面演示:
posText<- "I gave him my heart, and he took and pinched it to death; and flung it back to me.
People feel with their hearts, Ellen, and since he has destroyed mine, I have not power to feel for him."
if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh(c(
"trinker/termco",
"trinker/tagger",
"trinker/textshape"
))
tagged <- tag_pos(split_sentence(posText)[[1]])
tagged
## 1] "I/PRP gave/VBD him/PRP my/PRP$ heart/NN ,/, and/CC he/PRP took/VBD and/CC pinched/VBD it/PRP to/TO death/NN ;/: and/CC flung/VBD it/PRP back/RB to/TO me/PRP ./."
## [2] "People/NNS feel/VBP with/IN their/PRP$ hearts/NNS ,/, Ellen/NNP ,/, and/CC since/IN he/PRP has/VBZ destroyed/VBN mine/NN ,/, I/PRP have/VBP not/RB power/NN to/TO feel/VB for/IN him/PRP ./."
c(tagged)
## [[1]]
## PRP VBD PRP PRP$ NN , CC PRP
## "I" "gave" "him" "my" "heart" "," "and" "he"
## VBD CC VBD PRP TO NN : CC
## "took" "and" "pinched" "it" "to" "death" ";" "and"
## VBD PRP RB TO PRP .
## "flung" "it" "back" "to" "me" "."
##
## [[2]]
## NNS VBP IN PRP$ NNS ,
## "People" "feel" "with" "their" "hearts" ","
## NNP , CC IN PRP VBZ
## "Ellen" "," "and" "since" "he" "has"
## VBN NN , PRP VBP RB
## "destroyed" "mine" "," "I" "have" "not"
## NN TO VB IN PRP .
## "power" "to" "feel" "for" "him" "."
select_tags(tagged, c("NN", "NNP", "NNPS", "NNS"))
## [1] "heart/NN death/NN"
## [2] "People/NNS hearts/NNS Ellen/NNP mine/NN power/NN"
as_basic(tagged)
## [1] "I/pronoun gave/verb him/pronoun my/pronoun heart/noun ,/. and/conjunction he/pronoun took/verb and/conjunction pinched/verb it/pronoun to/preposition death/noun ;/. and/conjunction flung/verb it/pronoun back/adverb to/preposition me/pronoun ./."
## [2] "People/noun feel/verb with/preposition their/pronoun hearts/noun ,/. Ellen/noun ,/. and/conjunction since/preposition he/pronoun has/verb destroyed/verb mine/noun ,/. I/pronoun have/verb not/adverb power/noun to/preposition feel/verb for/preposition him/pronoun ./."
count_tags(tagged, pretty = FALSE)
## n.tokens , . : CC IN NN NNP NNS PRP PRP$ RB TO VB VBD VBN VBP VBZ id
## 1: 22 1 1 1 3 0 2 0 0 6 1 1 2 0 4 0 0 0 1
## 2: 24 3 1 0 1 3 2 1 2 3 1 1 1 1 0 1 2 1 2
lapply(tagged, names)
## [[1]]
## [1] "PRP" "VBD" "PRP" "PRP$" "NN" "," "CC" "PRP" "VBD" "CC"
## [11] "VBD" "PRP" "TO" "NN" ":" "CC" "VBD" "PRP" "RB" "TO"
## [21] "PRP" "."
##
## [[2]]
## [1] "NNS" "VBP" "IN" "PRP$" "NNS" "," "NNP" "," "CC" "IN"
## [11] "PRP" "VBZ" "VBN" "NN" "," "PRP" "VBP" "RB" "NN" "TO"
## [21] "VB" "IN" "PRP" "."