xml - 使用 Go 解析 RDF 三倍。一些项目错误地传递了正则表达式

标签 xml regex go freebase

在解析 Freebase RDF 数据转储时,我尝试仅根据实体的标题和文本解析某些实体。我正在使用正则表达式来匹配标题和文本,即使它们不匹配,返回 false,内容仍在传递。

我如何决定将什么转换成 XML 是属性 ["/type/object/name"] 不为空或者它是否包含 @en 并且属性 ["/common/document/text"] 是不为空。

什么定义空?我注意到,通过打印所有名称( properties["/type/object/name"] )和文本( properties["/common/document/text"] ),我注意到其中一些只是“[ ]”。我不想要那些。我想要的是那些不是那个并且在名称中包含 @en ( properties["/type/object/name"] ) 的。文本 ( properties["/common/document/text"] ) 不会有 @en,所以如果它不是 "[]"并且它对应的名称有 @en,那么该实体应该被转换为 XML。

当我运行我的代码时,我使用正则表达式来查看它是否匹配和不匹配那些东西,我看到那些东西被忽略了,那些“空实体”仍在转换为 XML。

这是我从终端抓取的一些输出:

<card>
<title>"[]"</title>
<image>"https://usercontent.googleapis.com/freebase/v1/image"</image>
%!(EXTRA string=/american_football/football_player/footballdb_id)<text>"[]"</text>
<facts>
    <fact property="/type/object/type">/type/property</fact>
    <fact property="/type/property/schema">/american_football/football_player</fact>
    <fact property="/type/property/unique">true</fact>
    <fact property="http://www/w3/org/2000/01/rdf-schema#label">"footballdb ID"@en</fact>
    <fact property="/type/property/expected_type">/type/enumeration</fact>
    <fact property="http://www/w3/org/1999/02/22-rdf-syntax-ns#type">http://www/w3/org/2002/07/owl#FunctionalProperty</fact>

    <fact property="http://www/w3/org/2000/01/rdf-schema#domain">/american_football/football_player</fact>

    <fact property="http://www/w3/org/2000/01/rdf-schema#range">/type/enumeration</fact>
 </facts>
 </card>

下面是我的代码,我做错了什么?它不应该匹配正则表达式然后不写它写的东西吗?

func validTitle(content []string) bool{
    for _, v := range content{
         emptyTitle, _ := regexp.MatchString("\"[]\"", v)
         validTitle, _ := regexp.MatchString("^[A-Za-z0-9][A-Za-z0-9_-]*$", v)
         englishTitle, _ := regexp.MatchString("@en", v)
         if (!validTitle || !englishTitle) && !emptyTitle{
              return false
         }
    }
    return true 
 }

 func validText(content []string) bool{
      for _, v := range content{
          emptyTitle, _ := regexp.MatchString("\"[]\"", v)
          validText, _ := regexp.MatchString("^[A-Za-z0-9][A-Za-z0-9_-]*$", v)
          if !validText && !emptyTitle{
             return false
          }
      }
      return true
 }

 func processTopic(id string, properties map[string][]string, file io.Writer){
      if validTitle(properties["/type/object/name"]) &&       validText(properties["/common/document/text"]){
           fmt.Fprintf(file, "<card>\n")
           fmt.Fprintf(file, "<title>\"%s\"</title>\n", properties["/type/object/name"])
           fmt.Fprintf(file, "<image>\"%s\"</image>\n", "https://usercontent.googleapis.com/freebase/v1/image", id)
           fmt.Fprintf(file, "<text>\"%s\"</text>\n", properties["/common/document/text"])
           fmt.Fprintf(file, "<facts>\n")
           for k, v := range properties{
                for _, value := range v{
                    fmt.Fprintf(file, "<fact property=\"%s\">%s</fact>\n", k, value)
                }
           }
           fmt.Fprintf(file, "</facts>\n")
           fmt.Fprintf(file, "</card>\n")
      }
 }

最佳答案

您的正则表达式无效,如果您检查错误,它会告诉您确切原因:

error parsing regexp: missing closing ]: `[]"`

regexp.MatchString("\"[]\"", v)
// should be
regexp.MatchString(`"\[\]"`, v)

另外既然你多次使用它,你应该在函数外编译它并使用它,例如:

var (
    emptyRe   = regexp.MustCompile(`"\[\]"`)
    titleRe   = regexp.MustCompile("^[A-Za-z0-9][A-Za-z0-9_-]*$")
    englishRe = regexp.MustCompile("@en")
)

func validTitle(content []string) bool {
    for _, v := range content {
        if emptyRe.MatchString(v) || !(englishRe.MatchString(v) || titleRe.MatchString(v)) {
            return false
        }
    }
    return true
}

此行需要 1 个值作为输入,但您给了它两个值:

fmt.Fprintf(file, "<image>\"%s\"</image>\n", 
            "https://usercontent.googleapis.com/freebase/v1/image", // this matches the %s
             id, // this doesn't
) 

应该是

fmt.Fprintf(file, "<image>\"%s/%s\"</image>\n", "https://usercontent.googleapis.com/freebase/v1/image", id)

关于xml - 使用 Go 解析 RDF 三倍。一些项目错误地传递了正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26390686/

相关文章:

python - 将 XML 字符串分成三个 float

regex - 有没有办法使用 Go.Regexp 匹配常量字符串以外的所有内容?

mysql - 如何强制执行自定义 MySQL 列格式

multithreading - 在 amd64 上拆分堆栈是不必要的

go - redis中mget()的时间复杂度是多少?

php - LoadXML 文件太大

python - 用有序字典替换字典推导式和defaultdict

go - golang接口(interface)如何转换数组?

ios - 如何在 xcode 7 中添加 libxml2.dylib

java - 字节缓冲区性能