<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.openoffice.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mismatch</id>
	<title>Apache OpenOffice Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.openoffice.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mismatch"/>
	<link rel="alternate" type="text/html" href="https://wiki.openoffice.org/wiki/Special:Contributions/Mismatch"/>
	<updated>2026-04-26T15:24:37Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.14</generator>
	<entry>
		<id>https://wiki.openoffice.org/w/index.php?title=API/Samples/Groovy/Writer/MostPopularWords&amp;diff=130155</id>
		<title>API/Samples/Groovy/Writer/MostPopularWords</title>
		<link rel="alternate" type="text/html" href="https://wiki.openoffice.org/w/index.php?title=API/Samples/Groovy/Writer/MostPopularWords&amp;diff=130155"/>
		<updated>2009-05-30T10:38:43Z</updated>

		<summary type="html">&lt;p&gt;Mismatch: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;[[Category:API]] [[Category:Samples]] [[Category:Groovy]] [[Category:Writer]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==About the MostPopularWords Example==&lt;br /&gt;
This example shows how to get the text of whole document or its selected parts and how to display some statistics (exactly the most popular words) for this text in info message box in a Groovy macro.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==The Code==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
    Some OpenOffice.org API classes. For more information on&lt;br /&gt;
    these classes and the OpenOffice.org API, see the OpenOffice.org&lt;br /&gt;
    Developers Guide at:&lt;br /&gt;
&lt;br /&gt;
    http://api.openoffice.org/&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.uno.UnoRuntime&lt;br /&gt;
import com.sun.star.uno.XComponentContext&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.container.XIndexAccess&lt;br /&gt;
import com.sun.star.container.XNameContainer&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.beans.XPropertySet&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.frame.XDesktop&lt;br /&gt;
import com.sun.star.frame.XModel&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.awt.XWindow&lt;br /&gt;
import com.sun.star.awt.XWindowPeer&lt;br /&gt;
import com.sun.star.awt.XMessageBoxFactory&lt;br /&gt;
import com.sun.star.awt.XMessageBox&lt;br /&gt;
import com.sun.star.awt.Rectangle&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.lang.XComponent&lt;br /&gt;
import com.sun.star.lang.XMultiComponentFactory&lt;br /&gt;
import com.sun.star.lang.XMultiServiceFactory&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.view.XSelectionSupplier&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.text.XTextDocument&lt;br /&gt;
import com.sun.star.text.XTextRange&lt;br /&gt;
import com.sun.star.text.XText&lt;br /&gt;
&lt;br /&gt;
// The number of words to display as a result&lt;br /&gt;
final int NW = 5&lt;br /&gt;
// These words should be ignored when searching for most popular words&lt;br /&gt;
final List&amp;lt;String&amp;gt; ignoreList = [&amp;#039;a&amp;#039;, &amp;#039;an&amp;#039;, &amp;#039;the&amp;#039;, &amp;#039;in&amp;#039;, &amp;#039;for&amp;#039;, &amp;#039;to&amp;#039;, &amp;#039;at&amp;#039;, &amp;#039;of&amp;#039;, &amp;#039;by&amp;#039;, &lt;br /&gt;
&amp;#039;and&amp;#039;, &amp;#039;or&amp;#039;, &amp;#039;nor&amp;#039;, &amp;#039;whether&amp;#039;, &amp;#039;as&amp;#039;, &amp;#039;with&amp;#039;, &amp;#039;on&amp;#039;, &amp;#039;through&amp;#039;, &amp;#039;when&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
class UnoCategory {&lt;br /&gt;
     public static Object uno(Object unoObj, Class clazz) { UnoRuntime.queryInterface(clazz, unoObj) }&lt;br /&gt;
     public static Object getAt(XPropertySet pset, String pname) { pset.getPropertyValue(pname) }&lt;br /&gt;
     public static void putAt(XPropertySet pset, String pname, Object newValue) { pset.setPropertyValue(pname, newValue) }&lt;br /&gt;
     public static Object getAt(XIndexAccess ndx, int x) { ndx.getByIndex(x) }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// The algorithmic part of this macro&lt;br /&gt;
List&amp;lt;String&amp;gt; getMostPopularWords(String text, List&amp;lt;String&amp;gt; ignoreList, int count) {&lt;br /&gt;
	words = (text =~ /\w+/)&lt;br /&gt;
	freqMap = [:]&lt;br /&gt;
	words.each {&lt;br /&gt;
		String lcWord = it.toLowerCase()&lt;br /&gt;
		if (!ignoreList.contains(lcWord)) {&lt;br /&gt;
			if (null == freqMap[lcWord]) {&lt;br /&gt;
				freqMap[lcWord] = 1&lt;br /&gt;
			} else {&lt;br /&gt;
				freqMap[lcWord]++&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	wordList = freqMap.keySet().toList()&lt;br /&gt;
	wordList.sort {freqMap[it]}&lt;br /&gt;
	return (count &amp;gt;= wordList.size()) ? wordList : wordList[-1..-count]&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This function is used to get the text of whole document or its selected parts&lt;br /&gt;
String getText() {&lt;br /&gt;
	String text = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	use (UnoCategory) {&lt;br /&gt;
		def oDoc = XSCRIPTCONTEXT.document&lt;br /&gt;
		def oModel = oDoc.uno(XModel)&lt;br /&gt;
		def oSelectionSupplier = oModel.currentController.uno(XSelectionSupplier)&lt;br /&gt;
		def oIndexAccess =  oSelectionSupplier.selection.uno(XIndexAccess)&lt;br /&gt;
		def  count = oIndexAccess.count&lt;br /&gt;
		&lt;br /&gt;
		def xText = oDoc.uno(XTextDocument).text&lt;br /&gt;
		if (count &amp;gt; 1) {&lt;br /&gt;
			// More than one selection&lt;br /&gt;
			// Work backwards so that multiple selections stay valid.&lt;br /&gt;
			for (i in count - 1..0) {&lt;br /&gt;
				// get the XTextRange of the selection&lt;br /&gt;
				oTextRange =  oIndexAccess[i].uno(XTextRange)&lt;br /&gt;
				text += &amp;#039; &amp;#039; + oTextRange.string&lt;br /&gt;
			}&lt;br /&gt;
		} else {&lt;br /&gt;
			oTextRange =  oIndexAccess[0].uno(XTextRange)&lt;br /&gt;
			if (0 == oTextRange.string.size()) {&lt;br /&gt;
			// The whole document, no selection&lt;br /&gt;
				text = xText.string&lt;br /&gt;
			} else {&lt;br /&gt;
			// Exactly one selection&lt;br /&gt;
				text = oTextRange.string&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	return text&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Getting parent window for message box&lt;br /&gt;
XWindow getParentWindow(xContext) {&lt;br /&gt;
    use (UnoCategory) {&lt;br /&gt;
        XMultiComponentFactory xMCF = xContext.serviceManager&lt;br /&gt;
        Object boxModel = xMCF.createInstanceWithContext(&amp;quot;com.sun.star.frame.Desktop&amp;quot;, xContext)&lt;br /&gt;
        XDesktop xd = boxModel.uno(XDesktop)&lt;br /&gt;
        XComponent box = xd.currentComponent&lt;br /&gt;
        XModel model = box.uno(XModel)&lt;br /&gt;
        return model.currentController.frame.containerWindow&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Function to display info message box&lt;br /&gt;
def showInfoMsg(title, text) {&lt;br /&gt;
     use (UnoCategory) {&lt;br /&gt;
         XWindow pWnd = getParentWindow(XSCRIPTCONTEXT.componentContext)&lt;br /&gt;
         XWindowPeer pWndPeer = pWnd.uno(XWindowPeer)&lt;br /&gt;
         XMessageBoxFactory msgBoxFactory = pWndPeer.toolkit.uno(XMessageBoxFactory)&lt;br /&gt;
         XMessageBox msgBox = msgBoxFactory.createMessageBox(pWndPeer, new Rectangle(), &amp;quot;infobox&amp;quot;, 0, title, text)&lt;br /&gt;
         msgBox.execute()&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
showInfoMsg(&amp;#039;The most popular words&amp;#039;, getMostPopularWords(getText(), ignoreList, NW).join(&amp;#039; &amp;#039;))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mismatch</name></author>
	</entry>
	<entry>
		<id>https://wiki.openoffice.org/w/index.php?title=API/Samples/Groovy/Writer/MostPopularWords&amp;diff=130154</id>
		<title>API/Samples/Groovy/Writer/MostPopularWords</title>
		<link rel="alternate" type="text/html" href="https://wiki.openoffice.org/w/index.php?title=API/Samples/Groovy/Writer/MostPopularWords&amp;diff=130154"/>
		<updated>2009-05-30T10:36:49Z</updated>

		<summary type="html">&lt;p&gt;Mismatch: (checkpoint save)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;[[Category:API]] [[Category:Samples]] [[Category:Groovy]] [[Category:Writer]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==About the MostPopularWords Example==&lt;br /&gt;
This example shows how to get the text of whole document or its selected parts and how to display some statistics (exactly the most popular words) for this text in info message box in a Groovy macro.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==The Code==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
    Some OpenOffice.org API classes. For more information on&lt;br /&gt;
    these classes and the OpenOffice.org API, see the OpenOffice.org&lt;br /&gt;
    Developers Guide at:&lt;br /&gt;
&lt;br /&gt;
    http://api.openoffice.org/&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.uno.UnoRuntime&lt;br /&gt;
import com.sun.star.uno.XComponentContext&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.container.XIndexAccess&lt;br /&gt;
import com.sun.star.container.XNameContainer&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.beans.XPropertySet&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.frame.XDesktop&lt;br /&gt;
import com.sun.star.frame.XModel&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.awt.XWindow&lt;br /&gt;
import com.sun.star.awt.XWindowPeer&lt;br /&gt;
import com.sun.star.awt.XMessageBoxFactory&lt;br /&gt;
import com.sun.star.awt.XMessageBox&lt;br /&gt;
import com.sun.star.awt.Rectangle&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.lang.XComponent&lt;br /&gt;
import com.sun.star.lang.XMultiComponentFactory&lt;br /&gt;
import com.sun.star.lang.XMultiServiceFactory&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.view.XSelectionSupplier&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.text.XTextDocument&lt;br /&gt;
import com.sun.star.text.XTextRange&lt;br /&gt;
import com.sun.star.text.XText&lt;br /&gt;
&lt;br /&gt;
// The number of words to display as a result&lt;br /&gt;
final int NW = 5&lt;br /&gt;
// These words should be ignored when searching for most popular words&lt;br /&gt;
final List&amp;lt;String&amp;gt; ignoreList = [&amp;#039;a&amp;#039;, &amp;#039;an&amp;#039;, &amp;#039;the&amp;#039;, &amp;#039;in&amp;#039;, &amp;#039;for&amp;#039;, &amp;#039;to&amp;#039;, &amp;#039;at&amp;#039;, &amp;#039;of&amp;#039;, &amp;#039;by&amp;#039;, &lt;br /&gt;
&amp;#039;and&amp;#039;, &amp;#039;or&amp;#039;, &amp;#039;nor&amp;#039;, &amp;#039;whether&amp;#039;, &amp;#039;as&amp;#039;, &amp;#039;with&amp;#039;, &amp;#039;on&amp;#039;, &amp;#039;through&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
class UnoCategory {&lt;br /&gt;
     public static Object uno(Object unoObj, Class clazz) { UnoRuntime.queryInterface(clazz, unoObj) }&lt;br /&gt;
     public static Object getAt(XPropertySet pset, String pname) { pset.getPropertyValue(pname) }&lt;br /&gt;
     public static void putAt(XPropertySet pset, String pname, Object newValue) { pset.setPropertyValue(pname, newValue) }&lt;br /&gt;
     public static Object getAt(XIndexAccess ndx, int x) { ndx.getByIndex(x) }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// The algorithmic part of this macro&lt;br /&gt;
List&amp;lt;String&amp;gt; getMostPopularWords(String text, List&amp;lt;String&amp;gt; ignoreList, int count) {&lt;br /&gt;
	words = (text =~ /\w+/)&lt;br /&gt;
	freqMap = [:]&lt;br /&gt;
	words.each {&lt;br /&gt;
		String lcWord = it.toLowerCase()&lt;br /&gt;
		if (!ignoreList.contains(lcWord)) {&lt;br /&gt;
			if (null == freqMap[lcWord]) {&lt;br /&gt;
				freqMap[lcWord] = 1&lt;br /&gt;
			} else {&lt;br /&gt;
				freqMap[lcWord]++&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	wordList = freqMap.keySet().toList()&lt;br /&gt;
	wordList.sort {freqMap[it]}&lt;br /&gt;
	return (count &amp;gt;= wordList.size()) ? wordList : wordList[-1..-count]&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This function is used to get the text of whole document or its selected parts&lt;br /&gt;
String getText() {&lt;br /&gt;
	String text = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	use (UnoCategory) {&lt;br /&gt;
		def oDoc = XSCRIPTCONTEXT.document&lt;br /&gt;
		def oModel = oDoc.uno(XModel)&lt;br /&gt;
		def oSelectionSupplier = oModel.currentController.uno(XSelectionSupplier)&lt;br /&gt;
		def oIndexAccess =  oSelectionSupplier.selection.uno(XIndexAccess)&lt;br /&gt;
		def  count = oIndexAccess.count&lt;br /&gt;
		&lt;br /&gt;
		def xText = oDoc.uno(XTextDocument).text&lt;br /&gt;
		if (count &amp;gt; 1) {&lt;br /&gt;
			// More than one selection&lt;br /&gt;
			// Work backwards so that multiple selections stay valid.&lt;br /&gt;
			for (i in count - 1..0) {&lt;br /&gt;
				// get the XTextRange of the selection&lt;br /&gt;
				oTextRange =  oIndexAccess[i].uno(XTextRange)&lt;br /&gt;
				text += &amp;#039; &amp;#039; + oTextRange.string&lt;br /&gt;
			}&lt;br /&gt;
		} else {&lt;br /&gt;
			oTextRange =  oIndexAccess[0].uno(XTextRange)&lt;br /&gt;
			if (0 == oTextRange.string.size()) {&lt;br /&gt;
			// The whole document, no selection&lt;br /&gt;
				text = xText.string&lt;br /&gt;
			} else {&lt;br /&gt;
			// Exactly one selection&lt;br /&gt;
				text = oTextRange.string&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	return text&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Getting parent window for message box&lt;br /&gt;
XWindow getParentWindow(xContext) {&lt;br /&gt;
    use (UnoCategory) {&lt;br /&gt;
        XMultiComponentFactory xMCF = xContext.serviceManager&lt;br /&gt;
        Object boxModel = xMCF.createInstanceWithContext(&amp;quot;com.sun.star.frame.Desktop&amp;quot;, xContext)&lt;br /&gt;
        XDesktop xd = boxModel.uno(XDesktop)&lt;br /&gt;
        XComponent box = xd.currentComponent&lt;br /&gt;
        XModel model = box.uno(XModel)&lt;br /&gt;
        return model.currentController.frame.containerWindow&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Function to display info message box&lt;br /&gt;
def showInfoMsg(title, text) {&lt;br /&gt;
     use (UnoCategory) {&lt;br /&gt;
         XWindow pWnd = getParentWindow(XSCRIPTCONTEXT.componentContext)&lt;br /&gt;
         XWindowPeer pWndPeer = pWnd.uno(XWindowPeer)&lt;br /&gt;
         XMessageBoxFactory msgBoxFactory = pWndPeer.toolkit.uno(XMessageBoxFactory)&lt;br /&gt;
         XMessageBox msgBox = msgBoxFactory.createMessageBox(pWndPeer, new Rectangle(), &amp;quot;infobox&amp;quot;, 0, title, text)&lt;br /&gt;
         msgBox.execute()&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
showInfoMsg(&amp;#039;The most popular words&amp;#039;, getMostPopularWords(getText(), ignoreList, NW).join(&amp;#039; &amp;#039;))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mismatch</name></author>
	</entry>
	<entry>
		<id>https://wiki.openoffice.org/w/index.php?title=API/Samples/Groovy/Writer/MostPopularWords&amp;diff=130151</id>
		<title>API/Samples/Groovy/Writer/MostPopularWords</title>
		<link rel="alternate" type="text/html" href="https://wiki.openoffice.org/w/index.php?title=API/Samples/Groovy/Writer/MostPopularWords&amp;diff=130151"/>
		<updated>2009-05-30T09:55:35Z</updated>

		<summary type="html">&lt;p&gt;Mismatch: (checkpoint save)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;[[Category:API]] [[Category:Samples]] [[Category:Groovy]] [[Category:Writer]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==About the MostPopularWords Example==&lt;br /&gt;
This example shows how to get the text of whole document or its selected parts and how to display some statistics (exactly the most popular words) for this text in info message box in a Groovy macro.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==The Code==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
    Some OpenOffice.org API classes. For more information on&lt;br /&gt;
    these classes and the OpenOffice.org API, see the OpenOffice.org&lt;br /&gt;
    Developers Guide at:&lt;br /&gt;
&lt;br /&gt;
    http://api.openoffice.org/&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.uno.UnoRuntime&lt;br /&gt;
import com.sun.star.uno.XComponentContext&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.container.XIndexAccess&lt;br /&gt;
import com.sun.star.container.XNameContainer&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.beans.XPropertySet&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.frame.XDesktop&lt;br /&gt;
import com.sun.star.frame.XModel&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.awt.XWindow&lt;br /&gt;
import com.sun.star.awt.XWindowPeer&lt;br /&gt;
import com.sun.star.awt.XMessageBoxFactory&lt;br /&gt;
import com.sun.star.awt.XMessageBox&lt;br /&gt;
import com.sun.star.awt.Rectangle&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.lang.XComponent&lt;br /&gt;
import com.sun.star.lang.XMultiComponentFactory&lt;br /&gt;
import com.sun.star.lang.XMultiServiceFactory&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.view.XSelectionSupplier&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.text.XTextDocument&lt;br /&gt;
import com.sun.star.text.XTextRange&lt;br /&gt;
import com.sun.star.text.XText&lt;br /&gt;
&lt;br /&gt;
// The number of words to display as a result&lt;br /&gt;
final int NW = 5&lt;br /&gt;
// These words should be ignored when searching for most popular words&lt;br /&gt;
final List&amp;lt;String&amp;gt; ignoreList = [&amp;#039;a&amp;#039;, &amp;#039;an&amp;#039;, &amp;#039;the&amp;#039;, &amp;#039;in&amp;#039;, &amp;#039;for&amp;#039;, &amp;#039;to&amp;#039;, &amp;#039;at&amp;#039;, &amp;#039;of&amp;#039;, &amp;#039;by&amp;#039;, &lt;br /&gt;
&amp;#039;and&amp;#039;, &amp;#039;or&amp;#039;, &amp;#039;nor&amp;#039;, &amp;#039;whether&amp;#039;, &amp;#039;as&amp;#039;, &amp;#039;with&amp;#039;, &amp;#039;on&amp;#039;, &amp;#039;through&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
class UnoCategory {&lt;br /&gt;
     public static Object uno(Object unoObj, Class clazz) { UnoRuntime.queryInterface(clazz, unoObj) }&lt;br /&gt;
     public static Object getAt(XPropertySet pset, String pname) { pset.getPropertyValue(pname) }&lt;br /&gt;
     public static void putAt(XPropertySet pset, String pname, Object newValue) { pset.setPropertyValue(pname, newValue) }&lt;br /&gt;
     public static Object getAt(XIndexAccess ndx, int x) { ndx.getByIndex(x) }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// The algorithmic part of this macro&lt;br /&gt;
List&amp;lt;String&amp;gt; getMostPopularWords(String text, List&amp;lt;String&amp;gt; ignoreList, int count) {&lt;br /&gt;
	words = (text =~ /\w+/)&lt;br /&gt;
	freqMap = [:]&lt;br /&gt;
	words.each {&lt;br /&gt;
		String lcWord = it.toLowerCase()&lt;br /&gt;
		if (!ignoreList.contains(lcWord)) {&lt;br /&gt;
			if (null == freqMap[lcWord]) {&lt;br /&gt;
				freqMap[lcWord] = 1&lt;br /&gt;
			} else {&lt;br /&gt;
				freqMap[lcWord]++&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	wordList = freqMap.keySet().toList()&lt;br /&gt;
	wordList.sort {freqMap[it]}&lt;br /&gt;
	return (count &amp;gt;= wordList.size()) ? wordList : wordList[-1..-count]&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This function is used to get the text of whole document or its selected parts&lt;br /&gt;
String getText() {&lt;br /&gt;
	String text = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	use (UnoCategory) {&lt;br /&gt;
		def oDoc = XSCRIPTCONTEXT.document&lt;br /&gt;
		def oModel = oDoc.uno(XModel)&lt;br /&gt;
		def oSelectionSupplier = oModel.currentController.uno(XSelectionSupplier)&lt;br /&gt;
		def oIndexAccess =  oSelectionSupplier.selection.uno(XIndexAccess)&lt;br /&gt;
		def  count = oIndexAccess.count&lt;br /&gt;
		&lt;br /&gt;
		def xText = oDoc.uno(XTextDocument).text&lt;br /&gt;
		if (count &amp;gt; 1) {&lt;br /&gt;
			// More than one selection&lt;br /&gt;
			// Work backwards so that multiple selections stay valid.&lt;br /&gt;
			for (i in count - 1..0) {&lt;br /&gt;
				// get the XTextRange of the selection&lt;br /&gt;
				oTextRange =  oIndexAccess[i].uno(XTextRange)&lt;br /&gt;
				text += &amp;#039; &amp;#039; + oTextRange.string&lt;br /&gt;
			}&lt;br /&gt;
		} else {&lt;br /&gt;
			oTextRange =  oIndexAccess[0].uno(XTextRange)&lt;br /&gt;
			if (0 == oTextRange.string.size()) {&lt;br /&gt;
			// The whole document, no selection&lt;br /&gt;
				text = xText.string&lt;br /&gt;
			} else {&lt;br /&gt;
			// Exactly one selection&lt;br /&gt;
				text = oTextRange.string&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	return text&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Helper function for displaying message box&lt;br /&gt;
XComponent getBox(xContext) {&lt;br /&gt;
    use (UnoCategory) {&lt;br /&gt;
        XMultiComponentFactory xMCF = xContext.serviceManager&lt;br /&gt;
        Object boxModel = xMCF.createInstanceWithContext(&amp;quot;com.sun.star.frame.Desktop&amp;quot;, xContext)&lt;br /&gt;
        XDesktop xd = boxModel.uno(XDesktop)&lt;br /&gt;
        XComponent box = xd.currentComponent&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Function to display info message box&lt;br /&gt;
def showInfoMsg(title, text) {&lt;br /&gt;
     use (UnoCategory) {&lt;br /&gt;
         XModel model = getBox(XSCRIPTCONTEXT.componentContext).uno(XModel)&lt;br /&gt;
         XWindow pWnd = model.currentController.frame.containerWindow&lt;br /&gt;
         XWindowPeer pWndPeer = pWnd.uno(XWindowPeer)&lt;br /&gt;
         XMessageBoxFactory msgBoxFactory = pWndPeer.toolkit.uno(XMessageBoxFactory)&lt;br /&gt;
         XMessageBox msgBox = msgBoxFactory.createMessageBox(pWndPeer, new Rectangle(), &amp;quot;infobox&amp;quot;, 0, title, text)&lt;br /&gt;
         msgBox.execute()&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mismatch</name></author>
	</entry>
	<entry>
		<id>https://wiki.openoffice.org/w/index.php?title=API/Samples/Groovy/Writer/MostPopularWords&amp;diff=130147</id>
		<title>API/Samples/Groovy/Writer/MostPopularWords</title>
		<link rel="alternate" type="text/html" href="https://wiki.openoffice.org/w/index.php?title=API/Samples/Groovy/Writer/MostPopularWords&amp;diff=130147"/>
		<updated>2009-05-30T09:27:19Z</updated>

		<summary type="html">&lt;p&gt;Mismatch: New page: &amp;lt;noinclude&amp;gt;Category:API Category:Samples Category:Groovy Category:Writer&amp;lt;/noinclude&amp;gt;  ==About the MostPopularWords Example== This example shows how to get the text of whole... (checkpoint save)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;[[Category:API]] [[Category:Samples]] [[Category:Groovy]] [[Category:Writer]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==About the MostPopularWords Example==&lt;br /&gt;
This example shows how to get the text of whole document or its selected parts and how to display some statistics (exactly the most popular words) for this text in info message box in a Groovy macro.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==The Code==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
    Some OpenOffice.org API classes. For more information on&lt;br /&gt;
    these classes and the OpenOffice.org API, see the OpenOffice.org&lt;br /&gt;
    Developers Guide at:&lt;br /&gt;
&lt;br /&gt;
    http://api.openoffice.org/&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.uno.UnoRuntime&lt;br /&gt;
import com.sun.star.uno.XComponentContext&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.container.XIndexAccess&lt;br /&gt;
import com.sun.star.container.XNameContainer&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.beans.XPropertySet&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.frame.XDesktop&lt;br /&gt;
import com.sun.star.frame.XModel&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.awt.XWindow&lt;br /&gt;
import com.sun.star.awt.XWindowPeer&lt;br /&gt;
import com.sun.star.awt.XMessageBoxFactory&lt;br /&gt;
import com.sun.star.awt.XMessageBox&lt;br /&gt;
import com.sun.star.awt.Rectangle&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.lang.XComponent&lt;br /&gt;
import com.sun.star.lang.XMultiComponentFactory&lt;br /&gt;
import com.sun.star.lang.XMultiServiceFactory&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.view.XSelectionSupplier&lt;br /&gt;
&lt;br /&gt;
import com.sun.star.text.XTextDocument&lt;br /&gt;
import com.sun.star.text.XTextRange&lt;br /&gt;
import com.sun.star.text.XText&lt;br /&gt;
&lt;br /&gt;
// The number of words to display as a result&lt;br /&gt;
final int NW = 5&lt;br /&gt;
// These words should be ignored when searching for most popular words&lt;br /&gt;
final List&amp;lt;String&amp;gt; ignoreList = [&amp;#039;a&amp;#039;, &amp;#039;an&amp;#039;, &amp;#039;the&amp;#039;, &amp;#039;in&amp;#039;, &amp;#039;for&amp;#039;, &amp;#039;to&amp;#039;, &amp;#039;at&amp;#039;, &amp;#039;of&amp;#039;, &amp;#039;by&amp;#039;, &lt;br /&gt;
&amp;#039;and&amp;#039;, &amp;#039;or&amp;#039;, &amp;#039;nor&amp;#039;, &amp;#039;whether&amp;#039;, &amp;#039;as&amp;#039;, &amp;#039;with&amp;#039;, &amp;#039;on&amp;#039;, &amp;#039;through&amp;#039;]&lt;br /&gt;
&lt;br /&gt;
class UnoCategory {&lt;br /&gt;
     public static Object uno(Object unoObj, Class clazz) { UnoRuntime.queryInterface(clazz, unoObj) }&lt;br /&gt;
     public static Object getAt(XPropertySet pset, String pname) { pset.getPropertyValue(pname) }&lt;br /&gt;
     public static void putAt(XPropertySet pset, String pname, Object newValue) { pset.setPropertyValue(pname, newValue) }&lt;br /&gt;
     public static Object getAt(XIndexAccess ndx, int x) { ndx.getByIndex(x) }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// The algorithmic part of this macro&lt;br /&gt;
List&amp;lt;String&amp;gt; getMostPopularWords(String text, List&amp;lt;String&amp;gt; ignoreList, int count) {&lt;br /&gt;
	words = (text =~ /\w+/)&lt;br /&gt;
	freqMap = [:]&lt;br /&gt;
	words.each {&lt;br /&gt;
		String lcWord = it.toLowerCase()&lt;br /&gt;
		if (!ignoreList.contains(lcWord)) {&lt;br /&gt;
			if (null == freqMap[lcWord]) {&lt;br /&gt;
				freqMap[lcWord] = 1&lt;br /&gt;
			} else {&lt;br /&gt;
				freqMap[lcWord]++&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	wordList = freqMap.keySet().toList()&lt;br /&gt;
	wordList.sort {freqMap[it]}&lt;br /&gt;
	return (count &amp;gt;= wordList.size()) ? wordList : wordList[-1..-count]&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This function is used to get the text of whole document or its selected parts&lt;br /&gt;
String getText() {&lt;br /&gt;
	String text = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	use (UnoCategory) {&lt;br /&gt;
		def oDoc = XSCRIPTCONTEXT.document&lt;br /&gt;
		def oModel = oDoc.uno(XModel)&lt;br /&gt;
		def oSelectionSupplier = oModel.currentController.uno(XSelectionSupplier)&lt;br /&gt;
		def oIndexAccess =  oSelectionSupplier.selection.uno(XIndexAccess)&lt;br /&gt;
		def  count = oIndexAccess.count&lt;br /&gt;
		&lt;br /&gt;
		def xText = oDoc.uno(XTextDocument).text&lt;br /&gt;
		if (count &amp;gt; 1) {&lt;br /&gt;
			// More than one selection&lt;br /&gt;
			// Work backwards so that multiple selections stay valid.&lt;br /&gt;
			for (i in count - 1..0) {&lt;br /&gt;
				// get the XTextRange of the selection&lt;br /&gt;
				oTextRange =  oIndexAccess[i].uno(XTextRange)&lt;br /&gt;
				text += &amp;#039; &amp;#039; + oTextRange.string&lt;br /&gt;
			}&lt;br /&gt;
		} else {&lt;br /&gt;
			oTextRange =  oIndexAccess[0].uno(XTextRange)&lt;br /&gt;
			if (0 == oTextRange.string.size()) {&lt;br /&gt;
				text = xText.string&lt;br /&gt;
			} else {&lt;br /&gt;
				text = oTextRange.string&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	return text&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mismatch</name></author>
	</entry>
</feed>