字符串与R字词典匹配

时间:2016-02-17 17:42:23

标签: r string pattern-matching string-matching

我有一个像这样的词表( wt )(3乘3)

ungrateful    mango       uncertain
hobby       prejudicial   meat
persecution   bird        honest

和单词词典( dict

persecution
overpowering
prejudicial
offense
ungrateful
uncertain
musical
murderous
detest
youth

我想用 dict 搜索 wt 中的所有单词,如果有任何单词与字典匹配,则会在单词表中给出字典单词的位置,并且将自动删除不匹配的单词。

    wt <- matrix(c("ungrateful","mango", "uncertain","hobby", "prejudicial", "meat","persecution","bird","honest"), nrow = 3, ncol = 3, byrow = TRUE)
    dict<- matrix(c(
"persecution",
"overpowering",
"prejudicial",
"offense",
"ungrateful",
"uncertain",
"musical",
"murderous",
"detest",
"youth"), nrow = 10, ncol = 1, byrow = FALSE)

for (i in 1:nrow(df)){
        for (i in 1:col(df)){
                x[i,j ] <- charmatch(df[i,j],dict_word)
        }          
}

但是当我期待像这样的输出

时,这会给出错误
     [,1] [,2] [,3]
 [1,]  5         6
 [2,]      3
 [3,]  1

我在R中很新,对语法不太了解。请帮忙。

2 个答案:

答案 0 :(得分:4)

package com.springJPA.domain; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; import com.springJPA.domain.Actels; import java.util.Collection; import javax.persistence.OneToMany; import javax.persistence.JoinColumn; /** * Entity implementation class for Entity: Reseau * */ @Entity public class Region implements Serializable { @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence_region") @SequenceGenerator(name = "id_Sequence_region", sequenceName = "ID_SEQ_REGION") private int id_region; private String nom_region; private int num_region; private static final long serialVersionUID = 1L; @OneToMany @JoinColumn(name = "Region_id_region", referencedColumnName = "id_region") private Collection<Actels> actels; public Region() { super(); } public int getId_region() { return id_region; } public void setId_region(int id_region) { this.id_region = id_region; } public String getNom_region() { return this.nom_region; } public void setNom_region(String region) { this.nom_region = region; } public int getNum_region() { return num_region; } public void setNum_region(int num_region) { this.num_region = num_region; } public Collection<Actels> getActels() { return actels; } public void setActels(Collection<Actels> param) { this.actels = param; } } 函数返回其第二个参数的匹配位置。 (如果有多个匹配,则仅返回第一个匹配的位置。)然后我们将其转换为与match矩阵的位置对应的矩阵。

wt
matrix(match(wt, dict), nrow=nrow(wt))

答案 1 :(得分:2)

与上面提到的@ epi10相同, charmatch

matrix(charmatch(wt,dict), nrow = nrow (wt))

pmatch

matrix(pmatch(wt,dict), nrow = nrow (wt))

也可以。