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.
The data
Section titled “The data”ميم لغة برمجة عربية، والبرمجة بالعربية ممتعة. في ميم تكتب البرنامج بالعربية، ثم يبنيه المجمّع ملفاً واحداً. لغة ميم صغيرة: كلمات قليلة، وقواعد واضحة. البرمجة بميم تعلّمك الذاكرة: من يحجز، ومن يحرّر؟ ميم، ميم، ميم!
The program
Section titled “The program”# أداة سطر أوامر: أكثر الكلمات تكراراً في ملف نصي# الاستعمال: تكرار <ملف> [عدد_الأوائل]استورد نظاماستورد ملفاتاستورد رياضيات
اخطاء: استعمال_خاطئ
هيكل مدخل: كلمة نص مرات عدد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)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».
Step by step
Section titled “Step by step”- Arguments:
اقرأ_الوسائطreturns!قائمة[نص], and its error isاستعمال_خاطئ(wrong usage) from theاخطاء:block.رئيسي(main) handles it withوالا خ:and prints how to use the program. - Reading:
جرّب ملفات.اقرأ_سطور(مسار)reads the whole file as lines, and theخاتمه حرر(سطور)next to it gives the list back on exit. - Counting: each line is split into pieces with
تقسيم,نظفremoves punctuation from the end of a word withجزء, and the dictionaryعدادcounts. - Sorting:
رتب_ب(مدخلات, قبل)with a comparison function: the most frequent first, then alphabetical order. - Printing:
رياضيات.نسق_عددaligns the columns to a fixed width.
What was hard
Section titled “What was hard”- 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.
What we learned
Section titled “What we learned”- 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-memtells you which line did not free.- In a long loop over strings from
جزء, computeطولonce and keep it.