Skip to content

لغة #35

Description

@mohmeed132

-- coding: utf-8 --

سكربت تحليل تغيّر البرادغم باستبدال كلمة بأخرى

from gensim.models import Word2Vec
from sklearn.metrics.pairwise import cosine_similarity
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

-----------------------------

1) إدخال النصوص (جُمل مبدئية)

-----------------------------

corpus = [
["العقل","يَقود","الإنسان"],
["الرغبة","تُملي","على","الإنسان"],
["التاريخ","يَكْتُب","الحاضر"],
["الأسطورة","تَصنع","المعنى"],
["العقل","يُضيء","الطريق"],
["الرغبة","تُحرّك","الإرادة"],
]

-----------------------------

2) تدريب Word2Vec

-----------------------------

w2v = Word2Vec(sentences=corpus, vector_size=50, window=5, min_count=1, workers=2, epochs=100)

-----------------------------

3) بناء الرسم الأصلي

-----------------------------

vocab = list(w2v.wv.index_to_key)
embs = np.array([w2v.wv[w] for w in vocab])
S = cosine_similarity(embs)

def build_graph(S, vocab, k=3):
G = nx.Graph()
for i, w in enumerate(vocab):
G.add_node(w)
neighbors_idx = np.argsort(-S[i])[:k+1]
for j in neighbors_idx:
if i==j: continue
G.add_edge(w, vocab[j], weight=float(S[i,j]))
return G

G0 = build_graph(S, vocab)

-----------------------------

4) استبدال كلمة A بـ B

-----------------------------

A, B = "العقل", "الرغبة"
embs_swapped = embs.copy()
idxA = vocab.index(A)
embs_swapped[idxA] = w2v.wv[B]
S2 = cosine_similarity(embs_swapped)
G1 = build_graph(S2, vocab)

-----------------------------

5) حساب المؤشرات

-----------------------------

deg0 = nx.degree_centrality(G0)
deg1 = nx.degree_centrality(G1)

bet0 = nx.betweenness_centrality(G0, weight='weight')
bet1 = nx.betweenness_centrality(G1, weight='weight')

delta_deg = {w: deg1[w] - deg0[w] for w in vocab}
delta_bet = {w: bet1[w] - bet0[w] for w in vocab}

print("\n🔹 تغيّر الدرجة المركزية:")
for w, d in delta_deg.items():
print(f"{w}: {d:.3f}")

print("\n🔹 تغيّر الوسطيّة (betweenness):")
for w, d in delta_bet.items():
print(f"{w}: {d:.3f}")

-----------------------------

6) التصوير

-----------------------------

pos = nx.spring_layout(G0, seed=42)

plt.figure(figsize=(12,6))

plt.subplot(1,2,1)
nx.draw(G0, pos, with_labels=True, node_color="skyblue", edge_color="gray", node_size=1200, font_family="Arial")
plt.title("الشبكة الأصلية")

plt.subplot(1,2,2)
nx.draw(G1, pos, with_labels=True, node_color="lightgreen", edge_color="gray", node_size=1200, font_family="Arial")
plt.title(f"بعد استبدال {A} ← {B}")

plt.show()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions