-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ar] Fix some bugs and add new rules #6785
Closed
Closed
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cc30352
[ar] Fix https://github.com/sohaibafifi/languagetool/issues/19 and ad…
linuxscout c17eca7
[ar] update headers
sohaibafifi 34fa6c5
[ar] update CHANGES.md
sohaibafifi 4fea22e
[ar] cleanup the code
sohaibafifi f075f90
[ar] cleanup the code according to reviews
sohaibafifi c1d152d
[ar] remove empty lines
sohaibafifi ac78e8a
[ar] Fix code styling
sohaibafifi abe332a
[ar] Fix code styling
sohaibafifi 4d7fa67
[ar] Fix code styling
sohaibafifi 6676b94
[ar] Fix code styling
sohaibafifi cb3fe36
Merge branch 'master' into 5.8
sohaibafifi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
...uage-modules/ar/src/main/java/org/languagetool/rules/ar/AdjectiveToExclamationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
/* LanguageTool, a natural language style checker | ||
* Copyright (C) 2022 Sohaib Afifi, Taha Zerrouki | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 | ||
* USA | ||
*/ | ||
package org.languagetool.rules.ar; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
import org.languagetool.AnalyzedTokenReadings; | ||
import org.languagetool.rules.RuleMatch; | ||
import org.languagetool.rules.SimpleReplaceDataLoader; | ||
import org.languagetool.rules.patterns.RuleFilter; | ||
import org.languagetool.tagging.ar.ArabicTagger; | ||
import org.languagetool.tools.ArabicWordMaps; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* Filter that maps suggestion from adverb to adjective. | ||
* | ||
* @since 5.8 | ||
*/ | ||
public class AdjectiveToExclamationFilter extends RuleFilter { | ||
|
||
public AdjectiveToExclamationFilter() { | ||
this.adj2compList = loadFromPath(FILE_NAME); | ||
} | ||
|
||
private final ArabicTagger tagger = new ArabicTagger(); | ||
private static final String FILE_NAME = "/ar/arabic_adjective_exclamation.txt"; | ||
private final Map<String, List<String>> adj2compList; | ||
|
||
private final Map<String, String> adj2comp = new HashMap<String, String>() {{ | ||
// tri letters verb: | ||
put("رشيد", "أرشد"); | ||
put("طويل", "أطول"); | ||
put("بديع", "أبدع"); | ||
// | ||
// TODO: add more Masdar verb | ||
//put("", ""); | ||
}}; | ||
|
||
|
||
@Nullable | ||
@Override | ||
public RuleMatch acceptRuleMatch(RuleMatch match, Map<String, String> arguments, int patternTokenPos, AnalyzedTokenReadings[] patternTokens) { | ||
|
||
// This rule return only the comparative according to given adjective | ||
String adj = arguments.get("adj"); // extract adjective | ||
String noun = arguments.get("noun"); // the second argument | ||
int adjTokenIndex; | ||
try { | ||
adjTokenIndex = Integer.valueOf(arguments.get("adj_pos")) - 1; | ||
} catch (NumberFormatException e) { | ||
throw new RuntimeException("Error parsing adj_pos from : " + arguments.get("adj_pos")); | ||
} | ||
|
||
// filter tokens which have a lemma of adjective | ||
|
||
// some cases can have multiple lemmas, but only adjective lemma are used | ||
List<String> adjLemmas = tagger.getLemmas(patternTokens[adjTokenIndex], "adj"); | ||
|
||
// get comparative from Adj/comp list | ||
List<String> compList = new ArrayList<>(); | ||
|
||
for (String adjlemma : adjLemmas) { | ||
// get comparative suitable to adjective | ||
List<String> comparativeList = adj2compList.get(adjlemma); | ||
if (comparativeList != null) { | ||
compList.addAll(comparativeList); | ||
} | ||
} | ||
|
||
// remove duplicates | ||
compList = new ArrayList<>(new HashSet<>(compList)); | ||
RuleMatch newMatch = new RuleMatch(match.getRule(), match.getSentence(), match.getFromPos(), match.getToPos(), match.getMessage(), match.getShortMessage()); | ||
// generate suggestion | ||
List<String> suggestionList = prepareSuggestions(compList, noun); | ||
for (String sug : suggestionList) { | ||
newMatch.addSuggestedReplacement(sug); | ||
} | ||
return newMatch; | ||
} | ||
|
||
/* prepare suggesiyton for a list of comparative */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo "suggesiyton" |
||
protected static List<String> prepareSuggestions(List<String> compList, String noun) { | ||
List<String> sugList = new ArrayList<>(); | ||
for (String comp : compList) { | ||
sugList.addAll(prepareSuggestions(comp, noun)); | ||
} | ||
return sugList; | ||
} | ||
|
||
protected static List<String> prepareSuggestions(String comp, String noun) { | ||
/* | ||
الحالات: | ||
الاسم ليس ضميرا | ||
|
||
|
||
ال كم الولد جميل==> ما أجمل الولد | ||
أجمل بالولد | ||
|
||
حالة الضمير | ||
|
||
كم هو جميل==> ما أجمله | ||
أجمل به | ||
|
||
حالة الضفة غير الثلاثية | ||
اسم: | ||
كم الطالب شديد الاستيعاب | ||
ما أشد استيعاب الطالب | ||
أشدد باستيعابه | ||
|
||
ضمير | ||
كم هو شديد الاستيعاب | ||
ما أشد استيعابه | ||
أشد باستيعابه | ||
*/ | ||
|
||
List<String> sugList = new ArrayList<>(); | ||
StringBuilder suggestion = new StringBuilder(); | ||
String newNoun = noun; | ||
// first form of exclamation ما أجمل | ||
// suggestion.append("ما"); | ||
// suggestion.append(" "); | ||
suggestion.append(comp); | ||
if (noun == null || noun.isEmpty()) { | ||
} else if (isPronoun(noun)) { | ||
// no space adding | ||
suggestion.append(ArabicWordMaps.getAttachedPronoun(noun)); | ||
} else { | ||
//if comparative is of second form don't add a space | ||
if (!comp.endsWith(" ب")) { | ||
suggestion.append(" "); | ||
} | ||
suggestion.append(noun); | ||
} | ||
|
||
// add suggestions | ||
sugList.add(suggestion.toString()); | ||
return sugList; | ||
} | ||
|
||
/* test if the word is an isolated pronoun */ | ||
private static boolean isPronoun(String word) { | ||
if (word == null) { | ||
return false; | ||
} | ||
return (word.equals("هو") | ||
|| word.equals("هي") | ||
|| word.equals("هم") | ||
|| word.equals("هما") | ||
|| word.equals("أنا") | ||
); | ||
} | ||
|
||
/* get correspondant attched to unattached pronoun */ | ||
private static String getAttachedPronoun(String word) { | ||
if (word == null) { | ||
return ""; | ||
} | ||
Map<String, String> isolatedToAttachedPronoun = new HashMap<>(); | ||
isolatedToAttachedPronoun.put("هو", "ه"); | ||
isolatedToAttachedPronoun.put("هي", "ها"); | ||
isolatedToAttachedPronoun.put("هم", "هم"); | ||
isolatedToAttachedPronoun.put("هن", "هن"); | ||
isolatedToAttachedPronoun.put("نحن", "نا"); | ||
return isolatedToAttachedPronoun.getOrDefault(word, ""); | ||
} | ||
|
||
protected static Map<String, List<String>> loadFromPath(String path) { | ||
return new SimpleReplaceDataLoader().loadWords(path); | ||
} | ||
|
||
public static String getDataFilePath() { | ||
return FILE_NAME; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...anguage-modules/ar/src/main/java/org/languagetool/rules/ar/AdvancedSynthesizerFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* LanguageTool, a natural language style checker | ||
* Copyright (C) 2022 Sohaib Afifi, Taha Zerrouki | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 | ||
* USA | ||
*/ | ||
|
||
package org.languagetool.rules.ar; | ||
|
||
|
||
import org.languagetool.language.Arabic; | ||
import org.languagetool.rules.AbstractAdvancedSynthesizerFilter; | ||
import org.languagetool.synthesis.Synthesizer; | ||
import org.languagetool.synthesis.ar.ArabicSynthesizer; | ||
|
||
/* | ||
* Synthesize suggestions using the lemma from one token (lemma_from) | ||
* and the POS tag from another one (postag_from). | ||
* | ||
* The lemma_select and postag_select attributes are required | ||
* to choose one among several possible readings. | ||
*/ | ||
public class AdvancedSynthesizerFilter extends AbstractAdvancedSynthesizerFilter { | ||
|
||
private final ArabicSynthesizer synth = new ArabicSynthesizer(new Arabic()); | ||
|
||
@Override | ||
protected Synthesizer getSynthesizer() { | ||
return synth; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
e
as the second argument here, so no information is lost.