المحاضرة 3 معالجة اللغات الطبيعية
DESCRIPTION
محاضرات يقدمها الدكتور أحمد الحربي لطلاب قسم الحاسب الألي مقرر : معالجة اللغات الطبيعيةTRANSCRIPT
1
معالجة اللغات الطبيعية دكتور/ أحمد الحربى محاضرة (٣)
سريعا، وفى بداية ھذه المحاضرة grammarتكلمنا فى نھاية المحاضرة السابقة عن النحو
:سوف نتكلم عنه بشىء من التفصيل
Grammarالنحو
الجمل بطريقة صحيحة، وھو يتتبر الشىء ھو عبارة عن القواعد المستخدمة فى بناء
:ولنحو مميزات كثيرة نذكر منھا األتى. NLPاألساسى لبناء نظام للـ
.كتابة وقراءة الجمل بطريقة صحيحة - ١
.فھم الجمل ووصفھا بطريقة دقيقة - ٢
).Parser(من خالل الفھم الجيد له يمكن إنشاء برنامج إلعراب الجمل - ٣
.ف لتراكيب الجمليعطى إمكانية وص - ٤
داخلى NLPفأنظمة Object(التى يتم بناؤھا تستطيع ترجمة الجمل المدخلة إلى الترميز ال
Code ( ة ق قواعد النحو، وإكتشاف األخطاء اللغوي ة تطبي م للنظام مما يعطى إمكاني ى ل الت
ق ھى تطبق عليھا القواعد، ويطبق النحو بطرق عديدة لتحديد قواعده، ومن أفضل ھذه الطر
.BNFوتختصر إلى ) Backus Naur Form(طريقة
:ونذكرھم كما يلى Objectsويتكون النحو من أربع
2
ائى - زء النھ ى : Terminalالج ل ف ا الجم ون منھ ى تتك ية الت وز األساس ى الرم وھ
.tokenاللغات الطبيعية، وتسمى
تم إحا) : Non-Terminal(الجزء الالنھائى - ا وھى عبارة عن رموز خاصة ي طتھ
الى ) set of characters(وھى تمثل بأى <..…>بواسطة ال الت أو بكلمات، والمث
:السابق ذكرھم يوضح ھذين النوعين
<SENTENCE> ::= <SUBJECT> likes <OBJECT>
<مفعول به> likes <فاعل> =:: <جملة>
األجزاء ا ىلالف ة ھ ا <OBJECT>و <SUBJECT>و <SENTENCE>: نھائي أم
likes فھى جزء نھائى.
ة - ز البداي راد ): Start Symbol(رم ة الم ة الجمل دد بداي ائى يح زء ال نھ و ج وھ
.فى القاعدة السابق ذكرھا <SENTENCE>معالجتھا مثل
وھى عبارة عن الطرق التى تستخدم فى بناء ) Production Rules(قواعد اإلنتاج -
ة من تراكي ة أخرىتراكيب نحوي ة ب نحوي دة . ومن األجزاء الالنھائي ر القاع وتعتب
.السابقة شكل من أشكال قواعد األنتاج
ة فى قواعد ى تستخدم مع األجزاء الالنھائي د الت ذكر بعض التقالي اج وفيما يلى ن اإلنت
BNF:
.<Verb> , <Noun>: مثل <..…>يتم وضع األجزاء الالنھائية بين العالمتين - ١
.عنصر قادر على تمثيل جزء ال نھائى لتعبر عن *نجمة يتم إستخدام ال - ٢
3
.حالة أو أكثر/إمكانية وجود مثال لتعبر عن+ يتم إستخدام عالمة - ٣
.ORالعالمة المنطقية لتعبر عن| يتم إستخدام عالمة - ٤
ارى ويمكن [..…]يعتبر الجزء الالنھائى المحاط بالقوسين المربعين - ٥ بانه جزء إختي
.رعدم أخذه فى اإلعتبا
Formal Grammar النحو المشكل
ة كلة من مجموعة Stringsوھو عبارة مجموعة من السالسل الحرفي ذات طول محدد مش
:من المفردات اللغوية ويوجد أربع أنواع من النحو ھم
ارة عن مجموعة من قواعد (Type 0 Grammar)نحو من النوع صفر .١ وھو عب
.ع عدم التقيد بشكل ما للقواعدوية ماألنتاج مكونة من رموز ومفردات لغ
ى الشكل وھو (Type 1 Grammar) ١نحو من النوع .٢ Xيكون عل Y ويكون
ن ودة Yالطرف األيم وز الموج دد الرم اويا لع وز مس ن الرم دد م ى ع ا عل محتوي
Context-sensitive and Type 0(ويسمى ھذا النوع . Xبالطرف األيسر
grammars.(
) Context-Free Grammar(ويسمى ) Type 2 Grammar( ٢نحو من النوع .٣
، وھذا النوع يجب أن تحتوى كل قاعدة إنتاج على رمز جزء ال نھائى واحد فقط فى
د بعض ذى يول اال لسياق النحو الحر ال الى يوضح مث الطرف األيسر، والجدول الت
ل ون مث ة تك زاء النھائي ث أن األج ل، حي و womanو man و aو the: الجم
study وwrite حيث رمز البداية ھو ،<S>.
4
<S> <NP> <VP>
<NP> <ART> <N>
<VP> <V> <NP>
<ART> the, a, …..
<N> man, woman, …..
<V> study, write, …..
كل ى ش ل ف و يمث و Treeوھ ياق النح مى س د يس ة، وق ات الطبيعي ة اللغ رامج معالج ى ب ف
و ر النح و الح ياق النح اس وس ة الحس ب الجمل ى تركي ى عل Phrase-Structured(المبن
Grammar.(
:وقواعد اإلنتاج تكون على الشكل التالى) Type 3 Grammar( ٣نحو النوع .٤
X aY or X a
جزء نھائى مفرد، وھذا النوع يمثل النحو aمتغيرات مفردة و Yو Xحيث أن
جموعة السالسل الحرفية لعنصر النظامى، على سبيل المثال يمكن أن يمثل ھذا النوع م
a واحد أو أكثر متبوعا بعنصرb واحد أو أكثر ، مع عدم ضمان عدد متساو منa's أو
:كما يلى b'sمن
S aS, S aT, T b, T bT
5
Search
state-space search: idea that problem-solving consists of moving
from one state to another.
solution path: path of states that leads to a solution.
search space: set of all possible solution paths.
depth-first search: explore one path fully before exploring others.
breadth-first search: explore all paths simultaneously branching
factor: number of options (branches) at each decision point.
Search Space: TIC-TAC-TOE
6
Parsing as Search
Parsing can be viewed as searching through the space of all
possible parse trees recognized by a grammar to find the correct parse
tree for the sentence.
Example: The smelly dog drank the water.
Grammar Lexicon S NP VP the: ART NP ART N smelly: ADJ NP ART ADJ N dog: V N VP V drank: V VP V NP water: V N
Parsing Techniques
ى كلمات م ة إل ة تجزى الجمل ة إعراب تقنيات اإلعراب عبارة عن عملي ع معرف
ر و الح د النح ياق قواع ا لس ك طبق تم ذل ات وي اقى الكلم ا بب ة وعالقتھ ل كلم ك
)context-free grammar( حيث يقوم ،parser ة بتميز تركيب الجمل ومعرف
.كلماتھا
Top-down Parsing nominal
7
وم : اإلعراب من القمة ألسفل ة عن مستوى الق parserيق ة المطلوب ا عن التركيب م بحث ة، ث م
ة ة كامل ى يكمل تركيب ة ويستمر حت ة مستوى القم ات تركيب يبحث عن قواعد لكل من مكون
ة(يبدأ بقاعدة اإلنتاج األكثر عمومية parserحيث أن . للجملة المدخلة فى النحو ) رمز البداي
.ويحاول أن ينتج مجموعة من من قواعد اإلنتاج التى تولد الجملة
- A top-down parser begins with the S symbol and successively
rewrites each non-terminal symbol by applying a grammar rule.
- Top-down parsing is based on expectations provided by the
grammar.
Bottom-up Parsing
لة المدخلة فى بتصفح قواعد النحو ليأخذ كلمات الجم parserيقوم : ألعلى اسفلاإلعراب من
شكل عبارات ومقاطع ويستمر فى المحاولة إلى تكوين أجزاء التركيبة ليبن كيف تشكل جميع
.دخلة لجملة صحيحة فى النحومكلمات الجملة ال
- A bottom-up parser begins with the terminal symbols of the input
sentence. The parser successively rewrites a sequence of terminal
and/or non-terminal symbols with the left-hand side of a grammar
rule (always a single non-terminal in CFGs).
- Bottom-up parsing uses the input symbols to constrain search.
8
Example: The following figure is 10.4, it describe how the
parser begins by looking up each word (book, that, and flight) in the
lexicon and building three partial trees with the part-of-speech for
each word.
.treesبعمل مجموعتين من parserقد تكون فعل أو أسم ولھذا يقوم bookولكن كلمة
Figure 10.4: An expanding bottom-up search space for the sentence: Book that flight.
9
Noise Disposal Parsing
راب رورىاإلع ر ض ال الغي ن الط: بإھم تخدم أى م راب يس ن اإلع وع م ذا الن ريقتين ھ
ا وى الجمل عليھ ى تحت ية الت لإلعراب مع التطبيقات التى تھتم بعدد قليل من الكلمات األساس
وى .وال تھتم بكافة الكلمات الموجودة فى الجملة ى تحت وھذه األنواع تھتم فقط بالمعلومات الت
.عليھا الجملة أكثر من مكونات الجملة
دة خارج ن ر مفي ا غي ة أنھ ذه الطريق وب ھ ة، وأن ومن عي ات معين ل Parserطاق تطبيق يقب
ذه وخصوصا . جمال قد تكون شاذة وغريبة وع ھو سھولة تنفي ولكن الميزة الھامة فى ھذا الن
.فى إستخالص المعلومات من الرسائل بسرعة
Chart Parsing
- General search methods are not best for syntactic parsing because
the same syntactic constituent may be rederived many times as a part
of different larger constituents.
For example, an NP could be part of different VPs or PPs.
- Chart parsing uses a chart to keep track of partial derivations so
nothing has to be rederived.
- Chart parsers also use an agenda to prioritize the constituents to be
processed. The agenda can be implemented as a stack or a queue, to
simulate a depth-first or breadth-first search strategy.
10
To simulate a true breadth-first strategy, the whole sentence
must be scanned so that each word can be placed on the agenda before
parsing begins.
Bottom-up Chart Parsing
1. If the agenda is empty, look up the next word in the lexicon and add
its possible parts-of-speech to the agenda. Include its position in
the input sentence.
For example, if the first word is an article then it would be
added as ART (1-2).
2. Select a constituent C from the agenda. We'll refer to the positions
that it spans as p1-p2.
3. Add C to the chart.
Top-down vs. Bottom-up Chart Parsing
- Bottom-up chart parsing checks the input and builds each constituent
exactly once. Avoids duplication of effort!
11
- But bottom-up chart parsing may build constituents that cannot be
used legally.
For example, given "the can", bottom-up parsing will build a
VP for "can" even if no grammar rules allow a VP to follow an article.
- Top-down chart parsing is highly predictive. Only grammar rules
that can be legally applied will be put on the chart.
- We can exploit the best of both worlds by using a top-down
approach with the bottom-up chart parsing strategy.
Top-Down Chart Parsing Algorithm
Initialize: For each grammar rule with S as its left-hand side, add the
arc S 0 X1…Xk to the chart using the top-down arc introduction
algorithm.
While there are input words left:
1. If the agenda is empty, add the next word to the agenda.
2. Select a constituent C from the agenda.
3. Combine C with every active arc on the chart. Add any new
constituents to the agenda.
4. For any active arcs created by step 3, add them to the chart using
the top-down arc introduction algorithm.
12
Syntactic Ambiguities
Syntactic ambiguities can cause the number of possible parse trees to
explode.
- PP attachment ambiguity:
I saw the man on the hill with a telescope.
- Noun phrase bracketing:
plastic cat food can cover
- Conjunctions and appositives:
Rover, my fish, and Fluffy
A medium-sized sentence with a moderate grammar can have over
1000 legal parse trees!
Dealing with Syntactic Ambiguity
One approach to minimizing syntactic ambiguity is to modify the
grammar. Consider this grammar:
NP NP1
NP1 noun
NP1 NP1 NP1
13
versus this grammar rule (if the Kleene + operator is legal):
NP noun+
or if the Kleene + operator is not legal:
NP noun
NP noun noun
NP noun noun noun
NP noun noun noun noun
These grammar rules look more complicated but eliminate all
structural ambiguity!
Packing
- Another way to reduce structural ambiguity is to use a packed chart.
- Packing combines constituents of the same type that include the
same words, no matter how they were derived.
For example, [[cat food] can] and [cat [food can]]
would be combined into the same NP.
- You can keep track of the different derivations for a constituent even
when the chart contains only one item representing them all.
Parsing Preferences: Minimal Attachment
14
The minimal attachment principle states that humans prefer
attachments that produce the fewest number of nodes in the parse tree.
For example: The boy walked the dog in the park.
Sometimes you can feel this preference strongly biasing one
interpretation over another, for example:
We painted all the walls with cracks.
Parsing Preferences: Right Association
The right association principle states that humans prefer that new
constituents be interpreted as part of the current constituent rather than
an earlier one.
For example: John wrote that Elvis died in Australia.
or She asked if the Jazz won the basketball game yesterday.
Parsing Preferences: Lexical
- The principles of minimal attachment and right association are
sometimes at odds with one another. For example:
The boy walked the dog in the park.
15
Right association prefers that "in the park" attaches to "dog", while
minimal attachment prefers "walked".
- Consider what happens when just a verb changes:
I wanted the dog in the store.
I kept the dog in the store.
I put the dog in the store.
- Lexical preferences will usually trump general principles!
Recursive Transition Networks (RTNs)
- Simple transition networks are finite state machines (FSMs)
- FSMs can recognize regular grammars.
- To recognize context-free grammars (CFGs), we need recursion. An
RTN allows arc labels that refer to other networks.
What makes a transition network?
A recursive transition network consists of nodes (states) and
labeled arcs (transitions). An RTN can have 5 types of arcs:
CAT: current word must belong to category (e.g., verb)
16
WORD: current word must match label exactly (e.g., \dog")
PUSH: named network must be successfully traversed
JUMP: can always be traversed
POP: can always be traversed and indicates that input string has been
accepted by the network
One state is specified as a start state. A string is accepted by an RTN
if a pop arc is reached and all the input has been consumed.
The metal can was dark blue.
the: ART was: VERB metal: ADJ NOUN dark: ADJ can: VERB NOUN blue: ADJ NOUN
Adding Features to a Grammar
- Simple context-free grammars only use general syntactic categories,
such as parts-of-speech and phrases (e.g, NP/VP/PP).
- But natural languages have many other constraints, such as number
and person agreement.
17
- The lexicon and morphological analyzer can associate features with
words.
- More complicated CFGs can include constraints based on features.
Parsing techniques can be easily modified to use the feature
constraints.
Individual verbs have different expectations for what can follow:
SubCat Verb Example none sleep Mary slept. NP write Mary wrote a book. NP NP give Mary gave Rover a treat. NP PP:to give Mary gave a treat to Rover. NP NP buy Mary bought Rover a treat. NP PP:for buy Mary bought a treat for Rover. loc live Mary lives in Chicago. path walk Mary walked to the store. NP loc put Mary put a book on the table. VP[inf] try Mary tried to win. S:that believe Mary believes that Elvis is alive.