与えられた英文を、各単語に分けてabc順に並べ替えスラッシュで区切る関数を、Rで書いたことがあった。例えば、
- this is an example sentence
- an / example / is / sentence / this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
narabe <- function(n){ | |
rawn <- unlist(strsplit(n, " ")) | |
numberedn <- rawn[order(rawn)] | |
output <- numberedn[1] | |
for(i in 2:(length(rawn)-1)){ | |
output <- paste(output, numberedn[i], sep=" / ") | |
} | |
return(paste(output, numberedn[length(rawn)], sep=" / ")) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
narabe <- function(n){ | |
d <- unlist(strsplit(n, " ")) | |
d <- sort(d) | |
d <- paste(d, collapse=" / ") | |
return(d) | |
} |
以前はウンウン唸りながら1~2時間くらいかけて書いたものが、今では3分くらいで書けるように。私はR (を始めプログラミング) の専門的な訓練を受けたことはないが、本やインターネットのおかげで、自分にとって十分役にたつスクリプトが書けるようになってきた。このエントリーも誰かの役に立てば嬉しい。
なお、使い方は:
- 上のスクリプトをコピーandペースト、そしてエンターキーを押す。
- narabe("") と入力し (全て半角文字)、ダブルクオーテーションマークの間に変換したい文字列を入力する。エンターキーを押す。 例 narabe("this is an example sentence")
- 変換された文字列が返ってくる。
0 件のコメント:
コメントを投稿