-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
556 additions
and
56 deletions.
There are no files selected for viewing
Binary file not shown.
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
File renamed without changes.
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 @@ | ||
mvn package && mvn exec:java -D exec.mainClass="cuni.software.ViewRunner" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package cuni.software; | ||
|
||
import javafx.application.*; | ||
import javafx.beans.property.*; | ||
import javafx.collections.*; | ||
import javafx.fxml.*; | ||
import javafx.scene.control.*; | ||
import javafx.scene.control.SpinnerValueFactory.*; | ||
import javafx.stage.*; | ||
import javafx.stage.FileChooser.*; | ||
import javafx.util.converter.*; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
import java.util.stream.*; | ||
|
||
/** | ||
* @author Octavio Calleya | ||
*/ | ||
public class Controller { | ||
|
||
@FXML | ||
private TextField searchField; | ||
@FXML | ||
private ListView<Hyperlink> listView; | ||
@FXML | ||
private Button searchButton; | ||
@FXML | ||
private Button loadButton; | ||
@FXML | ||
private TextArea textArea; | ||
@FXML | ||
private Spinner<Double> similaritySpinner; | ||
|
||
private HostServices hostServices; | ||
private BooleanProperty searchingProperty = new SimpleBooleanProperty(false); | ||
private SearchEngine searchEngine = new SearchEngine(); | ||
private List<RssFeed> loadedRssFeeds; | ||
|
||
@FXML | ||
public void initialize() { | ||
searchButton.disableProperty().bind(searchingProperty.not().and(searchField.textProperty().isEmpty())); | ||
loadButton.disableProperty().bind(searchingProperty); | ||
loadButton.setOnAction(e -> loadRssFromFile()); | ||
searchButton.setOnAction(e -> performSearch()); | ||
DoubleSpinnerValueFactory doubleSpinnerValueFactory = new DoubleSpinnerValueFactory(1.550, 1.575); | ||
doubleSpinnerValueFactory.setAmountToStepBy(0.00025); | ||
doubleSpinnerValueFactory.setConverter(new DoubleStringConverter()); | ||
similaritySpinner.setValueFactory(doubleSpinnerValueFactory); | ||
} | ||
|
||
private void performSearch() { | ||
new Thread(() -> { | ||
Double similarityValue = similaritySpinner.getValue(); | ||
List<Article> relatedArticles = searchEngine.findRelatedArticles(searchField.getText(), similarityValue); | ||
Platform.runLater(() -> log("Found " + relatedArticles.size() + " articles")); | ||
addArticlesToList(relatedArticles); | ||
}).start(); | ||
} | ||
|
||
private void addArticlesToList(List<Article> articles) { | ||
Platform.runLater(() -> { | ||
ObservableList<Hyperlink> links = FXCollections.observableArrayList(); | ||
articles.forEach(article -> { | ||
Hyperlink link = new Hyperlink(article.getUri()); | ||
link.setOnAction(e -> hostServices.showDocument(article.getUri())); | ||
links.add(link); | ||
}); | ||
listView.setItems(links); | ||
}); | ||
} | ||
|
||
private void loadRssFromFile() { | ||
FileChooser chooser = new FileChooser(); | ||
chooser.setTitle("Select file..."); | ||
chooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt")); | ||
File feedsFile = chooser.showOpenDialog(searchField.getScene().getWindow()); | ||
if (feedsFile != null) { | ||
new Thread(() -> loadTask(feedsFile)).start(); | ||
log("Importing feeds..."); | ||
searchingProperty.setValue(true); | ||
} | ||
} | ||
|
||
private void loadTask(File feedsFile) { | ||
try { | ||
loadedRssFeeds = RssFeedParse.fromFile(feedsFile); | ||
printParsedFeeds(); | ||
addToSearchEngine(); | ||
} | ||
catch (IOException e) { | ||
log("Error loading rss feeds from file: " + e.getMessage()); | ||
} | ||
finally { | ||
Platform.runLater(() -> searchingProperty.setValue(false)); | ||
} | ||
} | ||
|
||
private void addToSearchEngine() { | ||
List<Article> allArticles = loadedRssFeeds.stream() | ||
.flatMap(feed -> feed.getArticles().stream()) | ||
.collect(Collectors.toList()); | ||
searchEngine.addArticles(allArticles); | ||
} | ||
|
||
private void printParsedFeeds() { | ||
loadedRssFeeds.forEach(feed -> { | ||
log("\t\t" + feed.getUrl().toString() + "\n\tLoaded articles:"); | ||
feed.getArticles().forEach(article -> log(article.toString())); | ||
}); | ||
} | ||
|
||
public void setHostServices(HostServices hostServices) { | ||
this.hostServices = hostServices; | ||
} | ||
|
||
public void log(String message) { | ||
Platform.runLater(() -> textArea.appendText(message + "\n")); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cuni.software; | ||
|
||
import com.google.common.collect.*; | ||
import org.jsoup.*; | ||
import org.jsoup.nodes.*; | ||
import org.jsoup.select.*; | ||
|
||
import java.io.*; | ||
|
||
public class Parser { | ||
|
||
private Multiset<String> articleTerms = HashMultiset.create(); | ||
|
||
/** | ||
* This method retrieves terms from the articles found in the provided link | ||
* @param link is a URL link to the atricle. | ||
* @return set of terms. | ||
*/ | ||
public Multiset<String> parseLink(String link){ | ||
try { | ||
Document doc = Jsoup.connect(link).get(); | ||
Elements elements = doc.getElementsByTag("p"); | ||
for(Element e: elements){ | ||
if (e.attributes().size() == 0){ //actual content doesn't have any atts in <p> tags | ||
retrieveItems(e); | ||
} | ||
} | ||
} catch (IOException e) { | ||
|
||
e.printStackTrace(); | ||
} | ||
return articleTerms; | ||
} | ||
|
||
private String formatWord(String word){ | ||
word.trim(); | ||
word = word.toLowerCase(); | ||
|
||
int position = -1; | ||
for (int i = word.length() - 1; i >= 0; i--){ | ||
if (Character.isLetterOrDigit((int)word.charAt(i))){ //resolves all Unicode letters thanks to (int) | ||
position = i; | ||
break; | ||
} | ||
} | ||
if (position < word.length() - 1){ | ||
word = word.substring(0, position + 1); | ||
} | ||
|
||
position = -1; | ||
|
||
for(int i = 0; i < word.length(); i++){ | ||
if (Character.isLetterOrDigit((int)word.charAt(i))){ | ||
position = i; | ||
break; | ||
} | ||
} | ||
if (position > 0){ | ||
word = word.substring(position); | ||
} | ||
|
||
return word; | ||
} | ||
|
||
private void retrieveItems(Element e){ | ||
String words[] = e.text().split(" "); | ||
for(String word : words){ | ||
word = formatWord(word); | ||
if (word.length() > 2){ | ||
articleTerms.add(word); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.