Skip to content

Commit fb40b23

Browse files
Merge pull request #15 from Seen-Arabic/development
add wordToLetters function
2 parents bb44e74 + c55bda7 commit fb40b23

9 files changed

Lines changed: 146 additions & 4 deletions

File tree

.github/workflows/npm-publish.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Publish Package to npmjs
2+
3+
# Trigger this workflow when a release is published
4+
on:
5+
release:
6+
types: [published]
7+
8+
jobs:
9+
build:
10+
# Specify the runner environment
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
# Step 1: Check out the repository's code
15+
- name: Check out repository
16+
uses: actions/checkout@v4
17+
18+
# Step 2: Set up Node.js environment
19+
- name: Set up Node.js
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: '20.x' # Specify the Node.js version
23+
registry-url: 'https://registry.npmjs.org'
24+
25+
# Step 3: Install dependencies using npm ci
26+
- name: Install dependencies
27+
run: npm ci
28+
29+
# Step 4: Publish the package to npm
30+
- name: Publish to npm
31+
run: npm publish
32+
env:
33+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Can be used in Node.js and the browser.
3333
- **Tatweel Removal**: Remove unnecessary Tatweel characters from Arabic phrases.
3434
- **Convert To Old Arabic**: Transform Arabic text into old script.
3535
- **Tashfeer**: Replaces Arabic text with visually similar characters for encoding purposes.
36+
- **Word To Letters**: Convert Arabic word to its pronounced letters.
3637

3738
## ⬇️ Installation
3839

@@ -109,6 +110,14 @@ console.log(ArabicServices.tashfeer('هذا النص مشفر'));
109110
// Output: 'هـۮו اڵـݔص مـݭفـݛ'
110111
```
111112

113+
## Word To Letters
114+
115+
```javascript
116+
const { ArabicServices } = require('arabic-services');
117+
console.log(ArabicServices.wordToLetters('شجرة'));
118+
// Output: شين جيم راء تاء_مربوطة
119+
```
120+
112121
<br>
113122

114123
## 💡 Contributing

example.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<script src="./dist/umd/arabic-services.umd.js"></script>
1616
<script>
1717
console.log('ArabicServices:', ArabicServices);
18+
console.log('-------- Functions --------');
1819
console.log('removeTashkeel:', ArabicServices.removeTashkeel('الخَيْلُ وَاللّيْلُ وَالبَيْداءُ تَعرِفُني'));
1920
console.log('removeTatweel:', ArabicServices.removeTatweel('رائـــــــع'));
2021
console.log(
@@ -24,9 +25,12 @@
2425
),
2526
);
2627
console.log('Tashfeer:', ArabicServices.tashfeer('هذا النص مشفر'));
28+
console.log('wordToLetters:', ArabicServices.wordToLetters('شجرة'));
29+
console.log('-------- Constants --------');
2730
console.log('ARABIC_DOTLESS_DICT:', ArabicServices.constants.ARABIC_DOTLESS_DICT);
2831
console.log('STANDARD_LETTERS:', ArabicServices.constants.STANDARD_LETTERS);
2932
console.log('TASHKEEL:', ArabicServices.constants.TASHKEEL);
33+
console.log('PRONOUNCED_LETTERS:', ArabicServices.constants.PRONOUNCED_LETTERS);
3034
</script>
3135
</body>
3236
</html>

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arabic-services",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Utility functions on Arabic text",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/constants/arabic-letters.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,45 @@ export const STANDARD_LETTERS: string[] = [
8080
'ئ',
8181
];
8282

83+
export const PRONOUNCED_LETTERS: { [key: string]: string } = {
84+
ا: 'ألف',
85+
إ: 'ألف_مكسورة',
86+
أ: 'ألف ',
87+
آ: 'ألف_مد',
88+
ء: 'همزة', // Not in STANDARD_LETTERS[]
89+
ب: 'باء',
90+
ت: 'تاء',
91+
ث: 'ثاء',
92+
ج: 'جيم',
93+
ح: 'حاء',
94+
خ: 'خاء',
95+
د: 'دال',
96+
ذ: 'ذال',
97+
ر: 'راء',
98+
ز: 'زاي',
99+
س: 'سين',
100+
ش: 'شين',
101+
ص: 'صاد',
102+
ض: 'ضاد',
103+
ط: 'طاء',
104+
ظ: 'ظاء',
105+
ع: 'عين',
106+
غ: 'غين',
107+
ف: 'فاء',
108+
ق: 'قاف',
109+
ك: 'كاف',
110+
ل: 'لام',
111+
م: 'ميم',
112+
ن: 'نون',
113+
ه: 'هاء',
114+
و: 'واو',
115+
ؤ: 'همزة_متوسطة_مضمومة',
116+
ى: 'ألف_لينة',
117+
ي: 'ياء',
118+
ئ: 'همزة_متوسطة_مكسورة',
119+
ة: 'تاء_مربوطة', // Not in STANDARD_LETTERS[]
120+
};
121+
83122
export const LETTERS_TASHFEER_REPLACEMENT_DICT: { [key: string]: string[] } = {
84123
ا: ['|', '1', '!', 'ן', 'ן', 'ו', 'ⴶ'],
85124
ب: ['ٮ', 'ٻ', 'پ', 'ڀ', 'ٹ', 'ޞ', 'ސ', 'ݐ', 'ݒ', 'ݕ'],

src/constants/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { ARABIC_DOTLESS_DICT, STANDARD_LETTERS } from './arabic-letters';
1+
export { ARABIC_DOTLESS_DICT, PRONOUNCED_LETTERS, STANDARD_LETTERS } from './arabic-letters';
22
export { TASHKEEL } from './tashkeel';

src/scripts/scripts.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ALEF,
44
ALONE_LETTERS,
55
LETTERS_TASHFEER_REPLACEMENT_DICT,
6+
PRONOUNCED_LETTERS,
67
STANDARD_LETTERS,
78
WAW,
89
YAA,
@@ -198,3 +199,32 @@ export function tashfeer(sentence: string): string {
198199
}
199200
return new_sentence.trim();
200201
}
202+
203+
/**
204+
* Converts a word to its pronounced letter representations based on PRONOUNCED_LETTERS.
205+
* @param {string} word - The word to convert.
206+
* @returns {string} The word with pronounced letters separated by spaces.
207+
*/
208+
export function wordToLetters(word: string): string {
209+
let newWord = '';
210+
211+
// Loop through each character in the input word
212+
for (let i = 0; i < word.length; i++) {
213+
const letter = word[i];
214+
215+
// Check if the current letter has a pronunciation in PRONOUNCED_LETTERS
216+
if (PRONOUNCED_LETTERS.hasOwnProperty(letter)) {
217+
newWord += PRONOUNCED_LETTERS[letter];
218+
219+
// Add a space after the pronounced letter unless it's the last letter in the word
220+
if (i !== word.length - 1) {
221+
newWord += ' ';
222+
}
223+
} else {
224+
// If the letter is not in PRONOUNCED_LETTERS, keep it unchanged
225+
newWord += letter;
226+
}
227+
}
228+
229+
return newWord.trim();
230+
}

tests/scripts.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,30 @@ describe('#tashfeer', () => {
7575
expect(result).not.toEqual(inputWord);
7676
});
7777
});
78+
79+
describe('#wordToLetters', () => {
80+
it('should return a string with pronounced Arabic letters', () => {
81+
const inputWord = 'هذه جملة عربية';
82+
const result = ArabicServices.wordToLetters(inputWord);
83+
expect(result).toEqual(expect.any(String));
84+
expect(result).not.toEqual(inputWord);
85+
});
86+
87+
it('should handle empty input', () => {
88+
const inputWord = '';
89+
const result = ArabicServices.wordToLetters(inputWord);
90+
expect(result).toEqual('');
91+
});
92+
93+
it('should handle input with no pronounced Arabic letters', () => {
94+
const inputWord = '12345 not in Arabic letters';
95+
const result = ArabicServices.wordToLetters(inputWord);
96+
expect(result).toEqual(inputWord);
97+
});
98+
99+
it('should handle input with spaces', () => {
100+
const inputWord = 'هذه جملة اخرى';
101+
const result = ArabicServices.wordToLetters(inputWord);
102+
expect(result).toEqual('هاء ذال هاء جيم ميم لام تاء_مربوطة ألف خاء راء ألف_لينة');
103+
});
104+
});

0 commit comments

Comments
 (0)