Last active
August 29, 2015 13:56
-
-
Save twneale/9085174 to your computer and use it in GitHub Desktop.
Basic uscode citation-to-neoid function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def citations(text): | |
rgx = u'(\\d+)\\s+U\\.?S\\.?C\\.?\\s*\xa7*\\s\\s*([\\d\\w\\-\\\u2013\\.\u2013]+)(\\([\\d\\w\\-\\\u2013\\.\u2013()]+\\))*' | |
matches = [] | |
for match in re.finditer(rgx, text): | |
title, section, path = match.groups() | |
section = section.strip(u'\u2013.- ') | |
start, end = match.span() | |
ident = '/us/usc/t%s/s%s' % (title, section) | |
if path: | |
pathsegs = path.strip('()') | |
pathsegs = re.split(r'[ ()]', pathsegs) | |
pathsegs = filter(None, pathsegs) | |
ident += '/' + '/'.join(pathsegs) | |
matches.append((start, end, ident)) | |
return matches | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment