NLP 7
NLP 7
---
---
stop_words = set(stopwords.words('english'))
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
print(filtered_tokens)
```
---
## 3. **Vectorisation de Texte**
documents = ["I love NLP.", "NLP is amazing.", "I enjoy learning NLP."]
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(documents)
print(vectorizer.get_feature_names_out())
print(tfidf_matrix.toarray())
```
vectorizer = CountVectorizer()
bow_matrix = vectorizer.fit_transform(documents)
print(vectorizer.get_feature_names_out())
print(bow_matrix.toarray())
```
---
## 4. **Analyse de Sentiment**
analyzer = SentimentIntensityAnalyzer()
sentence = "I absolutely love this product. It's fantastic!"
sentiment_score = analyzer.polarity_scores(sentence)
print(sentiment_score)
```
---
## 5. **Classification de Texte**
classifier = pipeline("sentiment-analysis")
text = "I really enjoyed the movie. It was fantastic!"
result = classifier(text)
print(result)
```
data = ["I love NLP.", "I hate math.", "NLP is fun.", "Math is boring."]
labels = ["positive", "negative", "positive", "negative"]
---
nlp = spacy.load("en_core_web_sm")
text = "Apple is looking to buy a startup in the UK for $1 billion."
doc = nlp(text)
---
## 7. **Traduction Automatique**
model_name = "Helsinki-NLP/opus-mt-en-fr"
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)
```python
from googletrans import Translator
translator = Translator()
text = "Natural Language Processing is amazing."
translation = translator.translate(text, src="en", dest="fr")
print(translation.text)
```
---
model_name = "t5-small"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)
---
openai.api_key = "YOUR_API_KEY"
response = openai.Completion.create(
engine="text-davinci-003",
prompt="Explain the importance of NLP in modern technology.",
max_tokens=100
)
print(response.choices[0].text.strip())
```
---