Author Archives: MantasCode

C#: How to (parse the text content)/ read from Microsoft Word Document doc

Get the Text out of a Microsoft word file. Read from MS word.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices.ComTypes;
 
namespace readDOC
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object miss = System.Reflection.Missing.Value;
            object path = @"C:\DOC\myDocument.docx";
            object readOnly = true;
            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            string totaltext = "";
            for (int i = 0; i < docs.Paragraphs.Count; i++)
            {
                   totaltext += " \r\n "+ docs.Paragraphs[i+1].Range.Text.ToString();
            }
            Console.WriteLine(totaltext);
            docs.Close();
            word.Quit();
        }
    }
}

Java: Protege Frames: How to get a distinct list of all possible Slots within a Project.

If for whatever reason you might need this list. All you have to do is loop through the Cls’s, then loop through the Slots of the Cls’s, using the method .getOwnSlots()

More Protege Frames how-to’s here http://mantascode.com/?p=507

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
 
import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protege.model.Project;
 
//This Program will output a complete list of distinct slots within a Protege Frames Project file.
 
public class jokke
{
      private static final String PROJECT_FILE_NAME = "C:\\MC\\RadLex.pprj";
      public static void main(String[] args)
      {
            HashSet idsLoaded = new HashSet();
            Collection errors = new ArrayList();
        Project project = new Project(PROJECT_FILE_NAME, errors);
        KnowledgeBase kb = project.getKnowledgeBase();
        ArrayList distinctSlotList = new ArrayList();
        //get Class iterator
        Iterator radClsIter = kb.getClses().iterator();
        //loop through each class
        while ( radClsIter.hasNext())
        {
            //get Cls object
            Cls currentClass = (Cls) radClsIter.next();
            //String for a comma deliminated list of slots
            String slotsString = currentClass.getOwnSlots().toString();
            String [] clsSlotComponents = slotsString.split(",");
            //loop through each slot
            for ( int i = 0 ; i &lt;  clsSlotComponents.length; i++ )
            {
                  //check to see if particular slot already exists in ArrayList
                  if ( distinctSlotList.contains(clsSlotComponents[i].trim().replaceAll("\\[|\\]", "")))
                  {}
                  else
                  {
                        distinctSlotList.add(clsSlotComponents[i].trim().replaceAll("\\[|\\]", ""));
                  }
            }
        }
 
        Iterator iterator = distinctSlotList.iterator();
        while(iterator.hasNext())
        {
            System.out.println(iterator.next());
        }
      }
}

Javascript: How to parse xml, write to html5 local storage, then read from local storage, and allow user to search content.

This simple example will demonstrate how to use javascript to write to, and read from, html5 local storage.

The file being parsed and saved is books.xml.  There are two html pages here, LAUNCH.html and OFFLINE.html.

LAUNCH.html will parse the xml file and write it to the user’s browser:

OFFLINE.html will allow the user to search the content of their local storage

Continue reading

JAVA: How to programmatically scale down all images in a directory using ImageMagick?

You can download Image Magick for free at http://www.imagemagick.org

Images are in C:\images
ImageMagick executable is in C:\ImageMagick\convert.exe

Loop through all files in folder and scale them down 25% of their original size.

Use this command to resize the image and append “s_” to the begining of the file name.

Example:
C:\ImageMagick\convert.exe C:\images\1.jpg -resize 25% C:\images\s_1.jpg

import java.io.File;
import java.io.IOException;
 
public class ImgManipulator
{
	public static void main(String[] args)
	{
		  String path = "c:\\images\\";
		  String files;
		  File folder = new File(path);
		  File[] listOfFiles = folder.listFiles();
		  for (int i = 0; i &lt; listOfFiles.length; i++)
		  {
if (listOfFiles[i].isFile())
{
	files = listOfFiles[i].getName();
	System.out.println("Scaling"+files);
        try
	{
Process p = Runtime.getRuntime().exec(
"C:\\ImageMagick\\convert.exe C:\\images\\"+files+" -resize 25% C:\\images\\s_"+files);
        }
	catch (IOException e)
	{
		e.printStackTrace();
	}
}
		  }
	}
 
}

C#: How to programmatically resize images or create thumbnails from a variety of differently sized images using ffmpeg

ImageMagick should be used to do this,  ffmpeg is for AV.

But to do this using ffmpeg and c#, follow these steps….

You can download ffmpeg for free at http://ffmpeg.org/download.html

My images exists in C:\image_directory and my ffmpeg exists in C:\ffmpeg.

 

I will be using the following ffmpeg command to resize each image (create a thumbnail):

C:\ffmpeg\bin>ffmpeg -i c:\image_directory\1.png -s 80×80 c:\image_directory\t\t_1.png


The above command will take an input image 1.png, resize it to 80px by 80px, save it in a sub-folder named \t\,
and append “t_” to the begining of the file name.

Here is how you would loop through every image in the folder:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
 
namespace ImgResize
{
    class Program
    {
        static void Main(string[] args)
        {
            //get an array of all the image files//
            string[] imgPaths = Directory.GetFiles(@"c:\image_directory\");
 
            //loop through each image//
            for (int i = 0; i &lt; imgPaths.Length; i++)
            {
                string file_path = imgPaths[i];
                //get the file name out of full path//
                string filename = Regex.Match(imgPaths[i], @".*\\([^\\]+$)").Groups[1].Value;
                //output the filename about to be converted
                Console.WriteLine("converting : "+filename);
                //make arguements string
                string write2string = " -i " + file_path + " -s 80x80 "+@"c:\\image_directory\\t\\t_" + filename;
                //create a process
                Process myProcess = new Process();
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.RedirectStandardOutput = true;
                //point ffmpeg location
                myProcess.StartInfo.FileName = @"c:\\ffmpeg\\bin\\ffmpeg";
                //set arguements
                myProcess.StartInfo.Arguments = write2string;
                myProcess.Start();
                myProcess.WaitForExit();
            }
        }
    }
}

JAVA: How to programmatically manipulate a Protégé-Frames lexicon / ontology / dictionary using Protege API and Java.

This tutorial will give a brief overview of how to modify a Protégé-Frames dictionary using Java.

Download Protege:

protege.stanford.edu/

Reference the API:

protege.stanford.edu/protege/3.4/docs/api/core/

The Following examples will require these imports:

import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.Instance;
import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protege.model.Slot;

Assuming you’ve successfully associated the appropriate Protege libraries with your Eclipse, lets get started with some simple HOW-TOs in programmatical Protege ontology manipulation;

For this tutorial I will be using the RadLex Lexicon/Dictionary.

The first thing you will need is the actual project files.
Mine are called : RadLex.pins, RadLex.pont, and RadLex.pprj
These files are created when you save a Lexicon/Dictionary in Frames format using Protege.

Create a pointer to them in your java code. I have mine in in C:\smelo\RadLex.pprj
You only have to point to the *.pprj file.

//Get the project file of the lexicon you want to manipulate
private static final String PROJECT_FILE_Pointer = "C:\\smelo\\RadLex.pprj";

Next, you will need to get the KnowledgeBase object.

//errors object is required in getting the project
Collection errors = new ArrayList();
//Get Project project
Project project = new Project(PROJECT_FILE_Pointer, errors);
//Get Knowledgebase kb
KnowledgeBase kb = project.getKnowledgeBase();

In Protege Frames, each Term/Concept/Lexicon Entity/Thingy is considered a “class”(Cls). The entire RadLex dictionary is nothing more then a bunch of terms(classes) with attributes(slots), and the relationships amongst them.

So lets iterate through all the classes in the KnowledgeBase.

To do this we need to get a class iterator:

//RadLex Class iterator
Iterator radClsIter = kb.getClses().iterator();

Loop through every class

In RadLex, the “name” of every class is its Radlex Id number, or “RID###..”
such as “RID1099” “RID35707” “RID3874” ….

Every RadLex class contains a number of “slots” or attributes that you can populate:

Using the method .getOwnSlots() on a Cls Object will show you a list of all the slots and their names

These are all different attributes(slots) of a single concept(Cls) in RadLex:

Slot(Related_Condition), Slot(Anatomical_Site), Slot(Related_modality), Slot(ACR_ID), Slot(UMLS_Term), Slot(Is_A), Slot(UMLS_ID), Slot(Misspelling of term), Slot(Has_Subtype), Slot(Non-English_name), Slot(Member_Of), Slot(Preferred_name), Slot(Source), Slot(Non-Sanctioned Synonym), Slot(Synonym), Slot(Version_Number), Slot(Term_Status), Slot(SNOMED_ID), Slot(Acronym), Slot(SNOMED_Term), Slot(ACR_Term), Slot(Comment), Slot(Image_URL), Slot(Definition), Slot(:ROLE), Slot(:DOCUMENTATION), Slot(:SLOT-CONSTRAINTS), Slot(:DIRECT-INSTANCES), Slot(:DIRECT-SUPERCLASSES), Slot(:DIRECT-SUBCLASSES), Slot(:DIRECT-TEMPLATE-SLOTS), Slot(:NAME), Slot(:DIRECT-TYPE)

Some slot types are simple strings, while others are more complex Instances.

while( radClsIter.hasNext() )
{
	Cls currentClass = radClsIter.next();
}

So now that we have a brief scattered overview of the RadLex Lexicon/Dictionary,
and we know how to iterate through each Concept/Term/Class within it. Lets do some practical HOW-TOs.

HOW TO get the value of “Preferred_name” slot from a class object(currentClass)?

One way of retrieving of the content of a slot of a class would be by the slot’s name.
So all you have to do is, get another slot iterator this time for the class(term/concept), and compare its name with the one you want.
Then, iterate through the content of the slot and display its value.(You might not have to do this last step; it depends of the type of slot)

In RadLex the Preferred_name slot of a class is populated with an Instance type.
So we have to get the instance of a slot then get the name of the Instance.

String slotName = "Preferred_name"; 
 
Iterator slotIter = currentClass.getOwnSlots().iterator();
 
//Iterate through the slots
while( slotIter.hasNext() )
{
	Slot currentSlot = slotIter.next();
 
	//Compare the slot name with your slotName String
	if( slotName.equals( currentSlot.getName() ) )
    	{
		Object slotContentObject = null;
 
		//Create another iterator for the content of the slot
    		Iterator iterSlotContent = currentClass.getOwnSlotValues(currentSlot).iterator();
 
    		if( iterSlotContent.hasNext() )
    		{
    			slotContentObject = iterSlotContent.next(); 
 
			//if the content of a slot is of type Instance
    			if(slotContentObject instanceof Instance)
  			{
				//cast to Instance
    				Instance instSlotValue = (Instance) slotContentObject;
 
				//use the method getBrowserText() on the Instance to get its String content
    				System.out.println(instSlotValue.getBrowserText());
    			}
    		}
    	}
}

HOW TO change the Definition slot of a Class/Concept/Term by specific “name” (RID) and save to project file?

Say we want to change the content of the “Definition” slot in Cls “RID35712”:

String slotName = "Definition"; 
 
//get the Cls object of the Term/Concept/Class
Cls myConcept = kb.getCls( "RID35712" );
 
Iterator myConceptClsIter = myConcept.getOwnSlots().iterator();
while( myConceptClsIter.hasNext() )
{
	Slot currentSlot = myConceptClsIter.next();
	if(slotName.equals(currentSlot.getName()) )
	{
		//set new Definition
		myConcept.setOwnSlotValue(currentSlot, "This is my new Definition for Term RID35712");
	}
}
 
//save changes
project.save(errors);

HOW TO display ALL synonyms of the entire project?

What if we want to list the names of ALL synonyms throughout the entire dictionary

Iterator radClsIter = kb.getClses().iterator();
 
while( radClsIter.hasNext() ){
        Cls currentClass = radClsIter.next();
        String prefName = findPreferredName(currentClass);
 
        String slotName = "Synonym";
        Iterator slotIter = currentClass.getOwnSlots().iterator();
 
        while( slotIter.hasNext() )
        {
    		Slot currentSlot = slotIter.next();
    		if(slotName.equals(currentSlot.getName()) )
    		{
    			Object slotContentObject = null;
    			Iterator iterSlotContent = currentClass.getOwnSlotValues(currentSlot).iterator();
    			if( iterSlotContent.hasNext() )
    			{
    				slotContentObject = iterSlotContent.next();
    				if(slotContentObject instanceof Instance)
    				{
    					Instance instSlotValue = (Instance) slotContentObject;
    					System.out.println(instSlotValue.getBrowserText());
    				}
    			}
    		}
    	}
}

HOW TO create a new Class/Concept/Term and Add it as a child of RID35712?

We will be assigning the “name” as new unique “RID12345” and adding it as child of the term “RID35712”

Collection myCol =   kb.getClsNameMatches("RID35712",1);
kb.createCls("RID12345", myCol);

That covers a few basics, if you would like an example of something I havent covered, feel free to leave a comment.

GL & HF :D

Android: RadLex – How to HTTP post to a URL, retrieve and parse xml, format and display results in a ListView, override back button, Async Tasks, Loading Dialogs, Webservices and more…

RadLex for android is a program that uses Stanford’s BioPortal Webservices to retrieve data about the RadLex Ontology / Lexicon / Dictionary.  It currently allows the user to navigate the tree structure from concept to concept starting at the root term.

This application requires full Internet access permissions and is dependent on Stanford BioPortal’s webservices http://bioportal.bioontology.org being operational.

RadLex is available for free download at the Android Market: https://market.android.com/details?id=radlex.wave

User Instructions: [Short Click] or tap on a concept, will navigate to its children or sub-terms.  [Long Click] or hold on a concept, will popup a dialog with a definition and other term details (if it has any).


Continue reading

Android: ScotchApp – How to parse Excel data and display it using a ListView and Dialog.

Some time ago, I had come across some interesting data about Scotch Whiskey.  I soon, decided to parse it and display it in an android app.  The app can be downloaded for free at the Android Market https://market.android.com/details?id=Scotch.App

scotchapp

This Scotch data can be downloaded here below.

http://www.bio.umontreal.ca/casgrain/en/labo/scotch.html

This file being parsed : scotch.xls

To get this Excel Data into my android java application:  Using Microsoft Excel, I simply saved it as a *.csv, opened it using notepad, and copy and pasted it into a string variable (String scotchData).

For each row of different Scotch, parse the appropriate columns for their values.

I also added a tasty photograph taken by Kyle May.

Continue reading

C#: Scrabble Player; Part 2 – Find all permutations of a string, parse a dictionary, cheat at Scrabble.

This an extension of a post from before.

C#: Find all permutations of a string, parse a dictionary, cheat at Scrabble.

ScrabblePlayer is a combination of my own joyous curiosity and an accumulation of my previous posts.  It is not built for performance.

This program accepts an input of English Letters from the user.  It then, figures out all possible permutations of the characters(letters), as well as, all permutations of sub-combinations down to a length of two.

Example: User inputs the word “abcd” ;

sub-combinations and -> permutations of input “abcd

each set has its own permutations recurviely.

1.) abcd : 4 characters, 24 permutations; (1*2*3*4)
2.) abc  : 3 characters, 6  permutations; (1*2*3)
3.) ab   : 2 characters, 2  permutations; (1*2)

Next, ScrabblePlayer parses a set of XML files which contain Websters Dictionary content.  The parsing populates both a LinkedList and a HashTable.  LinkedList<string> is set to the word, HashTable<string, string> is set to word as Key, and definition as Value.

After a LinkedList of both the accumulated permutations and dictionary words is created, ScrabblePlayer compares them.  If a permutation of the User’s input is found in the dictionary word list, it is displayed along with its definition.

The dictionary can be downloaded at http://www.ibiblio.org/webster/. I have renamed all the files ( A.xml, B.xml, C.xml, and so on ) .

Continue reading