Skip to content
العربية

Project: most frequent words

The idea: تكرار <ملف> [عدد_الأوائل] counts the words in the file, prints how many there are and how many are distinct, then the most frequent ones.

نص.txt
ميم لغة برمجة عربية، والبرمجة بالعربية ممتعة.
في ميم تكتب البرنامج بالعربية، ثم يبنيه المجمّع ملفاً واحداً.
لغة ميم صغيرة: كلمات قليلة، وقواعد واضحة.
البرمجة بميم تعلّمك الذاكرة: من يحجز، ومن يحرّر؟
ميم، ميم، ميم!
تكرار.م
# أداة سطر أوامر: أكثر الكلمات تكراراً في ملف نصي
# الاستعمال: تكرار <ملف> [عدد_الأوائل]
استورد نظام
استورد ملفات
استورد رياضيات
اخطاء:
استعمال_خاطئ
هيكل مدخل:
كلمة نص
مرات عدد64
ثابت علامات = ".،!؟,;:\"()"
داله نظف(كلمة نص) نص:
# يحذف علامات الترقيم من آخر الكلمة. جزء يرجع عرضاً بلا نسخ، لكن طول ويحوي عليه يحجزان نسخاً لا تُعاد (0.9.1)
ن = كلمة
طالما طول(ن) > 0 و يحوي(علامات, جزء(ن, طول(ن) - 1, طول(ن))):
ن = جزء(ن, 0, طول(ن) - 1)
ارجع ن
داله قبل(ا مدخل, ب مدخل) منطقي:
اذا ا.مرات != ب.مرات:
ارجع ا.مرات > ب.مرات
ارجع ا.كلمة < ب.كلمة
داله اقرأ_الوسائط() !قائمة[نص]:
وسائط = نظام.وسائط()
اذا طول(وسائط) < 1 او طول(وسائط) > 2:
ارجع خطأ.استعمال_خاطئ
ارجع وسائط
داله عد_الكلمات(مسار نص, عداد قاموس[نص, عدد64]) !عدد64:
سطور = جرّب ملفات.اقرأ_سطور(مسار)
خاتمه حرر(سطور)
مجموع = 0
لكل سطر في سطور:
قطع = تقسيم(قص_الفراغات(سطر), " ")
لكل قطعة في قطع:
ك = نظف(قطعة)
اذا طول(ك) == 0:
تجاوز
مجموع += 1
اذا يوجد(عداد, ك):
عداد[ك] = عداد[ك] + 1
غير ذلك:
عداد[ك] = 1
حرر(قطع)
ارجع مجموع
داله رئيسي() !فراغ:
وسائط = اقرأ_الوسائط() والا خ:
اطبع("الاستعمال: تكرار <ملف> [عدد_الأوائل]")
ارجع خ
كم = 10
اذا طول(وسائط) == 2:
كم = حلل(وسائط[1], عدد64) والا خ:
اطبع("عدد الأوائل ليس عدداً: ", وسائط[1])
ارجع خ
عداد قاموس[نص, عدد64] = {}
خاتمه حرر(عداد)
مجموع = عد_الكلمات(وسائط[0], عداد) والا خ:
اطبع("تعذّرت قراءة ", وسائط[0], ": ", خ)
ارجع خ
مدخلات قائمة[مدخل] = []
خاتمه حرر(مدخلات)
لكل ك في مفاتيح(عداد):
اضف(مدخلات, مدخل(ك, عداد[ك]))
رتب_ب(مدخلات, قبل)
اطبع("كلمات: ", مجموع, "، مختلفة: ", طول(عداد))
لكل ع في مدى(0, رياضيات.اصغر_صحيح(كم, طول(مدخلات))):
م = مدخلات[ع]
اطبع(رياضيات.نسق_عدد(ع + 1, 3, خطاء), " ", رياضيات.نسق_عدد(م.مرات, 8, خطاء), " ", م.كلمة)
رئيسي() والا خ:
نظام.انهِ(1)
Output recorded from the compilermeem تكرار.م -- نص.txt 5 · meem 0.17.3+434e13f
كلمات: 35، مختلفة: 28  1         6  ميم  2         2  بالعربية  3         2  لغة  4         1  البرمجة  5         1  البرنامج

Also built with meem build when the site was built: a static file of 73.2 kB, run on its own, and its output matched the output above.

We changed two things from the report’s version: we removed the «internal time» line because its output changes on every run, and we corrected the comment on نظف (clean), which said «a view, with no allocation».

  1. Arguments: اقرأ_الوسائط returns !قائمة[نص], and its error is استعمال_خاطئ (wrong usage) from the اخطاء: block. رئيسي (main) handles it with والا خ: and prints how to use the program.
  2. Reading: جرّب ملفات.اقرأ_سطور(مسار) reads the whole file as lines, and the خاتمه حرر(سطور) next to it gives the list back on exit.
  3. Counting: each line is split into pieces with تقسيم, نظف removes punctuation from the end of a word with جزء, and the dictionary عداد counts.
  4. Sorting: رتب_ب(مدخلات, قبل) with a comparison function: the most frequent first, then alphabetical order.
  5. Printing: رياضيات.نسق_عدد aligns the columns to a fixed width.
  • The hidden cost (in 0.9.1): نظف uses جزء because it is a «view» with no copying. But in 0.9.1, طول(ن) on the view inside the loop allocated a small copy every time, never given back and not visible in the code, and that only showed up with --debug-mem. In 0.10.0, operations on a view allocate nothing.
  • Memory: on a 9 MB file, the built program’s peak was 69.6 MB, against 10.7 for the equivalent Python program: Meem reads the whole file and its lines into memory (there was no line-by-line reading before 0.14.0), and on top of that came the hidden copies in 0.9.1. This measurement has not been redone since 0.9.1.
  • Time: 0.70 s built statically, 0.56 s built with --dynamic, and Python 0.74 s.
  • Errors are values: forgetting to handle !قائمة is a compile error, so the program never reaches run time with a neglected failure.
  • خاتمه حرر(...) next to the allocating line is enough in simple cases, and --debug-mem tells you which line did not free.
  • In a long loop over strings from جزء, compute طول once and keep it.