2015-05-16 42 views
2

我有一個字符串在「?」之後提取文本

x <- "Name of the Student? Michael Sneider" 

我想提取「邁克爾·斯奈德」出來的。

我用:

str_extract_all(x,"[a-z]+") 
str_extract_all(data,"\\?[a-z]+") 

但不能提取的名稱。

回答

2

我想這應該有助於

substr(x, str_locate(x, "?")+1, nchar(x)) 
3

試試這個:

sub('.*\\?(.*)','\\1',x) 
1

str_match在這種情況下,更有助於

str_match(x, ".*\\?\\s(.*)")[, 2] 
#[1] "Michael Sneider" 
2
x <- "Name of the Student? Michael Sneider" 

sub(pattern = ".+?\\?" , x , replacement = '') 
2

採取寬鬆的措辭優勢的問題,我們可以走開路並使用自然語言處理從字符串中提取所有名稱:

library(openNLP) 
library(NLP) 
# you'll also have to install the models with the next line, if you haven't already 
# install.packages('openNLPmodels.en', repos = 'http://datacube.wu.ac.at/', type = 'source') 

s <- as.String(x) # convert x to NLP package's String object 

# make annotators 
sent_token_annotator <- Maxent_Sent_Token_Annotator() 
word_token_annotator <- Maxent_Word_Token_Annotator() 
entity_annotator <- Maxent_Entity_Annotator() 

# call sentence and word annotators 
s_annotated <- annotate(s, list(sent_token_annotator, word_token_annotator)) 
# call entity annotator (which defaults to "person") and subset the string 
s[entity_annotator(s, s_annotated)] 
## Michael Sneider 

矯枉過正?大概。但有趣的是,實際上並非所有難以實現的東西。