简体   繁体   中英

editing sentence start <s> and end </s> with R for prediction

I'm building an NLP model to predict the next word in R. So, for a 3 sentences corpus:

a<-"i like cheese"

b<-"the dog like cat"

c<-"the cat eat cheese"

I want it to become:

>a
 "<.s> i like cheese <./s>"

>b
 "<.s> the dog like cat <./s>"

>c
 "<.s> the cat eat cheese <./s>"

Is there a simpler way to do this than:

a<-Unlist(strsplit(a, " "))
a[1]<-"<.s>"
a[length(a)]<-"./s>"
a<-paste(a, collapse = " ")
> a 
 "<.s> i like cheese <./s>"

您只是连接字符串,所以这应该可以工作:

 a <- paste("<.s>", a, "<./s>")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM