-
Notifications
You must be signed in to change notification settings - Fork 74
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
Alberto Monge
authored and
Alberto Monge
committed
Apr 1, 2017
1 parent
74c3e44
commit 9accdda
Showing
5 changed files
with
366 additions
and
5 deletions.
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
110 changes: 106 additions & 4 deletions
110
Lab3_SpellChecker/src/it/polito/tdp/spellchecker/controller/SpellChecker.fxml
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 |
---|---|---|
@@ -1,7 +1,109 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
|
||
<?import javafx.geometry.Insets?> | ||
<?import javafx.scene.control.Button?> | ||
<?import javafx.scene.control.ComboBox?> | ||
<?import javafx.scene.control.Label?> | ||
<?import javafx.scene.control.TextArea?> | ||
<?import javafx.scene.layout.BorderPane?> | ||
|
||
<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.polito.tdp.spellchecker.controller.SpellCheckerController"> | ||
<!-- TODO Add Nodes --> | ||
<?import javafx.scene.layout.HBox?> | ||
<?import javafx.scene.layout.VBox?> | ||
|
||
<BorderPane prefWidth="540.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.polito.tdp.spellchecker.controller.SpellCheckerController"> | ||
<top> | ||
<Label text="Lab3_SpellChecker" BorderPane.alignment="CENTER"> | ||
<BorderPane.margin> | ||
<Insets top="10.0" /> | ||
</BorderPane.margin></Label> | ||
</top> | ||
<left> | ||
<VBox BorderPane.alignment="CENTER"> | ||
<children> | ||
<HBox alignment="CENTER_LEFT"> | ||
<children> | ||
<Label text="Choose the language: " /> | ||
<ComboBox fx:id="boxLingua" onAction="#doActivation" prefWidth="150.0"> | ||
<HBox.margin> | ||
<Insets left="10.0" /> | ||
</HBox.margin></ComboBox> | ||
</children> | ||
<opaqueInsets> | ||
<Insets /> | ||
</opaqueInsets> | ||
<padding> | ||
<Insets bottom="10.0" /> | ||
</padding> | ||
<VBox.margin> | ||
<Insets left="15.0" right="15.0" /> | ||
</VBox.margin> | ||
</HBox> | ||
<TextArea fx:id="txtDaCorreggere" maxWidth="500.0"> | ||
<VBox.margin> | ||
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | ||
</VBox.margin></TextArea> | ||
<HBox alignment="CENTER_RIGHT" nodeOrientation="LEFT_TO_RIGHT"> | ||
<VBox.margin> | ||
<Insets bottom="5.0" left="15.0" right="20.0" top="20.0" /> | ||
</VBox.margin> | ||
<children> | ||
<Button alignment="TOP_LEFT" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" onAction="#doSpellCheck" text="Spell Check" textAlignment="CENTER"> | ||
<HBox.margin> | ||
<Insets /> | ||
</HBox.margin> | ||
</Button> | ||
</children> | ||
<opaqueInsets> | ||
<Insets right="10.0" /> | ||
</opaqueInsets> | ||
</HBox> | ||
<Label text="Wrong words"> | ||
<VBox.margin> | ||
<Insets top="10.0" /> | ||
</VBox.margin> | ||
<padding> | ||
<Insets left="10.0" /> | ||
</padding> | ||
</Label> | ||
<TextArea fx:id="txtCorretto" maxWidth="500.0"> | ||
<VBox.margin> | ||
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | ||
</VBox.margin> | ||
</TextArea> | ||
<HBox alignment="CENTER_RIGHT"> | ||
<children> | ||
<Label fx:id="lblErrori" text="Errori" textFill="RED"> | ||
<opaqueInsets> | ||
<Insets right="10.0" /> | ||
</opaqueInsets> | ||
<padding> | ||
<Insets right="10.0" /> | ||
</padding> | ||
</Label> | ||
<Button alignment="CENTER_RIGHT" contentDisplay="RIGHT" mnemonicParsing="false" onAction="#doClearText" text="Clear Text "> | ||
<HBox.margin> | ||
<Insets right="20.0" /> | ||
</HBox.margin> | ||
</Button> | ||
</children> | ||
<opaqueInsets> | ||
<Insets left="10.0" right="10.0" /> | ||
</opaqueInsets> | ||
<VBox.margin> | ||
<Insets /> | ||
</VBox.margin> | ||
</HBox> | ||
<Label fx:id="lblStato" text="Label"> | ||
<padding> | ||
<Insets left="10.0" /> | ||
</padding> | ||
</Label> | ||
</children> | ||
<opaqueInsets> | ||
<Insets bottom="10.0" top="10.0" /> | ||
</opaqueInsets> | ||
<BorderPane.margin> | ||
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> | ||
</BorderPane.margin> | ||
</VBox> | ||
</left> | ||
</BorderPane> |
133 changes: 133 additions & 0 deletions
133
Lab3_SpellChecker/src/it/polito/tdp/spellchecker/controller/SpellCheckerController.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 |
---|---|---|
@@ -1,5 +1,138 @@ | ||
package it.polito.tdp.spellchecker.controller; | ||
|
||
import java.net.URL; | ||
import java.util.*; | ||
import java.util.ResourceBundle; | ||
|
||
import it.polito.tdp.spellchecker.model.*; | ||
import it.polito.tdp.spellchecker.model.Dictionary; | ||
import javafx.event.ActionEvent; | ||
import javafx.fxml.FXML; | ||
import javafx.scene.control.ComboBox; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.TextArea; | ||
import javafx.scene.control.TextField; | ||
import javafx.scene.paint.Color; | ||
import javafx.scene.text.Text; | ||
import javafx.scene.text.TextFlow; | ||
|
||
public class SpellCheckerController { | ||
|
||
private Dictionary dizionario; | ||
|
||
List<String> listaDaCorreggere = new LinkedList<String>(); | ||
|
||
@FXML | ||
private ResourceBundle resources; | ||
|
||
@FXML | ||
private URL location; | ||
|
||
@FXML | ||
private ComboBox<String> boxLingua; | ||
|
||
@FXML | ||
private TextArea txtDaCorreggere; | ||
|
||
@FXML | ||
private TextArea txtCorretto; | ||
|
||
@FXML | ||
private Label lblErrori; | ||
|
||
@FXML | ||
private Label lblStato; | ||
|
||
|
||
@FXML | ||
void doClearText(ActionEvent event) { | ||
// Reset dell'interfaccia grafica | ||
txtDaCorreggere.clear(); | ||
listaDaCorreggere.clear(); | ||
txtCorretto.clear(); | ||
lblErrori.setText(""); | ||
} | ||
|
||
|
||
|
||
@FXML | ||
void doActivation(ActionEvent event) { | ||
// L'utente non può inserire del testo prima di aver selezionato la lingua | ||
if (boxLingua.getValue() != null) { | ||
txtDaCorreggere.setDisable(false); | ||
txtCorretto.setDisable(false); | ||
|
||
txtDaCorreggere.clear(); | ||
txtCorretto.clear(); | ||
} | ||
} | ||
|
||
|
||
@FXML | ||
void doSpellCheck(ActionEvent event) { | ||
|
||
// Inizializzazione | ||
txtCorretto.clear(); | ||
listaDaCorreggere.clear(); | ||
|
||
// Gestisco la selezione della lingua | ||
dizionario.loadDictionary(boxLingua.getValue()); | ||
|
||
|
||
// Prendo il testo da correggere | ||
String inputText = txtDaCorreggere.getText(); | ||
if (inputText.isEmpty()) | ||
return; | ||
|
||
// Divido il testo usando gli spazi e elimino la punteggiatura | ||
StringTokenizer st = new StringTokenizer(inputText, " "); | ||
while (st.hasMoreTokens()) { | ||
listaDaCorreggere.add(st.nextToken().replaceAll("[ \\p{Punct}]", "").trim().toLowerCase()); | ||
} | ||
|
||
// Chiamo la funzione per la correzione del testo | ||
long l1 = System.nanoTime(); | ||
List<RichWord> lista = dizionario.spellCheckText(listaDaCorreggere); | ||
long l2 = System.nanoTime(); | ||
|
||
// Stampo il rich text | ||
int errori = 0; | ||
String richText = ""; | ||
|
||
for (RichWord r : lista) { | ||
if(!r.isCorretta()){ | ||
errori ++; | ||
richText += r.getParola(); | ||
richText += "\n"; | ||
} | ||
} | ||
|
||
txtCorretto.setText(richText); | ||
lblStato.setText("Spell check completed in " + (l2 - l1) / 1E9 + " seconds"); | ||
|
||
// Aggiorno il contenuto della label. | ||
lblErrori.setText("The text contains " + errori + " errors"); | ||
} | ||
|
||
@FXML | ||
void initialize() { | ||
assert boxLingua != null : "fx:id=\"boxLingua\" was not injected: check your FXML file 'SpellChecker.fxml'."; | ||
assert txtDaCorreggere != null : "fx:id=\"txtDaCorreggere\" was not injected: check your FXML file 'SpellChecker.fxml'."; | ||
assert txtCorretto != null : "fx:id=\"txtCorretto\" was not injected: check your FXML file 'SpellChecker.fxml'."; | ||
assert lblErrori != null : "fx:id=\"lblErrori\" was not injected: check your FXML file 'SpellChecker.fxml'."; | ||
assert lblStato != null : "fx:id=\"lblStato\" was not injected: check your FXML file 'SpellChecker.fxml'."; | ||
|
||
// Inizializzazione dei componenti | ||
txtDaCorreggere.setText("Selezionare una lingua"); | ||
txtDaCorreggere.setDisable(true); | ||
txtCorretto.setDisable(true); | ||
boxLingua.getItems().addAll("English", "Italian"); | ||
|
||
lblErrori.setText(""); | ||
lblStato.setText(""); | ||
|
||
this.dizionario = new Dictionary(); | ||
|
||
} | ||
} | ||
|
91 changes: 91 additions & 0 deletions
91
Lab3_SpellChecker/src/it/polito/tdp/spellchecker/model/Dictionary.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,91 @@ | ||
package it.polito.tdp.spellchecker.model; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.*; | ||
|
||
public class Dictionary { | ||
|
||
final static boolean dicotomica = false; | ||
|
||
protected List<String> dizionario; | ||
|
||
public Dictionary() { | ||
dizionario = new LinkedList<String>(); | ||
} | ||
|
||
public void loadDictionary(String language) { | ||
try { | ||
|
||
FileReader fr = new FileReader("rsc/" + language + ".txt"); | ||
BufferedReader br = new BufferedReader(fr); | ||
String word; | ||
|
||
while ((word = br.readLine()) != null) { | ||
// Aggiungo word alla struttura dati. | ||
dizionario.add(word.toLowerCase()); | ||
} | ||
|
||
// Ordino il dizionario (per la ricerca dicotomica) | ||
Collections.sort(dizionario); | ||
|
||
br.close(); | ||
|
||
} catch (IOException e) { | ||
|
||
System.out.println("Errore nella lettura del file"); | ||
} | ||
|
||
}; | ||
|
||
public List<RichWord> spellCheckText(List<String> inputTextList) { | ||
|
||
List<RichWord> parole = new LinkedList<RichWord>(); | ||
|
||
RichWord r; | ||
for (String s : inputTextList) { | ||
|
||
if (dicotomica) { | ||
if (binarySearch(s.toLowerCase())) | ||
r = new RichWord(s, true); | ||
else | ||
r = new RichWord(s, false); | ||
parole.add(r); | ||
|
||
} else { | ||
if (dizionario.contains(s.toLowerCase())) | ||
r = new RichWord(s, true); | ||
else | ||
r = new RichWord(s, false); | ||
parole.add(r); | ||
} | ||
} | ||
return parole; | ||
} | ||
|
||
|
||
|
||
/* | ||
* Metodo che implementa la ricerca dicotomica | ||
*/ | ||
private boolean binarySearch(String stemp) { | ||
|
||
int inizio = 0; | ||
int fine = dizionario.size(); | ||
|
||
while (inizio!=fine){ | ||
int medio = inizio + (fine - inizio)/2; | ||
if (stemp.compareToIgnoreCase(dizionario.get(medio))==0){ | ||
return true; | ||
} else if (stemp.compareToIgnoreCase(dizionario.get(medio))>0){ | ||
inizio=medio +1; | ||
} else { | ||
fine=medio; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
Lab3_SpellChecker/src/it/polito/tdp/spellchecker/model/RichWord.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,34 @@ | ||
package it.polito.tdp.spellchecker.model; | ||
|
||
public class RichWord { | ||
|
||
@Override | ||
public String toString() { | ||
return "RichWord [parola=" + parola + ", corretta=" + corretta + "]"; | ||
} | ||
|
||
private String parola; | ||
private boolean corretta; | ||
|
||
public RichWord(String parola, boolean corretta) { | ||
this.corretta = corretta; | ||
this.parola = parola; | ||
} | ||
|
||
public String getParola() { | ||
return parola; | ||
} | ||
|
||
public void setParola(String parola) { | ||
this.parola = parola; | ||
} | ||
|
||
public boolean isCorretta() { | ||
return corretta; | ||
} | ||
|
||
public void setCorretta(boolean corretta) { | ||
this.corretta = corretta; | ||
} | ||
|
||
} |