The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
Feature/adding csharp support to textsplitter (#10350)
Syed Ather Rizvi4258c23867b5
diff --git a/libs/langchain/langchain/text_splitter.py b/libs/langchain/langchain/text_splitter.pyindex d0bf6fca1b..be189548c1 100644--- a/libs/langchain/langchain/text_splitter.py+++ b/libs/langchain/langchain/text_splitter.py@@ -627,6 +627,7 @@ class Language(str, Enum):LATEX = "latex"HTML = "html"SOL = "sol"+ CSHARP = "csharp"class RecursiveCharacterTextSplitter(TextSplitter):@@ -1002,6 +1003,43 @@ class RecursiveCharacterTextSplitter(TextSplitter):"<title","",]+ elif language == Language.CSHARP:+ return [+ "\ninterface ",+ "\nenum ",+ "\nimplements ",+ "\ndelegate ",+ "\nevent ",+ # Split along class definitions+ "\nclass ",+ "\nabstract ",+ # Split along method definitions+ "\npublic ",+ "\nprotected ",+ "\nprivate ",+ "\nstatic ",+ "\nreturn ",+ # Split along control flow statements+ "\nif ",+ "\ncontinue ",+ "\nfor ",+ "\nforeach ",+ "\nwhile ",+ "\nswitch ",+ "\nbreak ",+ "\ncase ",+ "\nelse ",+ # Split by exceptions+ "\ntry ",+ "\nthrow ",+ "\nfinally ",+ "\ncatch ",+ # Split by the normal type of lines+ "\n\n",+ "\n",+ " ",+ "",+ ]elif language == Language.SOL:return [# Split along compiler information definitions@@ -1032,6 +1070,7 @@ class RecursiveCharacterTextSplitter(TextSplitter):" ","",]+else:raise ValueError(f"Language {language} is not supported! "diff --git a/libs/langchain/tests/unit_tests/test_text_splitter.py b/libs/langchain/tests/unit_tests/test_text_splitter.pyindex 39d07b0152..717b28c242 100644--- a/libs/langchain/tests/unit_tests/test_text_splitter.py+++ b/libs/langchain/tests/unit_tests/test_text_splitter.py@@ -498,6 +498,73 @@ public class HelloWorld {]+def test_csharp_code_splitter() -> None:+ splitter = RecursiveCharacterTextSplitter.from_language(+ Language.CSHARP, chunk_size=CHUNK_SIZE, chunk_overlap=0+ )+ code = """+using System;+class Program+{+ static void Main()+ {+ int age = 30; // Change the age value as needed++ // Categorize the age without any console output+ if (age < 18)+ {+ // Age is under 18+ }+ else if (age >= 18 && age < 65)+ {+ // Age is an adult+ }+ else+ {+ // Age is a senior citizen+ }+ }+}+ """++ chunks = splitter.split_text(code)+ assert chunks == [+ "using System;",+ "class Program\n{",+ "static void",+ "Main()",+ "{",+ "int age",+ "= 30; // Change",+ "the age value",+ "as needed",+ "//",+ "Categorize the",+ "age without any",+ "console output",+ "if (age",+ "< 18)",+ "{",+ "//",+ "Age is under 18",+ "}",+ "else if",+ "(age >= 18 &&",+ "age < 65)",+ "{",+ "//",+ "Age is an adult",+ "}",+ "else",+ "{",+ "//",+ "Age is a senior",+ "citizen",+ "}\n }",+ "}",+ ]++def test_cpp_code_splitter() -> None:splitter = RecursiveCharacterTextSplitter.from_language(Language.CPP, chunk_size=CHUNK_SIZE, chunk_overlap=0