]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/catalog/biblio_fingerprint.js
6d83dbaaea1561002d391fdfed6b0f3047fb99bc
[Evergreen.git] / Open-ILS / src / javascript / backend / catalog / biblio_fingerprint.js
1 // so we can tell if it's a book or other type
2 load_lib('record_type.js');
3
4 environment.result = {};
5
6 var marcdoc = new XML(environment.marc);
7 var marc_ns = new Namespace('http://www.loc.gov/MARC21/slim');
8
9 var modsdoc = new XML(environment.mods);
10 var mods_ns = new Namespace('http://www.loc.gov/mods/');
11 default xml namespace = marc_ns;
12
13 //var mods3_ns = new Namespace('http://www.loc.gov/mods/v3');
14
15
16 var rtype = recordType(marcdoc); // BKS, SER, VIS, MIX, MAP, SCO, REC, COM
17
18 var quality = 0;
19 var t = '';
20 var a = '';
21
22 try {
23         // first, related items entries (700t)
24         var t = marcdoc.datafield.( @tag == '700' ).subfield.( @code == 't');
25         if (!t.length()) throw "No title in related item added entry (700)";
26         
27         a = t.parent().subfield.( @code == 'a' );
28
29         quality += 10;
30
31         log_debug("title: " + t);
32         log_debug("author: " + a);
33 } catch(e) {
34         log_debug(e);
35         log_debug("Looking in main entries");
36
37         var _t = '';
38         try {
39                 try { 
40                         try { // uniform title
41                                 _t = marcdoc.datafield.( @tag == '240' ).subfield.( @code == 'a' );
42                                 if (!_t.length()) throw "No title in 240";
43                         } catch(e) { // translation of title
44                                 log_debug(e);
45                                 _t = marcdoc.datafield.( @tag == '242' ).subfield.( @code == 'a' );
46                                 if (!_t.length()) throw "No title in 242";
47                         }
48                 } catch(e) { // alternate title (not as note) 
49                         log_debug(e);
50                         _t = marcdoc.datafield.( @tag == '246' && !(@ind1.match(/0|1/)) ).subfield.( @code == 'a' );
51                         if (!_t.length()) throw "No title in 246";
52                 }
53
54                 t = _t[0];
55                 log_debug("Title: " + t);
56                 quality += 25;
57
58         } catch(e) {
59                 log_debug(e);
60                 log_debug("Using title proper (245a)");
61                 t = marcdoc.datafield.( @tag == '245' ).subfield.( @code == 'a' );
62                 t = t[0];
63                 quality += 10;
64         }
65
66         try {
67                 var _a = marcdoc.datafield.( @tag == '100' || @tag == '110' || @tag == '111').subfield.( @code == 'a' );
68                 if (!_a.length()) throw "No author in 100, 110, 111";
69                 
70                 a = _a[0];
71                 log_debug("Author: " + a);
72
73         } catch(e) {
74                 log_debug(e);
75                 log_debug("Trying to find a publisher (260b)");
76                 a = marcdoc.datafield.( @tag == '260' ).subfield.( @code == 'b' );
77                 a = a[0];
78         }
79 }
80
81 if (rtype != 'BKS') {
82         quality += marcdoc.datafield.length() / 2;
83 } else {
84         quality += 20 + marcdoc.datafield.length();
85 }
86
87 var title = t;
88 if (!title) {
89         log_debug("no title found");
90         title = '';
91 } else {
92         title = title.toString();
93 }
94
95 title = title
96         .toLowerCase()
97         .replace(/\[.+?\]/,'')
98         .replace(/\bthe\b|\ban?d?\b|\W+/g,'');
99
100
101 var author = a;
102 if (!author) {
103         author = '';
104 } else {
105         author = author.toString();
106 }
107
108 author = author.toLowerCase().replace(/^\s*(\w+).*?$/,"$1");
109
110 environment.result.fingerprint = title + author;
111
112 if (marcdoc.datafield.(@tag == '040').subfield.(@code == 'a').toString().match(/DLC/)) {
113         quality += 5;
114         log_debug( 'got DLC bump' );
115 }
116
117 if (marcdoc.datafield.(@tag == '039').subfield.(@code == 'b').toString().match(/oclc/i)) {
118         quality += 10;
119         log_debug( 'got OCLC source bump' );
120         
121 } else if (marcdoc.datafield.(@tag == '039').subfield.(@code == 'b').toString().match(/isxn/i)) {
122         quality += 5;
123         log_debug( 'got ISxN source bump' );
124         
125 } else if (marcdoc.datafield.(@tag == '039').subfield.(@code == 'b').toString().match(/local/i)) {
126         quality += 1;
127         log_debug( 'got Local source bump' );
128 }
129
130 if (extractFixedField(marcdoc, 'Lang') == 'eng') {
131         quality *= 2;
132         log_debug( 'got language bump for ' + extractFixedField(marcdoc, 'Lang') );
133 }
134
135
136 environment.result.quality = quality;
137