有一种简单的方法可以将拉丁字母转换为希腊字母,使用stringiR 的包,它依赖于ICU的音译器:
library(stringi)
stri_trans_general("abcd", "latin-greek")
Run Code Online (Sandbox Code Playgroud)
是否有类似的简单方法将拉丁语转换为古希腊语(αβγδ)而不是希腊语(ἀβκδ)?
编辑 df和dict
我有一个包含句子的数据框:
df <- data_frame(text = c("I love pandas", "I hate monkeys", "pandas pandas pandas", "monkeys monkeys"))
Run Code Online (Sandbox Code Playgroud)
还有一个包含单词及其对应分数的字典:
dict <- data_frame(word = c("love", "hate", "pandas", "monkeys"),
score = c(1,-1,1,-1))
Run Code Online (Sandbox Code Playgroud)
我想在其中添加一个"得分"列df,将每个句子的得分相加:
预期成绩
text score
1 I love pandas 2
2 I hate monkeys -2
3 pandas pandas pandas 3
4 monkeys monkeys -2
Run Code Online (Sandbox Code Playgroud)
更新
以下是迄今为止的结果:
Akrun的方法
建议1
df %>% mutate(score = sapply(strsplit(text, ' '), function(x) with(dict, sum(score[word %in% x]))))
Run Code Online (Sandbox Code Playgroud)
请注意,要使此方法起作用,我必须使用data_frame()创建df,dict而不是 …
我想知道为什么我通过使用gsub和stringi获得两个不同的输出字符串.元字符是否"." 不包括stringi中的新行?stringi读"逐行"吗?
顺便说一句,我没有找到任何方法来执行stringi的"正确"替换,所以我需要在这里使用gsub.
string <- "is it normal?\n\nhttp://www.20minutes.fr"
> gsub(" .*?http"," http", string)
[1] "is http://www.20minutes.fr"
> stri_replace_all_regex(string, " .*?http"," http")
[1] "is it normal?\n\nhttp://www.20minutes.fr"
Run Code Online (Sandbox Code Playgroud) 在RI中可以\\1用来引用捕获组。但是,在使用stringi软件包时,此操作无法按预期工作。
library(stringi)
fileName <- "hello-you.lst"
(fileName <- stri_replace_first_regex(fileName, "(.*)\\.lst$", "\\1"))
[1] "1"
Run Code Online (Sandbox Code Playgroud)
预期输出:hello-you。
在文档中,我找不到与该问题有关的任何内容。
我正在阅读/学习最伟大的正则表达式伎俩我们说我们想要的东西除非......使用(*SKIP)(*FAIL).好的,所以我把它用于下面的玩具示例,它在基础R中工作,但在stringi中有以下错误.我是否需要使用stringi做一些不同的事情来使语法工作?
x <- c("I shouldn't", "you should", "I know", "'bout time")
pat <- '(?:houl)(*SKIP)(*FAIL)|(ou)'
grepl(pat, x, perl = TRUE)
## [1] FALSE TRUE FALSE TRUE
stringi::stri_detect_regex(x, pat)
## Error in stringi::stri_detect_regex(x, pat) :
## Syntax error in regexp pattern. (U_REGEX_RULE_SYNTAX)
Run Code Online (Sandbox Code Playgroud) 我可以使用perl参数gsub来改变单个子表达式的大小写.例如,如果我想找到一个小写i后跟和撇号或字符串结尾(这里多余),我可以这样做:
gsub("(\\bi(\\b|'))", "\\U\\1", "i am able to move do it as i'm going to.", perl = TRUE)
## [1] "I am able to move do it as I'm going to."
Run Code Online (Sandbox Code Playgroud)
注意I并且I'm是上限但it不是.
如果base和stringi使用不同的正则表达式引擎,我如何使用stringi做同样的事情(是否可能).
stri_replace_all_regex("i am able to move do it as i'm going to.", "(\\bi(\\b|'))", "\\U$1")
## [1] "1 am able to move do it as 1'm going to."
Run Code Online (Sandbox Code Playgroud) stringi我正在尝试在 R 最近更新到最新版本后安装软件包。但是,我遇到了这个错误:
\nRun Code Online (Sandbox Code Playgroud)* installing *source* package \xe2\x80\x98stringi\xe2\x80\x99 ...\n** package \xe2\x80\x98stringi\xe2\x80\x99 successfully unpacked and MD5 sums checked\nchecking for local ICUDT_DIR... icu55/data\nchecking for R_HOME... /usr/local/Cellar/r/3.4.3/lib/R\nchecking for R... /usr/local/Cellar/r/3.4.3/lib/R/bin/R\nchecking for R >= 3.1.0 for C++11 use... yes\nchecking for R < 3.4.0 for CXX1X flag use... no\nchecking for cat... /bin/cat\nchecking for gcc... /usr/local/opt/llvm/bin/clang -fopenmp\nchecking whether the C compiler works... no\nconfigure: error: in `/private/var/folders/y5/m7pd62wn3939vyqxygrd_ff80000gn/T/RtmpNBCRW2/R.INSTALL8ec750ee123/stringi':\nconfigure: error: C compiler cannot create executables\nSee `config.log' for more details\nERROR: configuration failed for package \xe2\x80\x98stringi\xe2\x80\x99\n* removing \xe2\x80\x98/usr/local/lib/R/3.4/site-library/stringi\xe2\x80\x99\nWarning …
给定一个英国邮政编码的"模板",如"A9 9AA",其中"A"是一个字母占位符,"9"是一个数字占位符,我要生成随机字符串邮编像"H8 4GB".字母可以是任何大写字母,编号从0到9.
因此,如果模板是"AA9A 9AA",那么我想要像"WC1A 9LK"这样的字符串.我现在忽略了生成"真实"的邮政编码,所以如果"WC1A"是一个有效的外向代码我就不会感到困扰.
我试图从stringi包中获取函数来解决问题,但问题似乎是替换或匹配模板中的"A"只会替换第一个替换,例如:
stri_replace_all_fixed("A9 9AA",c("A","A","A"), c("X","Y","Z"), vectorize_all=FALSE)
[1] "X9 9XX"
Run Code Online (Sandbox Code Playgroud)
所以它不会用替换向量中的每个元素替换每个"A"(但这是设计的).
也许stringi我已经错过了某些内容或基础R - 我想将它保存在这些包中,所以我不会膨胀我正在做的事情.
蛮力方法是拆分模板,做替换,将结果粘贴在一起,但我想看看是否有更快,自然的矢量化解决方案.
总结一下:
foo("A9 9AA") # return like "B6 5DE"
foo(c("A9 9AA","A9 9AA","A9A 9AA")) # returns c("Y6 5TH","D4 8JH","W0Z 3KQ")
Run Code Online (Sandbox Code Playgroud)
这是一个非矢量化版本,它依赖于构造表达式并对其进行评估......
random_pc <- function(fmt){
cc = gsub(" ",'c(" ")',gsub("9","sample(0:9,1)",gsub("A","sample(LETTERS,1)",strsplit(fmt,"")[[1]])))
paste(eval(parse(text=paste0("c(",paste(cc,collapse=","),")"))),collapse="")
}
> random_pc("AA9 9AA")
[1] "KO6 1AY"
Run Code Online (Sandbox Code Playgroud) 我正在试图抓一个网页
library(RCurl)
webpage <- getURL("https://somewebpage.com")
webpage
<div class='CredibilityFacts'><span id='qZyoLu'><a class='answer_permalink'
action_mousedown='AnswerPermalinkClickthrough' href='/someurl/answer/my_id'
id ='__w2_yeSWotR_link'>
<a class='another_class' action_mousedown='AnswerPermalinkClickthrough'
href='/ignore_url/answer/some_id' id='__w2_ksTVShJ_link'>
<a class='answer_permalink' action_mousedown='AnswerPermalinkClickthrough'
href='/another_url/answer/new_id' id='__w2_ksTVShJ_link'>
class(webpage)
[1] "character"
Run Code Online (Sandbox Code Playgroud)
我试图提取所有的href值,但只有当它在answer_permalink类之前.
这个的输出应该是
[1] "/someurl/answer/my_id" "/another_url/answer/new_id"
Run Code Online (Sandbox Code Playgroud)
/ignore_url/answer/some_id应该被忽略,因为它先于another_class而不是answer_permalink类.
现在,我正在考虑采用正则表达式的方法.我认为像这样的东西可以用于正则表达式stri_extract_all
class='answer_permalink'.*href='
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的.
我能以什么方式实现这一目标?而且,除了正则表达式之外,R中还有一个函数,我们可以像Javascript一样逐个提取元素吗?
我正在尝试安装最新版本的 stringi (1.1.6)。这是不可能的,因为最近更新了 Xcode。收到的错误configure: error: C compiler cannot create executables在这里有完整的输出:
Installing package into ‘/usr/local/lib/R/3.4/site-library’
(as ‘lib’ is unspecified)
trying URL 'http://cran.rstudio.com/src/contrib/stringi_1.1.6.tar.gz'
Content type 'application/x-gzip' length 3647049 bytes (3.5 MB)
==================================================
downloaded 3.5 MB
* installing *source* package ‘stringi’ ...
** package ‘stringi’ successfully unpacked and MD5 sums checked
checking for local ICUDT_DIR... icu55/data
checking for R_HOME... /usr/local/Cellar/r/3.4.0_1/R.framework/Resources
checking for R... /usr/local/Cellar/r/3.4.0_1/R.framework/Resources/bin/R
checking for R >= 3.1.0 …Run Code Online (Sandbox Code Playgroud)