<commit_before>/*
 * Copyright 2008 Novamente LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package relex.output;

import java.util.HashMap;

import relex.feature.FeatureNode;
import relex.frame.Frame;
import relex.ParsedSentence;

/**
 * Implements OpenCog Scheme output of the semantic frames.
 *
 * Copyright (c) 2008 <NAME> <<EMAIL>>
 */
public class OpenCogSchemeFrame
{
	// The sentence being examined.
	private ParsedSentence sent;

	// The id_map, previously created, for OpenCog id's
	private HashMap<FeatureNode,String> id_map = null;

	private Frame frame;

	/* ------------------------------------------------------------- */
	/* Constructors, and setters/getters for private members. */

	public OpenCogSchemeFrame()
	{
		sent = null;
		id_map = null;
		frame = new Frame();
	}

	public void setParse(ParsedSentence s, HashMap<FeatureNode,String> im)
	{
		sent = s;
		id_map = im;
	}

	/* ------------------------------------------------------------- */

	private String printFrames()
	{
		String ret = "";
		String fin = SimpleView.printRelationsAlt(sent, id_map);
		String[] fms = frame.process(fin);

		for (String fm : fms) 
		{
			// First, parse out the framenet string 
			if (fm.charAt(0) != '^') continue;
			int uscore = fm.indexOf('_');
			if (0 > uscore) continue;

			// String r = fm.substring(0, uscore);

			fm = fm.substring(uscore+1);
			int colon = fm.indexOf(':');
			if (0 > colon) continue;

			String frm = fm.substring(0,colon);

			fm = fm.substring(colon+1);
			int lparen = fm.indexOf('(');
			if (0 > lparen) continue;

			String felt = fm.substring(0,lparen);

			fm = fm.substring(lparen+1);
			int rparen = fm.indexOf(')');
			if (0 > rparen) continue;

			String cpt1 = fm.substring(0,rparen);
			String cpt2 = null;

			// Echo the parsed string 
			ret += "; " + frm + ":" + felt + "(" + cpt1 + ")\n";

			// Are there two concepts, or just one?
			int comma = cpt1.indexOf(',');
			if (0 < comma)
			{
				cpt2 = cpt1.substring(comma+1);
				cpt1 = cpt1.substring(0, comma);
			}
			if (cpt1 == null) continue;
			// cpt1 = id_map.get(cpt1);
			if (cpt1 == null) continue;

			// Is cpt1 a "DefinedLinguisticConceptNode"?
			Boolean cpt1_is_ling = false;
			Boolean cpt2_is_ling = false;
			if (cpt1.charAt(0) == '#') cpt1_is_ling = true;

			if (cpt2 != null)
			{
				// cpt2 = id_map.get(cpt2);
				// if (cpt2 == null) continue;
				if (cpt2.charAt(0) == '#') cpt2_is_ling = true;
			}

			// Are cpt1 and 2 true WordInstances, or concepts?
			Boolean cpt1_is_word = false;
			Boolean cpt2_is_word = false;
			if (-1 < cpt1.indexOf('@')) cpt1_is_word = true;
			if ((cpt2 != null) && (-1 < cpt2.indexOf('@'))) cpt2_is_word = true;

			// Link together.
			ret += "(FrameElementLink\n" +
			       "   (DefinedFrameNode \"#" + frm + "\")\n" +
			       "   (DefinedFrameElementNode \"#" +
                     frm + ":" + felt + "\")\n)\n";

			// Now, for the specific mappings
			ret += "(InheritanceLink\n";
			if (cpt1_is_ling)
			{
				ret += "   (DefinedLinguisticConceptNode \"" + 
				            cpt1 + "\")\n";
			}
			else if (cpt1_is_word)
			{
				ret += "   (WordInstanceNode \"" + cpt1 + "\")\n";
			}
			else
			{
				ret += "   (ConceptNode \"" + cpt1 + "\")\n";
			}
			ret += "   (DefinedFrameNode \"#" + frm + "\")\n)\n";

			// If no second concept, then we are done.
			if (cpt2 == null) continue;

			// Finally link the frame element
			ret += "(EvaluationLink\n";
			ret += "   (DefinedFrameElementNode \"#" +
                     frm + ":" + felt + "\")\n";

			// Embedded: Link first and second concepts together.
			ret += "   (ListLink\n";
			if (cpt1_is_ling)
			{
				ret += "      (DefinedLinguisticConceptNode \"" + 
				            cpt1 + "\")\n";
			}
			else if (cpt1_is_word)
			{
				ret += "      (WordInstanceNode \"" + cpt1 + "\")\n";
			}
			else
			{
				ret += "      (ConceptNode \"" + cpt1 + "\")\n";
			}
			if (cpt2_is_ling)
			{
				ret += "      (DefinedLinguisticConceptNode \"" + 
				            cpt2 + "\")\n";
			}
			else if (cpt2_is_word)
			{
				ret += "      (WordInstanceNode \"" + cpt2 + "\")\n";
			}
			else
			{
				ret += "      (ConceptNode \"" + cpt2 + "\")\n";
			}
			ret += "   )\n)\n";
		}
		return ret;
	}

	/* ------------------------------------------------------------- */
	public String toString()
	{
		return printFrames();
	}
}

/* ============================ END OF FILE ====================== */<commit_msg>Add missing hash marks to the concept nodes<commit_after>/*
 * Copyright 2008 Novamente LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package relex.output;

import java.util.HashMap;

import relex.feature.FeatureNode;
import relex.frame.Frame;
import relex.ParsedSentence;

/**
 * Implements OpenCog Scheme output of the semantic frames.
 *
 * Copyright (c) 2008 <NAME> <<EMAIL>>
 */
public class OpenCogSchemeFrame
{
	// The sentence being examined.
	private ParsedSentence sent;

	// The id_map, previously created, for OpenCog id's
	private HashMap<FeatureNode,String> id_map = null;

	private Frame frame;

	/* ------------------------------------------------------------- */
	/* Constructors, and setters/getters for private members. */

	public OpenCogSchemeFrame()
	{
		sent = null;
		id_map = null;
		frame = new Frame();
	}

	public void setParse(ParsedSentence s, HashMap<FeatureNode,String> im)
	{
		sent = s;
		id_map = im;
	}

	/* ------------------------------------------------------------- */

	private String printFrames()
	{
		String ret = "";
		String fin = SimpleView.printRelationsAlt(sent, id_map);
		String[] fms = frame.process(fin);

		for (String fm : fms) 
		{
			// First, parse out the framenet string 
			if (fm.charAt(0) != '^') continue;
			int uscore = fm.indexOf('_');
			if (0 > uscore) continue;

			// String r = fm.substring(0, uscore);

			fm = fm.substring(uscore+1);
			int colon = fm.indexOf(':');
			if (0 > colon) continue;

			String frm = fm.substring(0,colon);

			fm = fm.substring(colon+1);
			int lparen = fm.indexOf('(');
			if (0 > lparen) continue;

			String felt = fm.substring(0,lparen);

			fm = fm.substring(lparen+1);
			int rparen = fm.indexOf(')');
			if (0 > rparen) continue;

			String cpt1 = fm.substring(0,rparen);
			String cpt2 = null;

			// Echo the parsed string 
			ret += "; " + frm + ":" + felt + "(" + cpt1 + ")\n";

			// Are there two concepts, or just one?
			int comma = cpt1.indexOf(',');
			if (0 < comma)
			{
				cpt2 = cpt1.substring(comma+1);
				cpt1 = cpt1.substring(0, comma);
			}
			if (cpt1 == null) continue;
			// cpt1 = id_map.get(cpt1);
			if (cpt1 == null) continue;

			// Is cpt1 a "DefinedLinguisticConceptNode"?
			Boolean cpt1_is_ling = false;
			Boolean cpt2_is_ling = false;
			if (cpt1.charAt(0) == '#') cpt1_is_ling = true;

			if (cpt2 != null)
			{
				// cpt2 = id_map.get(cpt2);
				// if (cpt2 == null) continue;
				if (cpt2.charAt(0) == '#') cpt2_is_ling = true;
			}

			// Are cpt1 and 2 true WordInstances, or concepts?
			Boolean cpt1_is_word = false;
			Boolean cpt2_is_word = false;
			if (-1 < cpt1.indexOf('@')) cpt1_is_word = true;
			if ((cpt2 != null) && (-1 < cpt2.indexOf('@'))) cpt2_is_word = true;

			// Link together.
			ret += "(FrameElementLink\n" +
			       "   (DefinedFrameNode \"#" + frm + "\")\n" +
			       "   (DefinedFrameElementNode \"#" +
                     frm + ":" + felt + "\")\n)\n";

			// Now, for the specific mappings
			ret += "(InheritanceLink\n";
			if (cpt1_is_ling)
			{
				ret += "   (DefinedLinguisticConceptNode \"#" + 
				            cpt1 + "\")\n";
			}
			else if (cpt1_is_word)
			{
				ret += "   (WordInstanceNode \"" + cpt1 + "\")\n";
			}
			else
			{
				ret += "   (ConceptNode \"#" + cpt1 + "\")\n";
			}
			ret += "   (DefinedFrameNode \"#" + frm + "\")\n)\n";

			// If no second concept, then we are done.
			if (cpt2 == null) continue;

			// Finally link the frame element
			ret += "(EvaluationLink\n";
			ret += "   (DefinedFrameElementNode \"#" +
                     frm + ":" + felt + "\")\n";

			// Embedded: Link first and second concepts together.
			ret += "   (ListLink\n";
			if (cpt1_is_ling)
			{
				ret += "      (DefinedLinguisticConceptNode \"#" + 
				            cpt1 + "\")\n";
			}
			else if (cpt1_is_word)
			{
				ret += "      (WordInstanceNode \"" + cpt1 + "\")\n";
			}
			else
			{
				ret += "      (ConceptNode \"#" + cpt1 + "\")\n";
			}
			if (cpt2_is_ling)
			{
				ret += "      (DefinedLinguisticConceptNode \"#" + 
				            cpt2 + "\")\n";
			}
			else if (cpt2_is_word)
			{
				ret += "      (WordInstanceNode \"" + cpt2 + "\")\n";
			}
			else
			{
				ret += "      (ConceptNode \"#" + cpt2 + "\")\n";
			}
			ret += "   )\n)\n";
		}
		return ret;
	}

	/* ------------------------------------------------------------- */
	public String toString()
	{
		return printFrames();
	}
}

/* ============================ END OF FILE ====================== */
{'fraction_non_alphanumeric': 0.19843342036553524, 'fraction_numerical': 0.015866639887527616, 'mean_word_length': 2.189942344650865, 'pattern_counts': {'":': 6, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4730838', 'n_tokens_mistral': 4040, 'n_tokens_neox': 3455, 'n_words': 889}