@@ -4,10 +4,14 @@ import { getPref } from "../../utils/prefs";
44import { ScraperTask } from "../../utils/task" ;
55import { isChineseTopAttachment , isChinsesSnapshot } from "../../utils/detect" ;
66import { CNKI } from "./cnki" ;
7- import { PubScholar } from "./pubscholar" ;
7+ // import { PubScholar } from "./pubscholar";
8+ import { Yiigle } from "./yiigle" ;
9+ import { compareTwoStrings } from "string-similarity" ;
810
911const cnki = new CNKI ( ) ;
10- const pubscholar = new PubScholar ( ) ;
12+ // const pubscholar = new PubScholar();
13+ const yiigle = new Yiigle ( ) ;
14+
1115async function getSearchOption (
1216 item : Zotero . Item ,
1317) : Promise < SearchOption | null > {
@@ -58,15 +62,40 @@ export async function metaSearch(
5862 task . addMsg ( `Found ${ cnkiSearchResult . length } results from CNKI` ) ;
5963 scrapeSearchResults = scrapeSearchResults . concat ( cnkiSearchResult ) ;
6064 }
61- const pubscholarSearchResult = await pubscholar . search ( searchOption ) ;
62- ztoolkit . log ( "pubscholar results" , pubscholarSearchResult ) ;
63- if ( pubscholarSearchResult ) {
64- task . addMsg (
65- `Found ${ pubscholarSearchResult . length } results from PubScholar` ,
66- ) ;
67- scrapeSearchResults =
68- scrapeSearchResults . concat ( pubscholarSearchResult ) ;
65+ // const pubscholarSearchResult = await pubscholar.search(searchOption);
66+ // ztoolkit.log("pubscholar results", pubscholarSearchResult);
67+ // if (pubscholarSearchResult) {
68+ // task.addMsg(
69+ // `Found ${pubscholarSearchResult.length} results from PubScholar`,
70+ // );
71+ // scrapeSearchResults = scrapeSearchResults.concat(
72+ // pubscholarSearchResult,
73+ // );
74+ // }
75+ const yiigleSearchResult = await yiigle . search ( searchOption ) ;
76+ ztoolkit . log ( "yiigle results" , yiigleSearchResult ) ;
77+ if ( yiigleSearchResult ) {
78+ task . addMsg ( `Found ${ yiigleSearchResult . length } results from Yiigle` ) ;
79+ scrapeSearchResults = scrapeSearchResults . concat ( yiigleSearchResult ) ;
6980 }
81+
82+ // Filter search results
83+ const filteredResults1 = scrapeSearchResults . filter ( ( result ) => {
84+ return ( result . articleTitle as string ) . includes ( searchOption . title ) ;
85+ } ) ;
86+
87+ const filteredResults2 = scrapeSearchResults . filter ( ( result ) => {
88+ const score = compareTwoStrings (
89+ searchOption . title ,
90+ result . articleTitle as string ,
91+ ) ;
92+ ztoolkit . log ( `Similarity score for "${ result . articleTitle } ": ${ score } ` ) ;
93+ return (
94+ ! ( result . articleTitle as string ) . includes ( searchOption . title ) &&
95+ score > 0.85
96+ ) ;
97+ } ) ;
98+ scrapeSearchResults = filteredResults1 . concat ( filteredResults2 ) ;
7099 } else {
71100 task . addMsg ( "Filename parsing error" ) ;
72101 task . status = "fail" ;
@@ -87,82 +116,110 @@ export async function metaSearch(
87116}
88117
89118export async function metaTranslate ( task : ScraperTask ) : Promise < void > {
90- if ( task . searchResults ) {
119+ if ( task . searchResults . length === 0 ) {
120+ task . addMsg ( "No search results found." ) ;
121+ task . status = "fail" ;
122+ }
123+
124+ try {
125+ const resultIndex = task . resultIndex || 0 ; // default is 0
126+ task . resultIndex = resultIndex ;
127+ const searchResult = task . searchResults [ resultIndex ] ;
128+ const libraryID = task . item . libraryID ;
129+ ztoolkit . log ( `start translate for search result: ${ searchResult . title } ` ) ;
130+ let translatedItems : Zotero . Item [ ] = [ ] ;
91131 try {
92- const resultIndex = task . resultIndex || 0 ; // default is 0
93- task . resultIndex = resultIndex ;
94- const result = task . searchResults [ resultIndex ] ;
95- ztoolkit . log ( `start translate for search result: ${ result . title } ` ) ;
96- let newItem : Zotero . Item | null | undefined = null ;
97- switch ( result . source ) {
132+ switch ( searchResult . source ) {
98133 case "CNKI" :
99134 ztoolkit . log ( "translated by CNKI" ) ;
100- newItem = await cnki . translate ( task , false ) ;
135+ translatedItems = await cnki . translate (
136+ searchResult ,
137+ libraryID ,
138+ false ,
139+ ) ;
101140 break ;
102- case "PubScholar" :
103- ztoolkit . log ( "translated by PubScholar" ) ;
104- newItem = await pubscholar . translate ( task , false ) ;
141+ // case "PubScholar":
142+ // ztoolkit.log("translated by PubScholar");
143+ // newItem = await pubscholar.translate(task, false);
144+ // break;
145+ case "中华医学" :
146+ ztoolkit . log ( "translated by Yiigle" ) ;
147+ translatedItems = await yiigle . translate (
148+ searchResult ,
149+ libraryID ,
150+ false ,
151+ ) ;
105152 break ;
106153 default :
107154 break ;
108155 }
109- ztoolkit . log ( newItem ) ;
156+ ztoolkit . log ( translatedItems ) ;
157+ } catch ( e ) {
158+ ztoolkit . log ( `Translation error: ${ e } ` ) ;
159+ task . addMsg ( `Translation error: ${ e } ` ) ;
160+ }
110161
111- if ( newItem ) {
112- // if (addon.data.env != "development")
113- newItem = await globalItemFix ( newItem ) ;
114- if ( task . type == "attachment" ) {
115- task . item . parentID = newItem . id ;
116- } else if ( task . type == "snapshot" ) {
117- if ( task . item . isTopLevelItem ( ) ) {
118- ztoolkit . log ( "Translate snapshot item for webpage item" ) ;
119- const tmpJSON = newItem . toJSON ( ) ;
120- task . item . fromJSON ( tmpJSON ) ;
121- await newItem . eraseTx ( ) ;
122- } else {
123- ztoolkit . log ( "Translate snapshot attachment item" ) ;
124- const oldParentItem = task . item . parentItem ! ;
125- const collectionIDs = oldParentItem . getCollections ( ) ;
126- task . item . parentID = newItem . id ;
127- // When parent item is erased, the attachment item will be erased. Set new parent item before the old parent will be earsed.
128- await task . item . saveTx ( ) ;
129- await oldParentItem . eraseTx ( ) ;
130- newItem . setCollections ( collectionIDs ) ;
131- await newItem . saveTx ( ) ;
132- }
162+ if ( translatedItems . length === 1 ) {
163+ // if (addon.data.env != "development")
164+ const translatedItem = await globalItemFix ( task . item , translatedItems [ 0 ] ) ;
165+ if ( task . type == "attachment" ) {
166+ task . item . parentID = translatedItem . id ;
167+ } else if ( task . type == "snapshot" ) {
168+ if ( task . item . isTopLevelItem ( ) ) {
169+ ztoolkit . log ( "Translate snapshot item for webpage item" ) ;
170+ const tmpJSON = translatedItem . toJSON ( ) ;
171+ task . item . fromJSON ( tmpJSON ) ;
172+ await translatedItem . eraseTx ( ) ;
173+ } else {
174+ ztoolkit . log ( "Translate snapshot attachment item" ) ;
175+ const oldParentItem = task . item . parentItem ! ;
176+ const collectionIDs = oldParentItem . getCollections ( ) ;
177+ task . item . parentID = translatedItem . id ;
178+ // When parent item is erased, the attachment item will be erased. Set new parent item before the old parent will be earsed.
179+ await task . item . saveTx ( ) ;
180+ await oldParentItem . eraseTx ( ) ;
181+ translatedItem . setCollections ( collectionIDs ) ;
182+ await translatedItem . saveTx ( ) ;
133183 }
134- await task . item . saveTx ( ) ;
135- task . status = "success" ;
136- } else {
137- task . addMsg ( "Translation error" ) ;
138- task . status = "fail" ;
139184 }
140- } catch ( e ) {
141- task . addMsg ( `ERROR: ${ e } ` ) ;
185+ await task . item . saveTx ( ) ;
186+ task . status = "success" ;
187+ } else if ( translatedItems . length > 1 ) {
188+ task . addMsg (
189+ `Multiple items (${ translatedItems . length } ) translated, please check details.` ,
190+ ) ;
191+ task . status = "fail" ;
192+ } else {
193+ task . addMsg ( "Translation error" ) ;
142194 task . status = "fail" ;
143195 }
144- } else {
145- task . addMsg ( "No search results found." ) ;
196+ } catch ( e ) {
197+ task . addMsg ( `ERROR: ${ e } ` ) ;
146198 task . status = "fail" ;
147199 }
148200}
149201
150202// Need to update data in item returned by translator.
151- async function globalItemFix ( item : Zotero . Item ) : Promise < Zotero . Item > {
203+ async function globalItemFix (
204+ oldItem : Zotero . Item ,
205+ newItem : Zotero . Item ,
206+ ) : Promise < Zotero . Item > {
152207 if ( Zotero . Prefs . get ( "extensions.zotero.automaticTags" , true ) ) {
153208 // Keyword tag type is automatic.
154209 ztoolkit . log ( "update auto tags" ) ;
155- item . setTags (
156- item . getTags ( ) . map ( ( t : { tag : string ; type ?: number } ) => ( {
210+ newItem . setTags (
211+ newItem . getTags ( ) . map ( ( t : { tag : string ; type ?: number } ) => ( {
157212 tag : t . tag ,
158213 type : 1 ,
159214 } ) ) ,
160215 ) ;
161216 } else {
162217 // Remove automatic tags
163218 ztoolkit . log ( "remove all tags" ) ;
164- item . removeAllTags ( ) ;
219+ newItem . removeAllTags ( ) ;
165220 }
166- await item . saveTx ( ) ;
167- return item ;
221+ // Preserve collections
222+ oldItem . getCollections ( ) . forEach ( ( cid ) => newItem ! . addToCollection ( cid ) ) ;
223+ await newItem . saveTx ( ) ;
224+ return newItem ;
168225}
0 commit comments