<commit_before>import static org.junit.Assert.*;

import org.junit.Test;

import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree;

public class PoliqarpPlusTreeTest {
	
	PoliqarpPlusTree ppt;
	String map;

	private boolean equalsContent(String str, Object map) {
		str = str.replaceAll(" ", "");
		String mapStr = map.toString().replaceAll(" ", "");
		return str.equals(mapStr);
	}
	
	private boolean equalsQueryContent(String res, String query) {
		res = res.replaceAll(" ", "");
		ppt = new PoliqarpPlusTree(query);
		String queryMap = ppt.getRequestMap().get("query").toString().replaceAll(" ", "");
		return res.equals(queryMap);
	}
	
	@Test
	public void testContext() {
		String contextString = "{korap=http://korap.ids-mannheim.de/ns/query, @language=de, operands={@id=korap:operands, @container=@list}, relation={@id=korap:relation, @type=korap:relation#types}, class={@id=korap:class, @type=xsd:integer}, query=korap:query, filter=korap:filter, meta=korap:meta}";
		ppt = new PoliqarpPlusTree("[base=test]");
		assertTrue(equalsContent(contextString, ppt.getRequestMap().get("@context")));
	}
	
	@Test
	public void testSingleTokens() {
		// [base=Mann]
		String token1 = "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}";
		assertTrue(equalsQueryContent(token1, "[base=Mann]"));
		
		// [orth!=Frau]
		String token2 = "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation=!=}}";
		assertTrue(equalsQueryContent(token2, "[orth!=Frau]"));
		
		// [!p=NN]
		String token3 = "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation=!=}}";
		assertTrue(equalsQueryContent(token3, "[!p=NN]"));
		
		// [!p!=NN]
		String token4 = "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}";
		assertTrue(equalsQueryContent(token4, "[!p!=NN]"));
	}
	
	@Test
	public void testElements() {
		// <s>
		String elem1 = "{@type=korap:element, @value=s}";
		assertTrue(equalsQueryContent(elem1, "<s>"));
		
		// <vp>
		String elem2 = "{@type=korap:element, @value=vp}";
		assertTrue(equalsQueryContent(elem2, "<vp>"));
	}

	@Test
	public void testCoordinatedFields() {
		// [base=Mann&(cas=N|cas=A)]
		String cof1 = 
			"{@type=korap:token, @value=" +
				"{@type=korap:group, operands=[" +
					"{@type=korap:term, @value=base:Mann, relation==}," +
					"{@type=korap:group, operands=[" +
						"{@type=korap:term, @value=cas:N, relation==}," +
						"{@type=korap:term, @value=cas:A, relation==}" +
					"], relation=or}" +
				"], relation=and}" +
			"}";
		ppt = new PoliqarpPlusTree("[base=Mann&(cas=N|cas=A)]");
		map = ppt.getRequestMap().get("query").toString();
		assertEquals(cof1.replaceAll(" ", ""), map.replaceAll(" ", ""));
	}
	
	@Test
	public void testOccurrence() {
		// [base=foo]*
		String occ1 = "{@type=korap:group, operands=[" +
					     "{@type=korap:token, @value={@type=korap:term, @value=base:foo, relation==}}" +
					  "], relation=repetition, quantifier=* }"; 
		ppt = new PoliqarpPlusTree("[base=foo]*");<commit_msg>Equal serialization test in Poliqarp Serializer<commit_after>import static org.junit.Assert.*;

import org.junit.Test;

import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree;

public class PoliqarpPlusTreeTest {
	
	PoliqarpPlusTree ppt;
	String map;

	private boolean equalsContent(String str, Object map) {
		str = str.replaceAll(" ", "");
		String mapStr = map.toString().replaceAll(" ", "");
		return str.equals(mapStr);
	}
	
	private boolean equalsQueryContent(String res, String query) {
		res = res.replaceAll(" ", "");
		ppt = new PoliqarpPlusTree(query);
		String queryMap = ppt.getRequestMap().get("query").toString().replaceAll(" ", "");
		return res.equals(queryMap);
	}
	
	@Test
	public void testContext() {
		String contextString = "{korap=http://korap.ids-mannheim.de/ns/query, @language=de, operands={@id=korap:operands, @container=@list}, relation={@id=korap:relation, @type=korap:relation#types}, class={@id=korap:class, @type=xsd:integer}, query=korap:query, filter=korap:filter, meta=korap:meta}";
		ppt = new PoliqarpPlusTree("[base=test]");
		assertTrue(equalsContent(contextString, ppt.getRequestMap().get("@context")));
	}
	
	@Test
	public void testSingleTokens() {
		// [base=Mann]
		String token1 = "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}";
		assertTrue(equalsQueryContent(token1, "[base=Mann]"));
		
		// [orth!=Frau]
		String token2 = "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation=!=}}";
		assertTrue(equalsQueryContent(token2, "[orth!=Frau]"));
		
		// [!p=NN]
		String token3 = "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation=!=}}";
		assertTrue(equalsQueryContent(token3, "[!p=NN]"));
		
		// [!p!=NN]
		String token4 = "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}";
		assertTrue(equalsQueryContent(token4, "[!p!=NN]"));
	}
	
	@Test
	public void testElements() {
		// <s>
		String elem1 = "{@type=korap:element, @value=s}";
		assertTrue(equalsQueryContent(elem1, "<s>"));
		
		// <vp>
		String elem2 = "{@type=korap:element, @value=vp}";
		assertTrue(equalsQueryContent(elem2, "<vp>"));
	}

	@Test
	public void testCoordinatedFields() {
		// [base=Mann&(cas=N|cas=A)]
		String cof1 = 
			"{@type=korap:token, @value=" +
				"{@type=korap:group, operands=[" +
					"{@type=korap:term, @value=base:Mann, relation==}," +
					"{@type=korap:group, operands=[" +
						"{@type=korap:term, @value=cas:N, relation==}," +
						"{@type=korap:term, @value=cas:A, relation==}" +
					"], relation=or}" +
				"], relation=and}" +
			"}";
		ppt = new PoliqarpPlusTree("[base=Mann&(cas=N|cas=A)]");
		map = ppt.getRequestMap().get("query").toString();
		assertEquals(cof1.replaceAll(" ", ""), map.replaceAll(" ", ""));


		assertEquals(
		    new PoliqarpPlusTree(" [ base=Mann & ( cas=N | cas=A)] ").getRequestMap().get("query").toString(),
		    new PoliqarpPlusTree("[base=Mann &(cas=N|cas=A)]").getRequestMap().get("query").toString()
	        );
	}
	
	@Test
	public void testOccurrence() {
		// [base=foo]*
		String occ1 = "{@type=korap:group, operands=[" +
					     "{@type=korap:token, @value={@type=korap:term, @value=base:foo, relation==}}" +
					  "], relation=repetition, quantifier=* }"; 
		ppt = new PoliqarpPlusTree("[base=foo]*");
{'fraction_non_alphanumeric': 0.21179454660748256, 'fraction_numerical': 0.004755865567533291, 'mean_word_length': 5.372727272727273, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6526741', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2368, 'n_words': 358}