Learning to Generate Comments for API-Based Code Snippets

  • Conference paper
  • First Online:
Software Engineering and Methodology for Emerging Domains (NASAC 2017, NASAC 2018)

Part of the book series: Communications in Computer and Information Science ((CCIS,volume 861))

Abstract

Comments play an important role in software developments. They can not only improve the readability and maintainability of source code, but also provide significant resource for software reuse. However, it is common that lots of code in software projects lacks of comments. Automatic comment generation is proposed to address this issue. In this paper, we present an end-to-end approach to generate comments for API-based code snippets automatically. It takes API sequences as the core semantic representations of method-level API-based code snippets and generates comments from API sequences with sequence-to-sequence neural models. In our evaluation, we extract 217K pairs of code snippets and comments from Java projects to construct the dataset. Finally, our approach gains 36.48% BLEU-4 score and 9.90% accuracy on the test set. We also do case studies on generated comments, which presents that our approach generates reasonable and effective comments for API-based code snippets.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
EUR 32.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or Ebook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
EUR 29.95
Price includes VAT (France)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
EUR 42.79
Price includes VAT (France)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
EUR 52.74
Price includes VAT (France)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free ship** worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Similar content being viewed by others

Notes

  1. 1.

    https://github.com.

  2. 2.

    https://www.tensorflow.org.

  3. 3.

    https://www.versioneye.com/python/nltk/3.2.1.

References

  1. Abid, N.J., Dragan, N., Collard, M.L., Maletic, J.I.: Using stereotypes in the automatic generation of natural language summaries for C++ methods. In: 2015 IEEE International Conference on Software Maintenance and Evolution (ICSME), pp. 561–565. IEEE (2015)

    Google Scholar 

  2. Allamanis, M., Barr, E.T., Bird, C., Sutton, C.: Suggesting accurate method and class names. In: Proceedings of the 2015 10th Joint Meeting on Foundations of Software Engineering, pp. 38–49. ACM (2015)

    Google Scholar 

  3. Allamanis, M., Peng, H., Sutton, C.: A convolutional attention network for extreme summarization of source code. In: International Conference on Machine Learning, pp. 2091–2100 (2016)

    Google Scholar 

  4. Bahdanau, D., Cho, K., Bengio, Y.: Neural machine translation by jointly learning to align and translate. ar**v preprint ar**v:1409.0473 (2014)

  5. Buse, R.P., Weimer, W.R.: Automatic documentation inference for exceptions. In: Proceedings of the 2008 International Symposium on Software Testing and Analysis, pp. 273–282. Citeseer (2008)

    Google Scholar 

  6. Cho, K., Van Merriënboer, B., Bahdanau, D., Bengio, Y.: On the properties of neural machine translation: encoder-decoder approaches. ar**v preprint ar**v:1409.1259 (2014)

  7. Cho, K., et al.: Learning phrase representations using RNN encoder-decoder for statistical machine translation. ar**v preprint ar**v:1406.1078 (2014)

  8. Haiduc, S., Aponte, J., Marcus, A.: Supporting program comprehension with source code summarization. In: Proceedings of the 32nd ACM/IEEE International Conference on Software Engineering, vol. 2, pp. 223–226. ACM (2010)

    Google Scholar 

  9. Haije, T., Intelligentie, B.O.K., Gavves, E., Heuer, H.: Automatic comment generation using a neural translation model. Inf. Softw. Technol. 55(3), 258–268 (2016)

    Google Scholar 

  10. Hill, E., Pollock, L., Vijay-Shanker, K.: Automatically capturing source code context of NL-queries for software maintenance and reuse. In: Proceedings of the 31st International Conference on Software Engineering, pp. 232–242. IEEE Computer Society (2009)

    Google Scholar 

  11. Iyer, S., Konstas, I., Cheung, A., Zettlemoyer, L.: Summarizing source code using a neural attention model. In: Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics. Long Papers, vol. 1, pp. 2073–2083 (2016)

    Google Scholar 

  12. Kajko-Mattsson, M.: A survey of documentation practice within corrective maintenance. Empirical Softw. Eng. 10(1), 31–55 (2005)

    Article  Google Scholar 

  13. McBurney, P.W., McMillan, C.: Automatic documentation generation via source code summarization of method context. In: Proceedings of the 22nd International Conference on Program Comprehension, pp. 279–290. ACM (2014)

    Google Scholar 

  14. Montandon, J.E., Borges, H., Felix, D., Valente, M.T.: Documenting APIs with examples: lessons learned with the apiminer platform. In: 2013 20th Working Conference on Reverse Engineering (WCRE), pp. 401–408. IEEE (2013)

    Google Scholar 

  15. Movshovitz-Attias, D., Cohen, W.W.: Natural language models for predicting programming comments. In: Proceedings of the 51st Annual Meeting of the Association for Computational Linguistics. Short Papers, vol. 2, vol. 2, pp. 35–40 (2013)

    Google Scholar 

  16. Oda, Y., et al.: Learning to generate pseudo-code from source code using statistical machine translation (t). In: 2015 30th IEEE/ACM International Conference on Automated Software Engineering (ASE), pp. 574–584. IEEE (2015)

    Google Scholar 

  17. Papineni, K., Roukos, S., Ward, T., Zhu, W.J.: Bleu: a method for automatic evaluation of machine translation. In: Proceedings of the 40th annual meeting on association for computational linguistics, pp. 311–318. Association for Computational Linguistics (2002)

    Google Scholar 

  18. Raghothaman, M., Wei, Y., Hamadi, Y.: Swim: synthesizing what i mean-code search and idiomatic snippet synthesis. In: 2016 IEEE/ACM 38th International Conference on Software Engineering (ICSE), pp. 357–367. IEEE (2016)

    Google Scholar 

  19. de Souza, S.C.B., Anquetil, N., de Oliveira, K.M.: A study of the documentation essential to software maintenance. In: Proceedings of the 23rd Annual International Conference on Design of Communication: Documenting & Designing for Pervasive Information, pp. 68–75. ACM (2005)

    Google Scholar 

  20. Sridhara, G., Hill, E., Muppaneni, D., Pollock, L., Vijay-Shanker, K.: Towards automatically generating summary comments for java methods. In: Proceedings of the IEEE/ACM International Conference on Automated Software Engineering, pp. 43–52. ACM (2010)

    Google Scholar 

  21. Takang, A.A., Grubb, P.A., Macredie, R.D.: The effects of comments and identifier names on program comprehensibility: an experimental investigation. J. Prog. Lang. 4(3), 143–167 (1996)

    Google Scholar 

  22. Tenny, T.: Program readability: procedures versus comments. IEEE Trans. Software Eng. 14(9), 1271–1279 (1988)

    Article  Google Scholar 

  23. Thung, F., Lo, D., Lawall, J.: Automated library recommendation. In: 2013 20th Working Conference on Reverse Engineering (WCRE), pp. 182–191. IEEE (2013)

    Google Scholar 

  24. Wong, E., Liu, T., Tan, L.: Clocom: mining existing source code for automatic comment generation. In: 2015 IEEE 22nd International Conference on Software Analysis, Evolution, and Reengineering (SANER), pp. 380–389. IEEE (2015)

    Google Scholar 

  25. Wong, E., Yang, J., Tan, L.: Autocomment: mining question and answer sites for automatic comment generation. In: 2013 28th IEEE/ACM International Conference on Automated Software Engineering (ASE), pp. 562–567. IEEE (2013)

    Google Scholar 

  26. Woodfield, S.N., Dunsmore, H.E., Shen, V.Y.: The effect of modularization and comments on program comprehension. In: Proceedings of the 5th International Conference on Software Engineering, pp. 215–223. IEEE Press (1981)

    Google Scholar 

  27. Zhang, S., Zhang, C., Ernst, M.D.: Automated documentation inference to explain failed tests. In: 2011 26th IEEE/ACM International Conference on Automated Software Engineering (ASE 2011), pp. 63–72. IEEE (2011)

    Google Scholar 

  28. Zhong, H., **e, T., Zhang, L., Pei, J., Mei, H.: MAPO: mining and recommending API usage patterns. In: Drossopoulou, S. (ed.) ECOOP 2009. LNCS, vol. 5653, pp. 318–343. Springer, Heidelberg (2009). https://doi.org/10.1007/978-3-642-03013-0_15

    Chapter  Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Corresponding authors

Correspondence to Ge Li or Zhi ** .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2019 Springer Nature Singapore Pte Ltd.

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Lu, Y., Zhao, Z., Li, G., **, Z. (2019). Learning to Generate Comments for API-Based Code Snippets. In: Li, Z., Jiang, H., Li, G., Zhou, M., Li, M. (eds) Software Engineering and Methodology for Emerging Domains. NASAC NASAC 2017 2018. Communications in Computer and Information Science, vol 861. Springer, Singapore. https://doi.org/10.1007/978-981-15-0310-8_1

Download citation

  • DOI: https://doi.org/10.1007/978-981-15-0310-8_1

  • Published:

  • Publisher Name: Springer, Singapore

  • Print ISBN: 978-981-15-0309-2

  • Online ISBN: 978-981-15-0310-8

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics

Navigation