OCLC’s Classify in C#

During Code4Lib, I spent a little time playing with OCLC’s Classify service (http://oclc.org/developer/services/classify).  I’ve been working on adding a couple of functions into MarcEdit that will allow folks to leverage some of the OCLC web services.   Using OCLC’s Classify, I’ve been working on an experimental tool that would allow you to do batch classification of records based on OCLC’s classification.  Below, you will find the class and below that, you can find how this might look in MarcEdit.

OCLC Classify Class

 class oclc_api_classify
    {
        public enum Class_Type {lcc = 0, dd=1}
        private string pLastError = "";

        public string LastError
        {
            get { return pLastError; }
            set { pLastError = value; }
        }
        private string MakeHTTPRequest(string url)
        {
            System.Net.HttpWebRequest wr;
            System.IO.Stream objStream = null;
            try
            {
                wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
                wr.UserAgent = "MarcEdit 5.2";

                System.Net.WebResponse response = wr.GetResponse();
                objStream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(objStream);
                string sresults = reader.ReadToEnd();
                reader.Close();
                return sresults;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        public string DoSearch(string url, oclc_api_classify.Class_Type ctype)
        {
            string sreturn = MakeHTTPRequest(url);
            //process results
            return ProcessClassify(sreturn, ctype);
        }

        private string ProcessClassify(string s, oclc_api_classify.Class_Type ctype)
        {
            System.Xml.XmlDocument objXML = new System.Xml.XmlDocument();
            objXML.LoadXml(s);
            System.Xml.XmlNamespaceManager oMan = new System.Xml.XmlNamespaceManager(objXML.NameTable);
            oMan.AddNamespace("oclc", "http://classify.oclc.org");
           

            System.Xml.XmlNodeList objList;

            try
            {
                if (ctype == Class_Type.dd)
                {
                    objList = objXML.SelectNodes("//oclc:ddc/oclc:mostPopular", oMan);
                    if (objList != null)
                    {
                        foreach (System.Xml.XmlNode objN in objList)
                        {
                           
                            if (objN.Attributes["nsfa"] != null)
                            {
                                return objN.Attributes["nsfa"].InnerText;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    
                    objList = objXML.SelectNodes("//oclc:lcc/oclc:mostPopular",oMan);
                    if (objList != null)
                    {
                        foreach (System.Xml.XmlNode objN in objList)
                        {
                            if (objN.Attributes["nsfa"] != null)
                            {
                                return objN.Attributes["nsfa"].InnerText;
                                break;
                            }
                        }
                    }
                    else
                    {
                        //System.Windows.Forms.MessageBox.Show("miss");
                    }
                }
                return "";
            }
            catch (Exception e)
            {
                LastError = e.ToString();
                return "";
            } 
        }

        
    }

In MarcEdit:

In MarcEdit, I’ve added an experimental dialog that does the batch classification search.  Here’s an example:

image

As you can see, I’m envisioning two options:

  1. Ability to process Individual Records
  2. Ability to process All Records in a file
  3. Ability to select suggested Dewey or Library of Congress work
  4. Ability to decide to insert data if fields only if call numbers are not present
  5. Ability to determine what field the call number should be inserted into.

I’m thinking this will show up in the next MarcEdit update, so long as OCLC’s terms and conditions ends up being something that I (and they) can live with.

–TR


Posted

in

,

by

Tags:

Comments

One response to “OCLC’s Classify in C#”

  1. […] This post was mentioned on Twitter by infopeep, Becky Yoose. Becky Yoose said: Shiny! :cD Terry's Worklog – OCLC’s Classify in C# https://blog.reeset.net/archives/931 #marcedit […]