-1

ファイルのリストで特定のキーワード (txt で指定) を検索しようとしています。正規表現を使用して、ファイル内のキーワードの出現を検出して置き換えています。以下は、検索するために渡すカンマ区切りのキーワードです。

library(stringi)
txt <- "automatically got activated,may be we download,network services,food quality is excellent"

たとえば、「自動的にアクティブ化された」を検索し、automatically_got_activated...「may be we download」を「may_be_we_download」などに置き換えます。

txt <- "automatically got activated,may be we download,network services,food quality is excellent"

for(i in 1:length(txt)) {
    start <- head(strsplit(txt, split=" ")[[i]], 1) #finding the first word of the keyword 
    n <- stri_stats_latex(txt[i])[4]        #number of words in the keyword

    o <- tolower(regmatches(text, regexpr(paste0(start,"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,",
        n-1,"}"),text,ignore.case=TRUE)))   #best match for keyword for the regex in the file 

    p <- which(!is.na(pmatch(txt, o)))      #exact match for the keywords
}
4

1 に答える 1

1

これはあなたが探しているものかもしれないと思います。

> txt <- "automatically got activated,may be we download,network services,food quality is excellent"

検索する文の作成されたベクトル:

> searchList <- c('This is a sentence that automatically got activated',
                  'may be we download some music tonight',
                  'I work in network services',
                  'food quality is excellent every time I go',
                  'New service entrance',
                  'full quantity is excellent')

作業を行う関数:

replace.keyword <- function(text, toSearch)
{
    kw <- unlist(strsplit(txt, ','))
    gs <- gsub('\\s', '_', kw)
    sapply(seq(kw), function(i){
      ul <- ifelse(grepl(kw[i], toSearch),
                   gsub(kw[i], gs[i], toSearch),
                   "")
      ul[nzchar(ul)]
    })
}

結果:

> replace.keyword(txt, searchList)
# [1] "This is a sentence that automatically_got_activated"
# [2] "may_be_we_download some music tonight"              
# [3] "I work in network_services"                         
# [4] "food_quality_is_excellent every time I go"   

それがあなたのために働くかどうか私に知らせてください。

于 2014-05-22T14:50:54.267 に答える