Update README.md
Browse files
README.md
CHANGED
|
@@ -45,34 +45,63 @@ The model achieved the following performance on the test set:
|
|
| 45 |
|
| 46 |
## 🚀 Usage Example
|
| 47 |
|
|
|
|
| 48 |
```python
|
|
|
|
| 49 |
import pickle
|
| 50 |
-
import numpy as np
|
| 51 |
from tensorflow.keras.models import load_model
|
| 52 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
tokenizer = pickle.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
return labels[label]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
aspect = "Servis"
|
| 75 |
-
print(f"Sentiment for '{aspect}': {predict_sentiment(sentence, aspect)}")
|
| 76 |
````
|
| 77 |
|
| 78 |
## 🏋️♀️ Training Details
|
|
|
|
| 45 |
|
| 46 |
## 🚀 Usage Example
|
| 47 |
|
| 48 |
+
Download model from HF
|
| 49 |
```python
|
| 50 |
+
from huggingface_hub import hf_hub_download
|
| 51 |
import pickle
|
|
|
|
| 52 |
from tensorflow.keras.models import load_model
|
|
|
|
| 53 |
|
| 54 |
+
model_path = hf_hub_download(repo_id="Sengil/Turkish-ABSA-BiLSTM-Word2Vec", filename="absa_bilstm_model.keras")
|
| 55 |
+
tokenizer_path = hf_hub_download(repo_id="Sengil/Turkish-ABSA-BiLSTM-Word2Vec", filename="tokenizer.pkl")
|
| 56 |
+
|
| 57 |
+
# load model
|
| 58 |
+
model = load_model(model_path)
|
| 59 |
+
|
| 60 |
+
# load tokenizer
|
| 61 |
+
with open(tokenizer_path, "rb") as f:
|
| 62 |
tokenizer = pickle.load(f)
|
| 63 |
+
````
|
| 64 |
+
|
| 65 |
+
Input preprocessing
|
| 66 |
+
```python
|
| 67 |
+
import re
|
| 68 |
+
import nltk
|
| 69 |
+
nltk.download('punkt')
|
| 70 |
+
|
| 71 |
+
def preprocess_turkish(text):
|
| 72 |
+
text = text.lower()
|
| 73 |
+
text = re.sub(r"http\S+|www\S+|https\S+", "<url>", text)
|
| 74 |
+
text = re.sub(r"@\w+", "<user>", text)
|
| 75 |
+
text = re.sub(r"[^a-zA-Z0-9çğıöşüÇĞİÖŞÜ\s]", " ", text)
|
| 76 |
+
text = re.sub(r"(.)\1{2,}", r"\1\1", text)
|
| 77 |
+
text = re.sub(r"\s+", " ", text).strip()
|
| 78 |
+
return text
|
| 79 |
+
````
|
| 80 |
|
| 81 |
+
Predict the input
|
| 82 |
+
```python
|
| 83 |
+
import numpy as np
|
| 84 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 85 |
+
|
| 86 |
+
def predict_sentiment(sentence, aspect, max_len=84):
|
| 87 |
+
input_text = sentence + " [ASP] " + aspect
|
| 88 |
+
cleaned = preprocess_turkish(input_text)
|
| 89 |
+
tokenized = tokenizer.texts_to_sequences([cleaned])
|
| 90 |
+
padded = pad_sequences(tokenized, maxlen=max_len, padding='post')
|
| 91 |
+
|
| 92 |
+
pred = model.predict(padded)
|
| 93 |
+
label = np.argmax(pred)
|
| 94 |
+
labels = {0: "Negatif", 1: "Nötr", 2: "Pozitif"}
|
| 95 |
return labels[label]
|
| 96 |
+
````
|
| 97 |
+
|
| 98 |
+
run
|
| 99 |
+
```python
|
| 100 |
+
sentence = "Manzara sahane evet ama servis rezalet."
|
| 101 |
+
aspect = "manzara"
|
| 102 |
|
| 103 |
+
predict = predict_sentiment(sentence, aspect)
|
| 104 |
+
print("predict:", predict)
|
|
|
|
|
|
|
| 105 |
````
|
| 106 |
|
| 107 |
## 🏋️♀️ Training Details
|