-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bibtexextractor #4985
Closed
Closed
Bibtexextractor #4985
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1ecd42f
Added menu item and empty window
f1d9191
Added creation of new BibTeX entity without any parsing(constant debu…
ba8edc5
Added part of parsing
47b3034
Added last parsing part
6dd5e6b
Fixes for pull request
4bb6d23
Merge remote-tracking branch 'upstream/master' into bibtexextractor
aa8fb26
Remove conflict marker
koppor ec6513b
Merge branch 'master' into bibtexextractor
koppor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -210,3 +210,4 @@ Yang Zongze | |
Yara Grassi Gouffon | ||
Yifan Peng | ||
Zhang Liang | ||
Nikita Borovikov |
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
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
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
181 changes: 181 additions & 0 deletions
181
src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractor.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package org.jabref.gui.bibtexextractor; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.BiblatexEntryType; | ||
import org.jabref.model.entry.BiblatexEntryTypes; | ||
import org.jabref.model.entry.FieldName; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Calendar; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class BibtexExtractor { | ||
|
||
private final static String authorTag = "[author_tag]"; | ||
private final static String urlTag = "[url_tag]"; | ||
private final static String yearTag = "[year_tag]"; | ||
private final static String pagesTag = "[pages_tag]"; | ||
private final static String titleTag = "[title_tag]"; | ||
private final static String journalTag = "[journal_tag]"; | ||
|
||
private final static String INITIALS_GROUP = "INITIALS"; | ||
private final static String LASTNAME_GROUP = "LASTNAME"; | ||
|
||
private ArrayList<String> urls = new ArrayList<>(); | ||
private ArrayList<String> authors = new ArrayList<>(); | ||
private String year = new String(); | ||
private String pages = new String(); | ||
private String title = new String(); | ||
private boolean isArticle = true; | ||
private String journalOrPublisher = new String(); | ||
|
||
private static final Pattern urlPattern = Pattern.compile( | ||
"(?:^|[\\W])((ht|f)tp(s?):\\/\\/|www\\.)" + | ||
"(([\\w\\-]+\\.)+?([\\w\\-.~]+\\/?)*" + | ||
"[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)", | ||
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); | ||
|
||
private static final Pattern yearPattern = Pattern.compile( | ||
"\\d{4}", | ||
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); | ||
|
||
private static final Pattern authorPattern1 = Pattern.compile( | ||
"(?<" + LASTNAME_GROUP + ">\\p{Lu}\\w+),?\\s(?<" + INITIALS_GROUP + ">(\\p{Lu}\\.\\s){1,2})" + | ||
"\\s*(and|,|\\.)*", | ||
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); | ||
|
||
private static final Pattern authorPattern2 = Pattern.compile( | ||
"(?<" + INITIALS_GROUP + ">(\\p{Lu}\\.\\s){1,2})(?<" + LASTNAME_GROUP + ">\\p{Lu}\\w+)" + | ||
"\\s*(and|,|\\.)*", | ||
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); | ||
|
||
private static final Pattern pagesPattern = Pattern.compile( | ||
"(p.)?\\s?\\d+(-\\d+)?", | ||
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL); | ||
|
||
|
||
public BibEntry extract(String input){ | ||
String inputWithoutUrls = findUrls(input); | ||
String inputWithoutAuthors = findAuthors(inputWithoutUrls); | ||
String inputWithoutYear = findYear(inputWithoutAuthors); | ||
String inputWithoutPages = findPages(inputWithoutYear); | ||
String nonparsed = findParts(inputWithoutPages); | ||
return GenerateEntity(nonparsed); | ||
} | ||
|
||
private BibEntry GenerateEntity(String input){ | ||
BiblatexEntryType type = isArticle ? BiblatexEntryTypes.ARTICLE : BiblatexEntryTypes.BOOK; | ||
BibEntry extractedEntity = new BibEntry(type); | ||
extractedEntity.setField(FieldName.AUTHOR, String.join(" and ", authors)); | ||
extractedEntity.setField(FieldName.URL, String.join(", ", urls)); | ||
extractedEntity.setField(FieldName.YEAR, year); | ||
extractedEntity.setField(FieldName.PAGES, pages); | ||
extractedEntity.setField(FieldName.TITLE, title); | ||
if (isArticle){ | ||
extractedEntity.setField(FieldName.JOURNAL, journalOrPublisher); | ||
} | ||
else { | ||
extractedEntity.setField(FieldName.PUBLISHER, journalOrPublisher); | ||
} | ||
extractedEntity.setField(FieldName.COMMENT, input); | ||
return extractedEntity; | ||
} | ||
|
||
private String findUrls(String input){ | ||
Matcher matcher = urlPattern.matcher(input); | ||
while (matcher.find()) { | ||
urls.add(input.substring(matcher.start(1), matcher.end())); | ||
} | ||
return fixSpaces(matcher.replaceAll(urlTag)); | ||
} | ||
|
||
private String findYear(String input){ | ||
Matcher matcher = yearPattern.matcher(input); | ||
while (matcher.find()){ | ||
String yearCandidate = input.substring(matcher.start(), matcher.end()); | ||
Integer intYearCandidate = Integer.parseInt(yearCandidate); | ||
if (intYearCandidate > 1700 && intYearCandidate <= Calendar.getInstance().get(Calendar.YEAR)){ | ||
year = yearCandidate; | ||
return fixSpaces(input.replace(year, yearTag)); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
private String findAuthors(String input){ | ||
String currentInput = findAuthorsByPattern(input, authorPattern1); | ||
return findAuthorsByPattern(currentInput, authorPattern2); | ||
} | ||
|
||
private String findAuthorsByPattern(String input, Pattern pattern){ | ||
Matcher matcher = pattern.matcher(input); | ||
while (matcher.find()) { | ||
authors.add(GenerateAuthor(matcher.group(LASTNAME_GROUP), matcher.group(INITIALS_GROUP))); | ||
} | ||
return fixSpaces(matcher.replaceAll(authorTag)); | ||
} | ||
|
||
private String GenerateAuthor(String lastName, String initials){ | ||
return lastName + ", " + initials; | ||
} | ||
|
||
private String findPages(String input){ | ||
Matcher matcher = pagesPattern.matcher(input); | ||
if (matcher.find()){ | ||
pages = input.substring(matcher.start(1), matcher.end()); | ||
} | ||
return fixSpaces(matcher.replaceFirst(pagesTag)); | ||
} | ||
|
||
private String fixSpaces(String input){ | ||
return input.replaceAll("[,.!?;:]", "$0 ") | ||
.replaceAll("\\p{Lt}", " $0") | ||
.replaceAll("\\s+", " ").trim(); | ||
} | ||
|
||
private String findParts(String input) | ||
{ | ||
ArrayList<String> lastParts = new ArrayList<>(); | ||
String line = input; | ||
int afterAuthorsIndex = input.lastIndexOf(authorTag); | ||
if (afterAuthorsIndex == -1){ | ||
return input; | ||
} | ||
else { | ||
afterAuthorsIndex += authorTag.length(); | ||
} | ||
int delimiterIndex = input.lastIndexOf("//"); | ||
if (delimiterIndex != -1){ | ||
lastParts.add(input.substring(afterAuthorsIndex, delimiterIndex) | ||
.replace(yearTag, "") | ||
.replace(pagesTag, "")); | ||
lastParts.addAll(Arrays.asList(input.substring(delimiterIndex + 2).split(",|\\."))); | ||
} | ||
|
||
else { | ||
lastParts.addAll(Arrays.asList(input.substring(afterAuthorsIndex).split(",|\\."))); | ||
} | ||
int nonDigitParts = 0; | ||
for (String part: lastParts) { | ||
if (part.matches(".*\\d.*")){ | ||
break; | ||
} | ||
nonDigitParts++; | ||
} | ||
if (nonDigitParts > 0){ | ||
title = lastParts.get(0); | ||
line.replace(title, titleTag); | ||
} | ||
if (nonDigitParts > 1){ | ||
journalOrPublisher = lastParts.get(1); | ||
line.replace(journalOrPublisher, journalTag); | ||
} | ||
if (nonDigitParts > 2){ | ||
isArticle = false; | ||
} | ||
return fixSpaces(line); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/jabref/gui/bibtexextractor/ExtractBibtexAction.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.jabref.gui.bibtexextractor; | ||
|
||
import org.jabref.gui.JabRefFrame; | ||
import org.jabref.gui.actions.SimpleCommand; | ||
|
||
public class ExtractBibtexAction extends SimpleCommand { | ||
|
||
private final JabRefFrame jabRefFrame; | ||
|
||
public ExtractBibtexAction(JabRefFrame jabRefFrame) { | ||
this.jabRefFrame = jabRefFrame; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
ExtractBibtexDialog dlg = new ExtractBibtexDialog(jabRefFrame); | ||
dlg.showAndWait(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/org/jabref/gui/bibtexextractor/ExtractBibtexDialog.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.jabref.gui.bibtexextractor; | ||
|
||
import javafx.scene.control.*; | ||
import javafx.scene.layout.VBox; | ||
import org.jabref.Globals; | ||
import org.jabref.gui.JabRefFrame; | ||
import org.jabref.gui.util.BaseDialog; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.BiblatexEntryTypes; | ||
import org.jabref.model.entry.EntryType; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* GUI Dialog for the feature "Extract BibTeX from plain text". | ||
*/ | ||
public class ExtractBibtexDialog extends BaseDialog<Void> { | ||
|
||
private final JabRefFrame frame; | ||
private TextArea textArea; | ||
private Button buttonExtract; | ||
|
||
public ExtractBibtexDialog(JabRefFrame frame) { | ||
super(); | ||
this.setTitle(Localization.lang("Input text to parse")); | ||
this.frame = frame; | ||
|
||
initialize(); | ||
} | ||
|
||
private void initialize(){ | ||
textArea = new TextArea(); | ||
textArea.setWrapText(true); | ||
textArea.textProperty() | ||
.addListener((observable, oldValue, newValue) -> buttonExtract.setDisable(newValue.isEmpty())); | ||
|
||
VBox container = new VBox(20); | ||
container.getChildren().addAll( | ||
textArea); | ||
container.setPrefWidth(600); | ||
|
||
ButtonType buttonTypeGenerate = new ButtonType(Localization.lang("Extract"), ButtonBar.ButtonData.OK_DONE); | ||
getDialogPane().getButtonTypes().setAll( | ||
buttonTypeGenerate, | ||
ButtonType.CANCEL | ||
); | ||
|
||
buttonExtract = (Button) getDialogPane().lookupButton(buttonTypeGenerate); | ||
buttonExtract.setTooltip(new Tooltip((Localization.lang("Starts the extraction of the BibTeX entry")))); | ||
buttonExtract.setDisable(true); | ||
buttonExtract.setOnAction(e -> startExtraction()); | ||
|
||
getDialogPane().setContent(container); | ||
} | ||
|
||
private void startExtraction() | ||
{ | ||
BibtexExtractor extractor = new BibtexExtractor(); | ||
BibEntry entity = extractor.extract(textArea.getText()); | ||
trackNewEntry(BiblatexEntryTypes.ARTICLE); | ||
frame.getCurrentBasePanel().insertEntry(entity); | ||
} | ||
|
||
private void trackNewEntry(EntryType type) { | ||
Map<String, String> properties = new HashMap<>(); | ||
properties.put("EntryType", type.getName()); | ||
|
||
Globals.getTelemetryClient().ifPresent(client -> client.trackEvent("NewEntry", properties, new HashMap<>())); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't JabRef Frame here.
Best is to use the StateManager and then get the active Database and call insertEntries there