I have recently been investigating how I can use RAG to improve the accuracy of LLM-based machine translation. In this article, I will take Knowledge Graphs as an advanced form of RAG and demonstrate how combining knowledge graphs with large language models can significantly improve machine translation accuracy, particularly for domain-specific content.. The article basically provides a “toy example” of the use of a Knowledge Graph to generate a translation of a short French text. The working Python code given below has been generated by Claude.ai.
The Challenge
Traditional machine translation systems often struggle with:
- Domain-specific terminology
- Proper nouns and entity names
- Context-dependent translations
- Technical jargon
The Solution: Knowledge Graph + LLM
By integrating a knowledge graph that stores entities and their relationships, we can provide contextual information to the translation model, resulting in more accurate and contextually appropriate translations.
Implementation
Here’s a complete implementation of a knowledge graph-enhanced French-to-English translator using Python and Ollama:
Knowledge Graph Class
class KnowledgeGraph:
"""Simple knowledge graph for storing entities and relationships"""
def __init__(self):
self.entities = {}
self.relationships = []
def add_entity(self, entity_id: str, entity_type: str, properties: Dict):
"""Add an entity to the knowledge graph"""
self.entities[entity_id] = {
'type': entity_type,
'properties': properties
}
def add_relationship(self, source: str, relation: str, target: str):
"""Add a relationship between entities"""
self.relationships.append({
'source': source,
'relation': relation,
'target': target
})
def get_entity_context(self, entity_id: str) -> str:
"""Get contextual information about an entity"""
if entity_id not in self.entities:
return ""
entity = self.entities[entity_id]
context = f"{entity_id} is a {entity['type']}"
# Add properties
for key, value in entity['properties'].items():
context += f", {key}: {value}"
# Add relationships
related = [r for r in self.relationships if r['source'] == entity_id]
if related:
context += ". Related to: "
context += ", ".join([f"{r['target']} ({r['relation']})" for r in related])
return context
def find_entities_in_text(self, text: str) -> List[str]:
"""Find entities mentioned in text (simple string matching)"""
found = []
text_lower = text.lower()
for entity_id in self.entities.keys():
if entity_id.lower() in text_lower:
found.append(entity_id)
return found
Enhanced Translator Class
class KGEnhancedTranslator:
"""Translator that uses knowledge graph context"""
def __init__(self, model_name: str = "gemma2:2b"):
self.model_name = model_name
self.kg = KnowledgeGraph()
def add_domain_knowledge(self, entities: List[Dict], relationships: List[Dict]):
"""Populate the knowledge graph with domain knowledge"""
for entity in entities:
self.kg.add_entity(
entity['id'],
entity['type'],
entity['properties']
)
for rel in relationships:
self.kg.add_relationship(
rel['source'],
rel['relation'],
rel['target']
)
def translate(self, french_text: str) -> Dict[str, str]:
"""Translate French text to English using KG context"""
# Find relevant entities in the text
relevant_entities = self.kg.find_entities_in_text(french_text)
# Build context from knowledge graph
kg_context = ""
if relevant_entities:
kg_context = "Context information:\n"
for entity in relevant_entities:
kg_context += f"- {self.kg.get_entity_context(entity)}\n"
# Create prompt with KG context
prompt = f"""You are a French to English translator. Use the provided context to ensure accurate translation of domain-specific terms and proper nouns.
{kg_context}
Translate the following French text to English. Maintain the original meaning and use appropriate technical terminology based on the context provided.
French text: {french_text}
English translation:"""
# Call Ollama
response = ollama.chat(
model=self.model_name,
messages=[{
'role': 'user',
'content': prompt
}]
)
return {
'original': french_text,
'translation': response['message']['content'].strip(),
'context_used': kg_context,
'entities_found': relevant_entities
}
Example Usage
import ollama
from typing import Dict, List
def main():
"""Example usage of KG-enhanced translation"""
# Initialize translator
translator = KGEnhancedTranslator(model_name="gemma2:2b")
# Add domain knowledge about a fictional tech company
entities = [
{
'id': 'TechCorp',
'type': 'company',
'properties': {
'industry': 'technology',
'headquarters': 'Paris',
'founded': '2010'
}
},
{
'id': 'Marie Dubois',
'type': 'person',
'properties': {
'role': 'CEO',
'nationality': 'French'
}
},
{
'id': 'QuantumCore',
'type': 'product',
'properties': {
'type': 'quantum processor',
'release_year': '2024'
}
}
]
relationships = [
{'source': 'Marie Dubois', 'relation': 'works_at', 'target': 'TechCorp'},
{'source': 'Marie Dubois', 'relation': 'leads', 'target': 'TechCorp'},
{'source': 'TechCorp', 'relation': 'produces', 'target': 'QuantumCore'}
]
translator.add_domain_knowledge(entities, relationships)
# Example French texts to translate
test_texts = [
"Marie Dubois a annoncé que TechCorp lancera QuantumCore le mois prochain.",
"Le nouveau processeur quantique révolutionnera l'informatique.",
"TechCorp est basé à Paris et emploie plus de 500 personnes."
]
for i, text in enumerate(test_texts, 1):
print(f"\n--- Example {i} ---")
result = translator.translate(text)
print(f"\nOriginal (FR): {result['original']}")
print(f"\nTranslation (EN): {result['translation']}")
if result['entities_found']:
print(f"\nEntities detected: {', '.join(result['entities_found'])}")
print(f"\nContext used:\n{result['context_used']}")
else:
print("\nNo entities detected")
if __name__ == "__main__":
main()
Key Benefits
✅ Contextual Accuracy: The system understands domain-specific entities and their relationships
✅ Proper Noun Handling: Names and technical terms are translated appropriately
✅ Scalable: Easy to extend the knowledge graph with new entities and relationships
✅ Transparent: The system shows which entities were detected and what context was used
Use Cases
This approach is particularly valuable for:
- Technical documentation translation
- Business communications with specific terminology
- Academic papers with domain-specific concepts
- News articles about known entities and organizations
Next Steps
To extend this system, consider:
- Implementing more sophisticated entity recognition (NER)
- Adding support for additional languages
- Integrating with enterprise knowledge bases
- Implementing caching for frequently translated entities
Dependencies: ollama, Python 3.7+
The complete code is available and ready to use. Feel free to adapt it for your specific translation needs!
For more extensive use of this approach, I can be reached at support@localai.world.
#MachineLearning #NLP #AI #Translation #KnowledgeGraphs #Python




