###############################################
# FILE: funcs
# SUBJ: comment
# AUTH: Kruthika
# CREATED: 2023-11-20 17:41:25
# MODIFIED:
###############################################

@DECL

AddAttrs(L("node"))
{
    L("concept")=pnvar(L("node"),"con");
    if(pnvar(L("node"),"housenum"))
    addstrval(L("concept"),"housenum",pnvar(L("node"),"housenum"));
    if(pnvar(L("node"),"streetnum"))
    addstrval(L("concept"),"streetnum",pnvar(L("node"),"streetnum"));
    if(pnvar(L("node"),"streetname"))
    addstrval(L("concept"),"streetname",pnvar(L("node"),"streetname"));
    if(pnvar(L("node"),"streettype"))
    addstrval(L("concept"),"streettype",pnvar(L("node"),"streettype"));
    if(pnvar(L("node"),"streetsuff"))
    addstrval(L("concept"),"streetsuff",pnvar(L("node"),"streetsuff"));
    if(pnvar(L("node"),"city"))
    addstrval(L("concept"),"city",pnvar(L("node"),"city"));
    if(pnvar(L("node"),"state"))
    addstrval(L("concept"),"state",pnvar(L("node"),"state"));
    if(pnvar(L("node"),"pincode"))
    addstrval(L("concept"),"pincode",pnvar(L("node"),"pincode"));
    if(!pnvar(L("node"),"type"))
    addstrval(L("concept"),"type","individual");
    else
    {
       addstrval(L("concept"),"type",pnvar(L("node"),"type"));
       if(pnvar(L("node"),"hcnum"))
       addstrval(L("concept"),"hcnum",pnvar(L("node"),"hcnum"));
        if(pnvar(L("node"),"routenum"))
       addstrval(L("concept"),"routenum",pnvar(L("node"),"routenum"));
       if(pnvar(L("node"),"boxnum"))
       addstrval(L("concept"),"boxnum",pnvar(L("node"),"boxnum"));
    }   
    if(pnvar(L("node"),"country"))
    addstrval(L("concept"),"country",pnvar(L("node"),"country"));    
}
FindCity(L("node"),L("add"))
{
    L("pre")=pnprev(L("node"));
    while(L("pre"))
    {
        if(strisalpha(pnvar(L("pre"),"$text")))
        {
        pnmakevar(L("pre"),"city",1);
        if(!pnvar(L("add"),"city"))
        pnmakevar(L("add"),"city",pnvar(L("pre"),"$text"));
        L("pre")=0;
        }
        else if(strisdigit(pnvar(L("pre"),"$text")))
        L("pre")=0;
        else
        L("pre")=pnprev(L("pre"));
    }
}

StreetName(L("node"),L("add"))
{
    L("pre")=pnprev(L("node"));
    L("count")=0;
        if(strisalpha(pnvar(L("pre"),"$text")))
        {
        pnmakevar(L("pre"),"streetname",1);
         if(!pnvar(L("add"),"streetname"))
        pnmakevar(L("add"),"streetname",pnvar(L("pre"),"$text"));
        L("pre")=pnprev(L("pre"));
        }
        

    while(L("pre")&&L("count")<3)
    {
    if(L("pre")&&strisdigit(pnvar(L("pre"),"$text")))
        {
         if(!pnvar(L("add"),"streetnum"))
        pnmakevar(L("pre"),"streetnum",1);
        if(!pnvar(L("add"),"streetnum"))
        pnmakevar(L("add"),"streetnum",pnvar(L("pre"),"$text"));
        L("pre")=0;
        }
        else
        L("pre")=pnprev(L("pre"));
        L("count")=L("count")+1;
    }
}
CheckName(L("con"),L("out"))
{
    G("attrList") = findattrs(L("con"));

    while(nextattr(G("attrList")))
    {
        G("attrName") = attrname(G("attrList"));
         L("out") <<"\t\t\""<<G("attrName")<<"\":";
         G("valList") = attrvals(G("attrList"));
         G("valName") = getstrval(G("valList"));
                if(getstrval(G("valList"))){
                L("out") <<"\""<<getstrval(G("valList"))<<"\",\n";
                }
            # L("out") <<"\""<<getstrval(G("valList"))<<"\"\n";
            G("attrList") = nextattr(G("attrList"));
            L("count")++;
    }
    G("attrName") = attrname(G("attrList"));
         L("out") <<"\t\t\""<<G("attrName")<<"\":";
         G("valList") = attrvals(G("attrList"));
         G("valName") = getstrval(G("valList"));
                if(getstrval(G("valList"))){
                L("out") <<"\""<<getstrval(G("valList"))<<"\"\n";
}
}
XMLOut(L("out")){
        L("out")<<"{\n\t\"addresses\""<<":   [\n";
     L("con") = down(G("addresses"));
         L("count")=0;
     while(next(L("con")))
     {  
         L("out")<<"\t\t{"<<"\n";
         L("out") <<"\t\t\""<<"add-id"<<"\":"<<"\""+str(L("count"))+"\""+",\n";
CheckName(L("con"),L("out"));
 L("out")<<"\t},"<<"\n";
        L("con") = next(L("con"));
         L("count")++;
     }
     L("out")<<"\t\t{"<<"\n";
         L("out") <<"\t\t\""<<"add-id"<<"\":"<<"\""+str(L("count"))+"\""+",\n";
CheckName(L("con"),L("out"));
 L("out")<<"\t}"<<"\n";
        L("con") = next(L("con"));
        L("out")<<"\t]\n}";
        # L("out")<<"//The fields are:"<<"\n";
        # L("out")<<"//tld - top level domain"<<"\n";
        # L("out")<<"//cd - country domain"<<"\n";

}
@@DECL