Lexical Semantics
Nlp tutorial · PySpark.in
1. Lexical Semantics
The study of word meaning falls under the domain of lexical semantics. When we speak of what a word "means," we are engaging in one of the oldest and most fundamental inquiries in linguistics. Words are not merely arbitrary symbols they carry rich, structured meaning that connects them to the world and to one another.
```
word_meaning = {
"car": "a vehicle used for transport",
"mouse": "a small animal or a computer device"
}
word = "mouse"
print("Word:", word)
print("Meaning:", word_meaning[word])
```
2.Lemmas and Word Forms
The Lemma (Citation Form)
A lemma is the canonical, base form of a word , the form you would look up in a dictionary. It is also called the citation form. For example, mouse is the lemma. The dictionary entry for mouse covers all its uses, whether referring to a small rodent or a computer device.
Wordforms
The specific inflected or derived forms of a lemma are called wordforms.
sings, sang, sung are all wordforms of the lemma sing. carpets and carpet — the latter is the lemma; the former is a wordform.
Key insight: Dictionaries define lemmas, not every wordform. In many languages, the infinitive serves as the lemma for verbs. In Spanish, dormir ("to sleep") is the lemma for wordforms like duermes ("you sleep").
```
word_forms = {
"sing": ["sing", "sings", "sang", "sung"],
"mouse": ["mouse", "mice"]
}
word = "sang"
for lemma, forms in word_forms.items():
if word in forms:
print("Word form:", word)
print("Lemma:", lemma)
```
3. Word Senses and Polysemy
A single lemma can carry multiple distinct meanings, each called a word sense.
Polysemy
When a word has multiple senses, it is said to be polysemous.
The lemma mouse has at least two senses:
- Sense 1: A small rodent
- Sense 2: A hand-operated device for controlling a computer cursor
This creates interpretive challenges. If someone searches "mouse info," are they looking for a pet, or a peripheral device? This ambiguity is a central problem in NLP.
```
word_senses = {
"mouse": ["small rodent", "computer pointing device"]
}
word = "mouse"
print("Word:", word)
print("Senses:")
for sense in word_senses[word]:
print("-", sense)
print("This word is polysemous because it has more than one meaning.")
```
Word Sense Disambiguation (WSD)
The task of determining which sense of a polysemous word is intended in a given context is called Word Sense Disambiguation. It is a classic and important problem addressed extensively in computational linguistics.
```
sentence = "I clicked the mouse to open the file"
if "clicked" in sentence or "file" in sentence:
print("mouse means computer device")
elif "cat" in sentence:
print("mouse means small rodent")
else:
print("meaning unclear")
```
4. Synonymy
Synonymy is the relationship between two word senses whose meanings are identical . Two words are considered synonymous if one can be substituted for the other in any sentence without altering the truth conditions that is, without changing whether the sentence is true or false. "I drive a car to work."--- "I drive an automobile to work." (Truth preserved)
Examples of synonym pairs:
- couch / sofa
- vomit / throw up
- filbert / hazelnut
- car / automobile
An Important Nuance
While truth conditions may be preserved, meaning is not always perfectly identical. Consider: water vs. H₂O. Both refer to the same substance, yet H₂O belongs to scientific discourse while water is appropriate in everyday conversation. Substituting one for the other may feel stylistically or contextually wrong.
```
synonyms = {
"car": "automobile",
"sofa": "couch"
}
word1 = "car"
word2 = "automobile"
if synonyms[word1] == word2:
print(word1, "and", word2, "are synonyms")
else:
print(word1, "and", word2, "are not synonyms")
```
Nuance in Synonymy
```
word = "H2O"
context = "daily conversation"
if word == "H2O" and context == "daily conversation":
print("Correct meaning, but too scientific for daily conversation.")
elif word == "water":
print("Natural word for daily conversation.")
```
5. The Principle of Contrast
One of the foundational tenets of semantics is the Principle of Contrast, which holds that:
Any difference in linguistic form signals a difference in meaning.
This means that no two words however similar are ever absolutely identical in meaning. There is always some dimension (register, connotation, genre, formality) along which they differ.
```
word1 = "kid"
word2 = "child"
print(word1, "and", word2, "have similar meaning.")
print("But they are not exactly same.")
print("kid = informal")
print("child = formal")
```
6. Word Similarity
While most words lack true synonyms, many possess similar words. For instance, cat is not a synonym of dog, but they are certainly similar words.
What is Word Similarity?
Word similarity measures how alike two words are in meaning — not identical, but close. It is useful for:
- Question answering
- Paraphrasing
- Machine translation
Measuring Similarity — SimLex-999
Datasets like SimLex-999 ask humans to rate word similarity on a scale of 0 to 10:
Word Pair | Similarity Score |
|---|---|
vanish / disappear | 9.8 — nearly identical |
belief / impression | 5.95 — somewhat related |
muscle / bone | 3.65 — loosely related |
modest / flexible | 0.98 — barely related |
hole / agreement | 0.3 — almost nothing in common |
Similarity exists on a spectrum, not as a binary yes/no.
```
similarity_scores = {
("vanish", "disappear"): 9.8,
("belief", "impression"): 5.95,
("muscle", "bone"): 3.65,
("modest", "flexible"): 0.98,
("hole", "agreement"): 0.3
}
word1 = "vanish"
word2 = "disappear"
score = similarity_scores.get((word1, word2), "Score not found")
print("Word pair:", word1, "/", word2)
print("Similarity score:", score)
if score >= 8:
print("Meaning: Very similar")
elif score >= 5:
print("Meaning: Somewhat similar")
elif score >= 2:
print("Meaning: Loosely similar")
else:
print("Meaning: Not similar")
```
7.Word Relatedness
Relatedness is broader than similarity. Two words can be related without being similar at all.
Similarity vs. Relatedness
Concept | Definition | Example |
|---|---|---|
Similarity | Words share features or meaning | cat / dog (both animals) |
Relatedness | Words are connected in any way | coffee / cup (not similar, but deeply linked) |
``` similar_words = [("cat", "dog"), ("car", "bus")] related_words = [("coffee", "cup"), ("doctor", "hospital")] word1 = "coffee" word2 = "cup" if (word1, word2) in similar_words: print(word1, "and", word2, "are similar words.") elif (word1, word2) in related_words: print(word1, "and", word2, "are related words.") else: print(word1, "and", word2, "are not strongly related.") print("Similarity means same type/category.") print("Relatedness means connected in some way.") ``` |
8.Association
One classic type of relatedness is association is a psychological connection between concepts.
coffee and cup are not similar (one is a drink, the other is an object), but they are strongly associated through everyday experience — drinking coffee from a cup.
scalpel and surgeon are not similar, but are related through their shared role in a medical context.
```
associations = {
"coffee": ["cup", "cafe", "morning", "sugar"],
"surgeon": ["scalpel", "operation", "hospital"],
"student": ["book", "exam", "teacher"]
}
word = "coffee"
print("Word:", word)
print("Associated words:")
for associated_word in associations[word]:
print("-", associated_word)
```
9.Semantic Field
A semantic field is a set of words that together cover a particular conceptual domain, bearing structured relations with each other.
Examples of Semantic Fields:
Domain | Words in the Field |
|---|---|
Hospital | surgeon, scalpel, nurse, anaesthetic, hospital |
Restaurant | waiter, menu, plate, food, chef |
Housing | door, roof, kitchen, family, bed |
Words within a semantic field illuminate topical structure in documents — this is the foundation of topic models like Latent Dirichlet Allocation (LDA).
```
semantic_fields = {
"Hospital": ["surgeon", "scalpel", "nurse", "anaesthetic", "patient"],
"Restaurant": ["waiter", "menu", "plate", "food", "chef"],
"Housing": ["door", "roof", "kitchen", "family", "bed"]
}
domain = "Restaurant"
print("Semantic Field:", domain)
print("Words in this field:")
for word in semantic_fields[domain]:
print("-", word)
```
10.Connotation & Affective Meaning
Words carry not just denotative (dictionary) meaning but also affective meaning — emotional associations and feelings.
What is Connotation?
Connotation refers to the aspects of a word's meaning tied to emotions, sentiments, and evaluations.
Wonderful → positive connotation Dreary → negative connotation Fake, knockoff, forgery → negative connotations Copy, replica, reproduction → neutral or positive Innocent → positive; naive → negative (same core meaning, different feel!)
```
connotation = {
"wonderful": "positive",
"dreary": "negative",
"fake": "negative",
"replica": "neutral",
"innocent": "positive",
"naive": "negative"
}
word = "naive"
print("Word:", word)
print("Connotation:", connotation[word])
if connotation[word] == "positive":
print("This word gives a good feeling.")
elif connotation[word] == "negative":
print("This word gives a bad feeling.")
else:
print("This word is neutral.")
```
11. Sentiment
The study of positive or negative evaluative language is called sentiment analysis — one of the most widely applied areas of NLP, used in:
- Product and movie review analysis
- Social media monitoring
- Customer feedback processing
- Stance detection
```
positive_words = ["good", "great", "excellent", "wonderful", "happy", "love"]
negative_words = ["bad", "poor", "terrible", "dreary", "sad", "hate"]
sentence = "The movie was wonderful and excellent"
sentence_words = sentence.lower().split()
positive_count = 0
negative_count = 0
for word in sentence_words:
if word in positive_words:
positive_count += 1
elif word in negative_words:
negative_count += 1
print("Sentence:", sentence)
print("Positive words:", positive_count)
print("Negative words:", negative_count)
if positive_count > negative_count:
print("Sentiment: Positive")
elif negative_count > positive_count:
print("Sentiment: Negative")
else:
print("Sentiment: Neutral")
```
More Nlp tutorials
- Natural Language Understanding (NLU) , Natural Language Generation (NLG) and phases of NL
- Tokenization in NLP and NLP Project Life Cycle
- Coverting The Text to Vector(one hot encoding and bag of words method)
- Convert text to vector: N-grams and TF-IDF method
- Word Embedding
- What is Natural Language Processing ?
All tutorials · Try the free PySpark compiler · Practice challenges