Re: Parsing of Names

"Douglas S. Blank" <dblank-8NN4yrnyJ/L2fBVCVOL8/[email protected]> Fri, 18 May 2007 11:24:00 -0400 (EDT)
Newsgroups gmane.comp.gnome.apps.pybliographer
Message-ID <[email protected]>
On Thu, May 17, 2007 4:45 pm, Fr=E9d=E9ric Gobry said:
>> Below, I have a sample of code that reads a BibTeX file, and breaks the
>> data into fields and values. Is there an easy method that is built into
>> pybliographer to parse the text of names? I'd like each person's first
>> and last name. (And does this code look on track, or am I doing this the
>> hard way?)
>
> I might be missing something, but why aren't you using the
> BibTeX.Reader class directly? BTW, it has some heuristics to split
> author names.

Mostly because I don't know exactly what the schema is---these are generic
bibtex files. If I use the Reader then I have to know what the scheme is,
correct?

In any event, it looks like the Reader can't handle authors of the form:

author =3D "Scott McNamara, Martha Cyr, Chris Rogers and Barbara Bratzel",

Isn't that an acceptable BibTex form? Does Pyblio only allow names
separated by "and"?

-Doug

>>
>> Thanks for any pointers,
>>
>> -Doug
>>
>> def parseBibtex(udir, filename, fields):
>>      """ Pass in directory, filename, and list of valid fields. """
>>      from Pyblio.Parsers.Syntax.BibTeX.Parser import read, Comment,
>> Record, Join, Block, ATComment
>>      from Pyblio.Exceptions import ParserError
>>      fd =3D open(udir + "/" + filename, "r")
>>      try:
>>          records =3D read(fd)
>>      except ParserError, err:
>>          print "ERROR: %s at line %d: %s" % (file, err[0], err[1])
>>          return
>>      for rec in records:
>>          data =3D []
>>          if type(rec) =3D=3D Comment:
>>              pass # just a comment
>>          elif type(rec) =3D=3D ATComment:
>>              pass
>>          elif type(rec) =3D=3D Record:
>>              for pair in rec.fields:
>>                  field, value =3D pair
>>                  field =3D field.strip().lower()
>>                  if type(value) =3D=3D Join:
>>                      for part in value:
>>                          if type(part) =3D=3D Block:
>>                              if field not in fields:
>>                                  print p("Unknown field: %s" % field)
>>                                  continue
>>                              if field =3D=3D "author" or field =3D=3D "e=
ditor":
>>                                  # FIX: break value into names
>>                              else: # other than author
>>                                  data.append((field, part.flat()))
>>                          else: # other than block
>>                              data.append((field, part.flat()))
>>                  else: # other than join
>>                      data.append((field, value.flat()))
>>          else:
>>              print p("Unknown record type: %s" % type(rec))
>>          # do something with data
>>     # return something
>
> --
> Fr=E9d=E9ric
>



-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/